现在尝试通过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进行通信! 谢谢!
答案 0 :(得分:0)
你需要克服那个无限循环。你只想等到你得到一个&#39; A&#39;所以改变条件从&#34;总是如此&#34; (1)到#34;直到我们读到&#39;&#39;&#34;
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个字节组成(每个读取都是一个int,所以是2个字节),你也不是在写#&#34;行的结尾&#34;字符(\ 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()
答案 1 :(得分: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?)