Python - read from the serial port data line by line into a list when available(Python - 可用时从串行端口数据逐行读取到列表中)
问题描述
我的目标是编写一个代码,该代码将无限期地监听和读取串行端口,每隔几秒就会产生一次输出
串口输出:
aaaa::abcd:0:0:0//printf("%d
",data[0]);2387//printf("%d
",data[1]);14-9244-44108
我希望将数据附加到这样的列表中,python 假定输出
[abcd::abcd:0:0:0, 2387, 14, -9, 244, -44, 108]
我在许多其他代码中尝试了此代码,但没有任何效果,我一直没有输出编辑-下面的代码给了我这个输出
'''[['abcd::', 'abcd::', 'abcd::', 'abcd::', 'abcd::']] #or[['abcd::abcd:0:0:c9
', '2406
', '14
', '-7
']] # 以此类推,每次迭代输出不同'''#[['aaaa::c30c:0:0:c9
', '2462
', '11
', '-9
', '242
', '-45
','106
']] 显然它只工作了一次.ser = serial.Serial('/dev/ttyUSB1',115200, timeout=10)打印序列号而真:数据 = []data.append(ser.readlines())打印数据# 进一步处理# 将数据发送到其他地方等打印数据ser.close()
readline
会一直读取数据,直到读取一个终止符(新行).请尝试:阅读
.
更新:
使用picocom -b 115200/dev/ttyUSB0
或者putty(serial model) 来检测端口和波特率是否正确.我在你的两个问题中有两个不同的端口.如果打开错误端口,read()
将一直等待直到读取一个字节.像这样:
导入序列号# Windows 7的ser = serial.Serial()ser.port = 'COM1'ser.open()ser.read() # COM1 没有数据,读取一直等到读取一个字节.
如果你在控制台输入这段代码,控制台不会有这样的输出:
<块引用>>>>导入序列号>>>ser = serial.Serial()>>>ser.port = 'COM1'>>>ser.open()>>>ser.read()_
我们需要为读取添加超时来修复它.
你可以试试这个:
导入序列号进口时间z1 波特率 = 115200z1port = '/dev/ttyUSB0' # 运行前设置正确的端口z1serial = serial.Serial(端口=z1port,波特率=z1baudrate)z1serial.timeout = 2 # 设置读取超时# print z1serial # 调试串口.print z1serial.is_open # 打开时为真如果 z1serial.is_open:而真:大小 = z1serial.inWaiting()如果尺寸:数据 = z1serial.read(大小)打印数据别的:打印'没有数据'时间.sleep(1)别的:print 'z1serial 未打开'# z1serial.close() # 如果 z1serial 打开,则关闭 z1serial.
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:
aaaa::abcd:0:0:0
//printf("%d
",data[0]);
2387
//printf("%d
",data[1]);
14
-9
244
-44
108
I want the data to be appended in a list like this, python supposed output
[abcd::abcd:0:0:0, 2387, 14, -9, 244, -44, 108]
I tried this code amongst many others but nothing worked, I keep on getting no output EDIT- the code below gives me this output
'''[['abcd::', 'abcd::', 'abcd::', 'abcd::', 'abcd::']] #or
[['abcd::abcd:0:0:c9
', '2406
', '14
', '-7
']] # and so on, different output for each iteration'''
#[['aaaa::c30c:0:0:c9
', '2462
', '11
', '-9
', '242
', '-45
', '106
']] apparently it worked only once.
ser = serial.Serial('/dev/ttyUSB1',115200, timeout=10)
print ser.name
while True:
data = []
data.append(ser.readlines())
print data
# further processing
# send the data somewhere else etc
print data
ser.close()
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:
import serial
# windows 7
ser = serial.Serial()
ser.port = 'COM1'
ser.open()
ser.read() # COM1 has no data, read keep waiting until read one byte.
if you type this code in console, console will no output like this:
>>> import serial >>> ser = serial.Serial() >>> ser.port = 'COM1' >>> ser.open() >>> ser.read() _
we need add timeout for read to fix it.
you can try this:
import serial
import time
z1baudrate = 115200
z1port = '/dev/ttyUSB0' # set the correct port before run it
z1serial = serial.Serial(port=z1port, baudrate=z1baudrate)
z1serial.timeout = 2 # set read timeout
# print z1serial # debug serial.
print z1serial.is_open # True for opened
if z1serial.is_open:
while True:
size = z1serial.inWaiting()
if size:
data = z1serial.read(size)
print data
else:
print 'no data'
time.sleep(1)
else:
print 'z1serial not open'
# z1serial.close() # close z1serial if z1serial is open.
这篇关于Python - 可用时从串行端口数据逐行读取到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python - 可用时从串行端口数据逐行读取到列表中
基础教程推荐
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01