我正在使用Pyserial在python和arduino之间进行通信。我必须等到执行完arduino操作后才能继续我的python循环。我让arduino在完成它的操作后打印出"done“。我如何使用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‘don’,但是我不知道如何在我的python while循环中引用它。
发布于 2021-02-16 04:37:04
看起来arduino.readline()返回的是bytes,但您将其与str进行比较,因此结果始终为False
>>> print("Done" == b"Done")
False最简单的解决方案是将"Done"更改为b"Done",如下所示:
while arduino.readline() != b"Done":https://stackoverflow.com/questions/66215015
复制相似问题