使用Python读取串行接口会返回失真的数据 [英] Reading serial interface with Python returns distorted data

查看:70
本文介绍了使用Python读取串行接口会返回失真的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python读取二进制串行端口.每个源消息包含3个浮点值,共12个字节.使用下面显示的Python代码,我希望收到一条12字节的 bytearray 消息.但是,运行我发现的代码并不总是12个字节,有时甚至是乱码.我在这里做错什么或为什么会这样?

I am trying to read binary serial port using Python. Each source message consists of 3 floating point values, 12 bytes in total. Using the Python code shown below, I was expecting to get a 12-byte bytearray message. However, running the code I found out it is not always 12 bytes and sometimes it is gibberish. What am I doing wrong here or why is it so?

Pyhon代码:

#!/usr/bin/env python
import serial
import time

serialPort = 'COM3'
serialBaud = 921600
dataNumBytes = 4
numData = 3
rawData = bytearray(numData * dataNumBytes)

# Connect to serial port
print('Trying to connect to ' + str(serialPort) +
      ' at ' + str(serialBaud) + ' BAUD.')
try:
    s = serial.Serial(serialPort, serialBaud, timeout=4)
    print('Connected!')
except:
    print("Failed to connect with " + str(serialPort) +
          ' at ' + str(serialBaud) + ' BAUD.')

s.reset_input_buffer()  # flush input buffer
while (True):
    time.sleep(0.1)
    s.readinto(rawData)
    print(rawData)

终端输出:

bytearray(b'r u?\xb78\x0c\xbe\x1dN\x82>')
bytearray(b'@cu?\xb78\x0c\xbe0\xa7\x82>')
bytearray(b'\xca\x8fu?\x03\x9d\r\xbeno\x81>')
bytearray(b'@cu?\xb78\x0c\xbeno\x81>')
bytearray(b'\x0e\xa6u?\xb78\x0c\xbe\n\xf5\x81>')
bytearray(b'\x0e\xa6u?\xca\x91\x0c\xbe\n\xf5\x81>')
bytearray(b'\x98\xd2u?\xf0C\r\xbe\x81\xc8\x81>')
bytearray(b'\xca\x8fu?\x03\x9d\r\xbe0\xa7\x82>')
bytearray(b'\xca\x8fu?\xb78\x0c\xbe\xb9\xd3\x82>')
bytearray(b'@cu?\xb78\x0c\xbe\x1dN\x82>')
bytearray(b'@cu?\xb78\x0c\xbe\x1dN\x82>')
bytearray(b'\xfcLu?\xdd\xea\x0c\xbe\x94!\x82>')

推荐答案

好的.显然,数据输出是正确的,并且一切正常.我对12字节数据的理解是错误的.这是读取上面显示的示例的第一行的示例:

All right. Apparently, the data output is correct and everything works fine. My understanding of 12-byte data is wrong. Here is an example to read the first line of the example shown above:

>>> from struct import *
>>> data = bytearray(b'r u?\xb78\x0c\xbe\x1dN\x82>')    # define 12-byte data
>>> unpack('f', data[0:4])                              # unpack 1st floating value
(0.9575263261795044,)
>>> unpack('f', data[4:8])                              # unpack 2nd floating value
(-0.13693509995937347,)
>>> unpack('f', data[8:12])                             # unpack 3rd floating value
(0.25450220704078674,)

这篇关于使用Python读取串行接口会返回失真的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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