我正在使用Pyserial在python和arduino之间进行通信。在继续python循环之前,我必须等待arduino操作被执行。我让arduino在完成其操作后打印“完成”。如何使用readline()检查此问题。目前我正在尝试这个方法,但它从未脱离循环:
arduino = serial.Serial(port='COM3', baudrate=9600, timeout=.2)
for coordinate in coordinates:
c = str(coordinate[0]) + ", " + str(coordinate[1])
arduino.write(bytes(c, 'utf-8'))
while arduino.readline() != "Done":
print(arduino.readline())
void loop() {
while (!Serial.available()){
MotorControl(100);
}
MotorControl(0);
String coordinates = Serial.readString();
int i = coordinates.indexOf(',');
int x = coordinates.substring(0, i).toInt();
int y = coordinates.substring(i+1).toInt();
//there will be some other actions here
Serial.print("Done");
在终端中,我可以看到它打印出b'Done',但是我不知道如何在python while循环中引用它
看起来
arduino.readline()
正在返回bytes
,但您将其与str
进行比较,因此结果总是False
:最简单的解决方案是将
"Done"
更改为b"Done"
,如下所示:相关问题 更多 >
编程相关推荐