每隔一秒,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