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

2024-09-30 02:34:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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循环中引用它


Tags: 方法coordinatereadlineserialsubstringarduinointprint
1条回答
网友
1楼 · 发布于 2024-09-30 02:34:04

看起来arduino.readline()正在返回bytes,但您将其与str进行比较,因此结果总是False

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

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

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

相关问题 更多 >

    热门问题