问题描述
我的目标是编写一个代码,该代码将无限期地监听和读取串行端口,每隔几秒就会产生一次输出
串口输出:
我希望将数据附加到这样的列表中,python 假定输出
我在许多其他代码中尝试了此代码,但没有任何效果,我一直没有输出编辑-下面的代码给了我这个输出
readline
会一直读取数据,直到读取一个终止符(新行).请尝试:阅读
.
更新:
使用picocom -b 115200/dev/ttyUSB0
或者putty(serial model) 来检测端口和波特率是否正确.我在你的两个问题中有两个不同的端口.如果打开错误端口,read()
将一直等待直到读取一个字节.像这样:
如果你在控制台输入这段代码,控制台不会有这样的输出:
<块引用>我们需要为读取添加超时来修复它.
你可以试试这个:
I am aiming to write a code that will be indefinitely listening and reading from to a serial port that will have this output produced every few seconds
serial port output:
I want the data to be appended in a list like this, python supposed output
I tried this code amongst many others but nothing worked, I keep on getting no output EDIT- the code below gives me this output
readline
will keep reading data until read a terminator(new line). please try: read
.
UPDATED:
use picocom -b 115200 /dev/ttyUSB0
or putty(serial model) to detect the port and baud-rate is right. I got two different ports in your two questions.if open error port, read()
will keep waiting until read a byte. like this:
if you type this code in console, console will no output like this:
we need add timeout for read to fix it.
you can try this:
这篇关于Python - 可用时从串行端口数据逐行读取到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!