Python连载:如何使用read或readline函数一次读取超过1个字符

问题描述 投票:0回答:5

我无法使用我的程序读取多个字符,我似乎无法弄清楚我的程序出了什么问题。

import serial

ser = serial.Serial(
    port='COM5',\
    baudrate=9600,\
    parity=serial.PARITY_NONE,\
    stopbits=serial.STOPBITS_ONE,\
    bytesize=serial.EIGHTBITS,\
        timeout=0)

print("connected to: " + ser.portstr)
count=1

while True:
    for line in ser.read():

        print(str(count) + str(': ') + chr(line) )
        count = count+1

ser.close()

这是我得到的结果

connected to: COM5
1: 1
2: 2
3: 4
4: 3
5: 1

其实我一直在期待这个

connected to: COM5
1:12431
2:12431

像上面提到的那样,可以同时读取多个字符,而不是一个一个地读取。

python serial-port readline pyserial
5个回答
36
投票

我发现了几个问题。

第一:

ser.read() 一次只会返回 1 个字节。

如果您指定计数

ser.read(5)

它将读取 5 个字节(如果在 5 个字节到达之前发生超时,则读取更少。)

如果您知道您的输入始终以 EOL 字符正确终止, 更好的方法是使用

ser.readline()

将继续读取字符,直到收到 EOL。

第二:

即使你让 ser.read() 或 ser.readline() 返回多个字节, 由于您正在迭代返回值,因此您将 仍然一次处理一个字节。

摆脱

for line in ser.read():

然后说:

line = ser.readline()

21
投票

我用这个小方法用Python读取Arduino串口监视器

import serial
ser = serial.Serial("COM11", 9600)
while True:
     cc=str(ser.readline())
     print(cc[2:][:-5])

12
投票

串口一次发送8位数据,即1个字节,1个字节表示1个字符。

您需要实现自己的方法,该方法可以将字符读入缓冲区,直到到达某个哨兵。约定是发送一条类似

12431\n
的消息,指示一行。

因此,您需要做的是实现一个缓冲区,该缓冲区将存储 X 个字符,一旦达到该值

\n
,就对该行执行操作,然后继续将下一行读入缓冲区。

注意您必须处理缓冲区溢出情况,即当收到的行比您的缓冲区长等时...

编辑

import serial

ser = serial.Serial(
    port='COM5',\
    baudrate=9600,\
    parity=serial.PARITY_NONE,\
    stopbits=serial.STOPBITS_ONE,\
    bytesize=serial.EIGHTBITS,\
        timeout=0)

print("connected to: " + ser.portstr)

#this will store the line
line = []

while True:
    for c in ser.read():
        line.append(c)
        if c == '\n':
            print("Line: " + ''.join(line))
            line = []
            break

ser.close()

6
投票

我从我的 arduino uno 接收了一些日期(0-1023 数字)。 使用 1337holiday、jwygralak67 的代码以及其他来源的一些提示:

import serial
import time

ser = serial.Serial(
    port='COM4',\
    baudrate=9600,\
    parity=serial.PARITY_NONE,\
    stopbits=serial.STOPBITS_ONE,\
    bytesize=serial.EIGHTBITS,\
        timeout=0)

print("connected to: " + ser.portstr)

#this will store the line
seq = []
count = 1

while True:
    for c in ser.read():
        seq.append(chr(c)) #convert from ANSII
        joined_seq = ''.join(str(v) for v in seq) #Make a string from array

        if chr(c) == '\n':
            print("Line " + str(count) + ': ' + joined_seq)
            seq = []
            count += 1
            break


ser.close()

0
投票

有点不同:

import serial

ser = serial.Serial(
    port='COM5',
    baudrate=115200,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout=0
)

print("Connected to:", ser.portstr)

array_tmp = []

while True:
    byte_read = ser.read()  # Read bytes from serial port
    if byte_read:#if not empty
        byte_char = byte_read.decode()  # Decode bytes to string
        array_tmp.append(byte_char)
        if byte_char == '\n':
            print(''.join(array_tmp))  # Print the received line
            print(array_tmp)  # Print the received line
            array_tmp = []

ser.close()
© www.soinside.com 2019 - 2024. All rights reserved.