python - 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
解决方案
我解决它使用:
data = ser.readline()
while data == b'':
ser.write(b"1")
data = ser.readline()
单写好像不行。
推荐阅读
- python-3.x - 如何使用 pywinauto 从没有标题或 id 的静态无名对象获取信息?
- node.js - Puppeteer 查找数据使用情况
- mysql - 将数千条记录插入数据库
- android - Android Maps v2 - 尽管存在侦听器,但信息窗口单击不起作用
- c# - 解决 DI 容器中的循环依赖图
- javascript - Power BI embed + VueJS 错误:“TypeError:无法读取属性 'powerBiEmbed' of null”
- python - 在测试文件中使用一个 @allure.epic 装饰器
- javascript - 将 nextjs 站点部署到 netlify 不断崩溃
- python-3.x - 熊猫从字典中创建一个数据框,其中包含 Numpy 数组
- python - 我正在尝试过滤包含 .html 文件行的数据库表,但出现错误:TypeError: expected string or bytes-like object