【发布时间】:2021-02-15 20:32:21
【问题描述】:
我正在使用 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 的每隔一行是否等于“完成”。另一半(以及仅另一半)只是被打印出来,没有被检查。
标签: python arduino readline pyserial