Python - 可用时从串口数据逐行读取到列表中 [英] Python - read from the serial port data line by line into a list when available

查看:50
本文介绍了Python - 可用时从串口数据逐行读取到列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是编写一个代码,该代码将无限期地侦听和读取串行端口,该串行端口将每隔几秒钟产生一次输出

串口输出:

aaaa::abcd:0:0:0//printf("%d\n",data[0]);2387//printf("%d\n",data[1]);14-9244-44108

我希望将数据附加到这样的列表中,python 假设输出

[abcd::abcd:0:0:0, 2387, 14, -9, 244, -44, 108]

我在许多其他代码中尝试过这段代码,但没有任何效果,我一直没有输出编辑-下面的代码给了我这个输出

'''[['abcd::', 'abcd::', 'abcd::', 'abcd::', 'abcd::']] #或[['abcd::abcd:0:0:c9\n', '2406\n', '14\n', '-7\n']] # 以此类推,每次迭代输出不同'''#[['aaaa::c30c:0:0:c9\n', '2462\n', '11\n', '-9\n', '242\n', '-45\n','106\n']] 显然它只工作了一次.ser = serial.Serial('/dev/ttyUSB1',115200, timeout=10)打印 ser.name为真:数据 = []data.append(ser.readlines())打印数据# 进一步处理# 将数据发送到其他地方等打印数据ser.close()

解决方案

readline 会一直读取数据直到读取到一个终止符(换行).请尝试:阅读.

更新:

使用picocom -b 115200/dev/ttyUSB0 或putty(串口型号)检测端口和波特率是否正确.我在你的两个问题中得到了两个不同的端口.如果打开错误端口,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(port=z1port, baudrate=z1baudrate)z1serial.timeout = 2 # 设置读取超时# 打印 z1serial # 调试串行.print z1serial.is_open # 打开时为真如果 z1serial.is_open:为真:大小 = z1serial.inWaiting()如果尺寸:数据 = z1serial.read(size)打印数据别的:打印无数据"时间.睡眠(1)别的:打印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\n",data[0]);
2387
//printf("%d\n",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\n', '2406\n', '14\n', '-7\n']] # and so on, different output for each iteration''' 
#[['aaaa::c30c:0:0:c9\n', '2462\n', '11\n', '-9\n', '242\n', '-45\n', '106\n']] 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 - 可用时从串口数据逐行读取到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆