从 Arduino 读取数据时 Python readline() 的问题

Issues with Python readline() when reading data from Arduino

我正在尝试使用 pyserial 从 Windows 上的 arduino 读取数据。

import serial

device      = 'COM3'
baud        = 9600

with serial.Serial(device,baud, timeout = 0) as serialPort:
    while True:
        line = serialPort.readline()
        line = line.decode("utf-8")
        if line:
            print(line)
void setup() {
  Serial.begin(9600);
}

void loop() {

  int x = 12;
  int y = 34;
  int z = 56;
  Serial.print(x);
  Serial.print(',');
  Serial.print(y);
  Serial.print(',');
  Serial.println(z);  

}

arduino 串口监视器输出的正是我所期望的。

12,34,56
12,34,56
12,34,56

另一方面,python 脚本正在输出:

1
2,34
,56



12,
34,5
6


1
2,34
,56



12,
34,5
6

我试过延迟Arduino的输出,我试过在arduino代码中制作一个缓冲区,只在缓冲区满时输出数据,我想也许python会有时间正确读取它.

我在这个网站上看到很多人和其他人制作了类似的代码并建议它工作正常,但是我无法从 python 获得连贯的数据。有人知道我的问题吗?

尝试这样做

Python

import serial

device = 'COM3'
baud = 9600

with serial.Serial(device, baud) as port:
    while True:
        print(port.readline().decode("utf-8"))

Arduino

void setup() {
  Serial.begin(9600);
}

void loop() {
  int x = 12;
  int y = 34;
  int z = 56;
  Serial.println(x + ',' + y + ',' + z);  
}