python从串口读取数据的问题

发布于 2024-10-23 19:23:35 字数 1357 浏览 6 评论 0原文

我有一个 python 程序,它试图从串行端口读取 14 个字节 到达速度非常慢。我想要捕获 a 中的所有字节 字节数组[14]。我知道 python 3.0 中有新的字节数组功能,但是 我只运行 python 2.6.6。升级可能会产生意想不到的后果,所以我必须 坚持使用 2.6.6。

数据仅间歇性地通过串行端口流动。我收到一条消息 端口可能每 2 分钟左右一次。该数据流动非常缓慢。我看到的问题是 我的代码不能一次可靠地读取一个字节的数据。我想把这个框起来 数据恰好为 14 字节,然后处理数据并以新的 14 字节重新开始 字节。

我在这里采取了错误的方法吗?建议?

    ser = serial.Serial('/dev/ttyUSB1', 1200, timeout=0)
    ser.open()
    print "connected to: " + ser.portstr
    count=0
    while True:
        line =ser.readline(size=14) # should block, not take anything less than 14 bytes
        if line:
            # Here I want to process 14 bytes worth of data and have
            # the data be consistent.
            print "line(" + str(count) + ")=" + line
            count=count+1
    ser.close()

这是我所期望的: line(1)=� 0~ 888.ABC� /以回车符结尾

----------开始输出------

line(0)=0
line(1)=~ ??1. ABC �    # here get some multiple bytes and stuff gets out of synch

�ine(2)=
line(3)=0
line(4)=~
line(5)= 
line(6)=8
line(7)=8
line(8)=8
line(9)=.
line(10)= 
line(11)=A
line(12)=B
line(13)=C
line(14)= 
line(15)=�
line(16)=

#...
line(48)=
line(49)=�
line(50)=0
line(51)=~
line(52)= 
line(53)=8
line(54)=8
line(55)=8
line(56)=.
line(57)= 
line(58)=A
line(59)=B
line(60)=C
line(61)= 
line(62)=�
line(63)=

line(64)=

-------- --结束输出------

I have a python program that is trying to read 14 bytes from a serial port that
are arriving very slowly. I want want to capture all of the bytes in a
bytearray[14]. I understand there are new byte array features in python 3.0, but
I'm only running python 2.6.6. Upgrading may have unexpected consequences so I have to
stick with 2.6.6.

Data only intermittently flows over the serial port. I get one message on the
port maybe every 2 minutes or so. This data flows very slowly. The problem I am seeing is
that my code does not relaibly read the data one byte at a time. I want to frame this
data at exactly 14 bytes, then process the data and start fresh with a new 14
bytes.

Am I taking the wrong approach here? Advice?

    ser = serial.Serial('/dev/ttyUSB1', 1200, timeout=0)
    ser.open()
    print "connected to: " + ser.portstr
    count=0
    while True:
        line =ser.readline(size=14) # should block, not take anything less than 14 bytes
        if line:
            # Here I want to process 14 bytes worth of data and have
            # the data be consistent.
            print "line(" + str(count) + ")=" + line
            count=count+1
    ser.close()

Here's what I'm expecting: line(1)=� 0~ 888.ABC� /ends in carriage return

----------begin output------

line(0)=0
line(1)=~ ??1. ABC �    # here get some multiple bytes and stuff gets out of synch

�ine(2)=
line(3)=0
line(4)=~
line(5)= 
line(6)=8
line(7)=8
line(8)=8
line(9)=.
line(10)= 
line(11)=A
line(12)=B
line(13)=C
line(14)= 
line(15)=�
line(16)=

#...
line(48)=
line(49)=�
line(50)=0
line(51)=~
line(52)= 
line(53)=8
line(54)=8
line(55)=8
line(56)=.
line(57)= 
line(58)=A
line(59)=B
line(60)=C
line(61)= 
line(62)=�
line(63)=

line(64)=

----------end output------

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鸩远一方 2024-10-30 19:23:36

PySerial 的 API 文档说 readline() 读取直到 \n,或者直至达到尺寸。但是, read() 函数表示它将阻塞,直到达到大小。这两个函数都表示,如果设置了超时,它们将提前返回。

Possible values for the parameter timeout:

timeout = None: wait forever
timeout = 0: non-blocking mode (return immediately on read)
timeout = x: set timeout to x seconds (float allowed)

也许尝试使用 timeout=None 而不是 timeout=0

The API documentation for PySerial says readline() reads until \n, or until size is reached. However, the read() function says it will block until size is reached. Both of these functions say they will return early if a timeout is set.

Possible values for the parameter timeout:

timeout = None: wait forever
timeout = 0: non-blocking mode (return immediately on read)
timeout = x: set timeout to x seconds (float allowed)

Maybe try timeout=None instead of timeout=0

玩心态 2024-10-30 19:23:36

试试这个:

ser = Serial('/dev/ttyUSB1', 1200, timeout=0)
print "connected to: " + ser.portstr
count=0
while True:
     for line in ser.readlines()   
          print "line(" + str(count) + ")=" + line
          count=count+1

ser.close()   

如果您还没有使用过 line.strip() 函数,您可能也会感兴趣。另外,强调 ser.readlines() 中的 s,与 .readline() 不同。

Try this:

ser = Serial('/dev/ttyUSB1', 1200, timeout=0)
print "connected to: " + ser.portstr
count=0
while True:
     for line in ser.readlines()   
          print "line(" + str(count) + ")=" + line
          count=count+1

ser.close()   

You might also be interested in the line.strip() function if you haven't used it. Also, emphasis on the s in ser.readlines(), different from .readline().

jJeQQOZ5 2024-10-30 19:23:36

更改波特率。

ser = serial.Serial('/dev/ttyUSB1', 9600, timeout=0)

到兼容的波特率。

获取 14 字节数据:

data = ser.read(size=14)

Change the baudrate.

ser = serial.Serial('/dev/ttyUSB1', 9600, timeout=0)

to a compatible baudrate.

For getting 14 byte data :

data = ser.read(size=14)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文