Pyserial readline() 并等到收到一个值才能继续

Pyserial readline() and wait until receives a value to continue

我正在使用 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:

>>> print("Done" == b"Done")
False

最简单的解决方案是将 "Done" 更改为 b"Done",如下所示:

    while arduino.readline() != b"Done":