首页 > 解决方案 > Pyserial 可以在命令行中从 arduino 读取数据,但 python 文件不能

问题描述

我正在使用 pyserial 使用 mpu6050 从 arduino 读取串行数据。它在命令行中正常运行,但在 python 文件中什么也不读取。

这适用于 Arduino UNO、pyserial3.4、波特率 115200、Window10。

#in command line
>>> ser = serial.Serial("com3", 115200, timeout=1)
>>> ser.write(b"1")     #write a chatacter to begin
>>> ser.readline()
b'ypr\t-84.35\t7.81\t-1.14\r\n'

# in a python file
ser = serial.Serial("com3", 115200, timeout = 1)   
ser.write(b"1")
#time.sleep(1)          #makes no difference
data = ser.readline()   #timeout and read nothing

标签: pythonarduinopyserial

解决方案


我解决它使用:

data = ser.readline()
while data == b'':
    ser.write(b"1")
    data = ser.readline()

单写好像不行。


推荐阅读