在尝试在Python(在Win10 PC上)和Arduino(Uno)之间执行半双工通信时,我正在将一些字符串命令从PC发送到ARD,并根据读取的字符串输入发回一个特殊的声明或回显已读信息。
为了测试这一点,我为Arduino和Python写了一套非常基本的脚本。
Arduino:
// Test
// <python_script.py>
// https://github.com/fortruce/fortruce.github.io/blob/master/_posts/2014-8-31-talking-to-arduino-with-python.md
void setup(){
Serial.begin(9600);
//Serial.setTimeout(10000); // change from 1s to 10s timeout.
pinMode(7, INPUT);
}
// ARD sends data to PY
void loop(){
//Serial.write(0x41);
//delay(1000);
while(!Serial.available()){
//Serial.write(0x01);
//delay(300);
}
//Serial.read(); // read single byte being sent-over
// <https://www.arduino.cc/reference/en/language/functions/communication/serial/readstring/>
if(Serial.available()){
String input = Serial.readStringUntil("\n"); // If there are available bytes to read, them them into a String
// Functionality appears the same, whether using <readStringUntil("\n")> or <readString()>
if(input=="*SETUP?"){
/* Try-catch doesn't exist in C */
// http://www.cplusplus.com/doc/tutorial/exceptions/
if(Serial.availableForWrite()){
Serial.println("ARD told to do Setup!");
}
}
else if(input=="testAN"){
float val[6]; // Size = 4 bytes per Float, = 4*6 = 24
for(int i=0; i<6; i++){
val[i] = analogRead(i);
}
if(Serial.availableForWrite()){
Serial.print("<");
Serial.print(val[0]);
Serial.print(",");
Serial.print(val[1]);
Serial.print(",");
Serial.print(val[2]);
Serial.print(",");
Serial.print(val[3]);
Serial.print(",");
Serial.print(val[4]);
Serial.print(",");
Serial.print(val[5]);
Serial.println(">");
}
}
else{
if(Serial.availableForWrite()){
Serial.print("ARD received: {");
Serial.print(input);
Serial.println("}.");
}
}
// <https://www.arduino.cc/reference/en/language/functions/communication/serial/flush/>
Serial.flush(); // wait for outgoing data to finish.
}
}
// EOL
Python:
# Test
# https://github.com/fortruce/fortruce.github.io/blob/master/_posts/2014-8-31-talking-to-arduino-with-python.md
import serial, time
import struct
ard = serial.Serial('COM4')
ard.reset_input_buffer()
ard.reset_output_buffer()
### Need a way to break this loop; once we get the info we want, move to the next PY step...
while(True):
output = raw_input("Type what you want to send and hit 'Enter': ")
### Other forms of "output" I have tried:
#output = "testAN" ### This is what I thought I could send...
#output = "testAN\n"
#output = "testAN\r"
#output = "testAN\n\r"
#output = "testAN\r\n"
print("<" + unicode(output) + ">")
###print("b")
ard.reset_output_buffer()
###print("c")
print(ard.write(output))
#ard.write(b"%s" % output)
#time.sleep(0.5) # Give the ARD time to read...
###print("d")
#ard.flushInput() # Wait for Input to be of
print("e")
rec = ard.readline().strip("\n") # HUNG HERE
#rec = ard.read_until("\n") # HUNG HERE
ard.reset_input_buffer() # Reset input AFTER capture
print("f")
#print(rec)
rec = rec.strip("\n")
print("g")
#print(rec)
if(">" in rec):
print(rec) # To be sure...
break;
else:
print("Echo from ARD is: {%s}." % rec)
# End if-else
# End while-loop
print("Data was received, now can do next thing!")
# EOL
我交换
output = raw_input("Input: ") # type 'testAN' and hit 'Enter'
在带有
的Python中output = "testAN" # Constant string
,此Python脚本挂在readline()语句上。为什么是这样?我已经确认Write()正在发送相同数量的字节(6),而与源无关。
# e.g.
print(ard.write(raw_input(": "))) # w/input testAN
# and
print(ard.write("testAN"))
都返回6。