小编Ash*_*tha的帖子

pySerial 中的 readline() 有时会捕获从 Arduino 串行端口流式传输的不完整值

每隔一秒,Arduino 就会(串行)打印“当前时间(以毫秒为单位)和 Hello world ”。在串行监视器上,输出看起来不错。

但在 中pySerial,有时字符串中间会出现换行符。

313113   Hel
lo world
314114   Hello world
315114   Hello world
Run Code Online (Sandbox Code Playgroud)

我的Python代码如下:

import serial
import time
ser = serial.Serial(port='COM4',
                    baudrate=115200,
                    timeout=0)

while True:
   str = ser.readline()         # read complete line
   str = str.decode()           # decode byte str into Unicode
   str = str.rstrip()           

   if str != "":
      print(str)
   time.sleep(0.01)
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

我的配置:

Python 3.7
pySerial 3.4
板 Arduino Mega

python arduino pyserial python-3.x

5
推荐指数
1
解决办法
5650
查看次数

标签 统计

arduino ×1

pyserial ×1

python ×1

python-3.x ×1