通过Serial1将Arduino Yun引脚信息发送到Linino Python脚本,再通过UDP发送到Max

2 投票
2 回答
2326 浏览
提问于 2025-04-17 23:59

现在我正在尝试通过Python脚本发送我的Arduino程序,但我一直无法成功发送。

这是Arduino的代码。

    /* Sketch for reading 4 analogue inputs for glove
*/

// Setup pin locations
int flexPin0 = A0; //analog pin 0
int flexPin1 = A1; //analog pin 1
int flexPin2 = A2; //analog pin 2
int flexPin3 = A3; //analog pin 3

int inByte = 0;

void setup(){
  Serial.begin(9600); 
  while (!Serial) {
    ;
  }  
  establishContact(); 
}

void loop(){
  // Read values
    // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    // get incoming byte:
    inByte = Serial.read();

  int flexSensorReading0 = analogRead(flexPin0); 
  delay(10);
  int flexSensorReading1 = analogRead(flexPin1);
  delay(10);
  int flexSensorReading2 = analogRead(flexPin2); 
  delay(10);
  int flexSensorReading3 = analogRead(flexPin3); 
  delay(10);

  // Output results to serial
    Serial.write(flexSensorReading0);
    Serial.write(flexSensorReading1);
    Serial.write(flexSensorReading2);
    Serial.write(flexSensorReading3);

  delay(50); //just here to slow down the output for easier reading
  }
}

void establishContact() {
  while (Serial.available() <= 0) {
    Serial.println('A');   // send a capital A
    delay(500);
  }
}

这是Python脚本。

#!/usr/bin/python
import socket
import serial

ser = serial.Serial('/dev/ttyATH0', 9600)


while ser.read() != 'A':
#   do nothing
    pass


UDP_IP = "192.168.1.242"   #Max IP address
UDP_PORT = 8888
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP

while True: # ser.readline() == 'A':
    data_raw = read(8) #read 8 bytes
    sock.sendto(bytes(data_raw), (UDP_IP, UDP_PORT)) #sends the byte
    ser.write('A')

#recvmsg=sock.recv(1024) #read response
#print recvmsg
sock.close()

现在我遇到了一个错误。

Traceback (most recent call last):
  File "/mnt/sda1/udpgit.py", line 8, in <module>
    while ser.read() != 'A':
  File "/usr/lib/python2.7/site-packages/pyserial-2.7-py2.7.egg/serial/serialposix.py", line 475, in read
    raise SerialException('device reports readiness to read but returned no data (device disconnected or multiple access on port?)')
serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)

当Arduino上运行的是一个空的程序时,Python脚本可以正常运行,但一旦我尝试使用serial1,就会出现那个错误。

我正在使用Arduino Yun,想通过WiFi与Max进行通信!谢谢!

2 个回答

0

错误!

Traceback (most recent call last):
  File "/mnt/sda1/udpgit.py", line 8, in <module>
    while ser.read() != 'A':
  File "/usr/lib/python2.7/site-packages/pyserial-2.7-py2.7.egg/serial/serialposix.py", line 475, in read
    raise SerialException('device reports readiness to read but returned no data (device disconnected or multiple access on port?)')
serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)
0

首先,你需要解决那个无限循环的问题。你只想等到收到一个'A',所以把条件从“总是为真”(1)改成“直到我们读到'A'”。

while ser.readline() != 'A':
    //do nothing

然后打开套接字。

UDP_IP = "192.168.1.242"   #Uno IP address
UDP_PORT = 8888
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP

现在我们需要读取Arduino的数据,这些数据由8个字节组成(每个读取是一个整数,所以是2个字节)。另外,你没有写“行结束”字符(\n),所以readLine()方法是无法工作的。

data_raw = read(8) #read 8 byte
sock.sendto(bytes(MESSAGE), (UDP_IP, UDP_PORT)) #send byte
recvmsg=sock.recv(1024) #read response messagge (are you sure aout this?)
print recvmsg

现在我们可以关闭套接字。如果我们还想读取下一个8个字节的数据,只需重复上面的代码。

sock.close()

撰写回答