Python - 可用时从串口数据逐行读取到列表中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35936644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:10:11  来源:igfitidea点击:

Python - read from the serial port data line by line into a list when available

pythonserial-port

提问by Ahmed Al-haddad

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

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

[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()

回答by Mitchell Chu

readlinewill keep reading data until read a terminator(new line). please try: read.

readline将继续读取数据,直到读取终止符(新行)。请尝试:read

UPDATED:

更新:

use picocom -b 115200 /dev/ttyUSB0or 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:

使用picocom -b 115200 /dev/ttyUSB0或腻子(串行模型)来检测端口和波特率是否正确。我在你的两个问题中有两个不同的端口。如果打开错误端口,read()将一直等到读取一个字节。像这样:

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()
_
>>> 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.