繁体   English   中英

从 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

I have tried delaying the output from the Arduino, I have tried making a buffer in the arduino code and only output the data when the buffer was full, thinking maybe python would have time to read it correctly.

我在这个网站上看到很多人,其他人也编写了类似的代码并建议它工作正常,但是我无法从 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);  
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM