[英]Converting data from serial/usb using PySerial
I have a UBlox receiver connected to my computer and I am trying to read it using PySerial however I am new to python and was hoping to get some clarification/help on understanding the data. 我有一个连接到我的计算机的UBlox接收器,我正在尝试使用PySerial读取它,但是我是python的新手,希望对理解数据有一些澄清/帮助。
My code looks like: 我的代码如下:
import serial
# open the connection port
connection = serial.Serial('/dev/ttyACM0', 9600)
# open a file to print the data. I am doing this to make
# sure it is working
file1 = open('output_file', 'wb+')
# All messages from ublox receivers end with a carriage return
# and a newline
msg = connection.readline()
# print the message to the file
print >> file1, msg
What I get in the file, and when I print the 'type' of msg it is a list: 我在文件中得到的信息,当我打印味精的“类型”时,它是一个列表:
['\\xb5b\\x01\\x064\\x00\\xe0\\x88\\x96#\\xd3\\xb9\\xff\\xffX\\x07\\x03\\xdd6\\xc31\\xf6\\xfd)\\x18\\xea\\xe6\\x8fd\\x1d\\x00\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\xfd\\xff\\xff\\xff\\x01\\x00\\x00\\x00\\x02\\x00\\x00\\x00p\\x00\\x02\\x0f\\x16\\xa2\\x02\\x00\\x9c\\xeb\\xb5b\\x01\\x07\\\\x00\\xe0\\x88\\x96#\\xe0\\x07\\x01\\x17\\x15237\\x04\\x00\\x00\\x00\\xd6\\xb9\\xff\\xff\\x03\\x01\\n'] ['\\ xb5b \\ x01 \\ x064 \\ x00 \\ xe0 \\ x88 \\ x96#\\ xd3 \\ xb9 \\ xff \\ xffX \\ x07 \\ x03 \\ xdd6 \\ xc31 \\ xf6 \\ xfd)\\ x18 \\ xea \\ xe6 \\ x8fd \\ x1d \\ x00 \\ x01 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ xfd \\ xff \\ xff \\ xff \\ x01 \\ x00 \\ x00 \\ x00 \\ x02 \\ x00 \\ x00 \\ x00p \\ x00 \\ x02 \\ x0f \\ x16 \\ xa2 \\ x02 \\ x00 \\ x9c \\ xeb \\ xb5b \\ x01 \\ x07 \\\\ x00 \\ xe0 \\ x88 \\ x96#\\ xe0 \\ x07 \\ x01 \\ x17 \\ x15237 \\ x04 \\ x00 \\ x00 \\ x00 \\ xd6 \\ xb9 \\ xff \\ xff \\ x03 \\ x01 \\ n']
["\\x1a\\x0c\\x04\\x19'y\\x00$\\xf7\\xff\\xff\\x1a\\x1d\\x04\\x01\\x00\\x007\\x00\\x00\\x00\\x00\\x00\\x02\\x1f\\x0c\\x01\\x00+:\\x00\\x00\\x00\\x00\\x00\\x01 \\r\\x07&-\\x9f\\x00\\xff\\x01\\x00\\x00\\x17\\xc1\\x0c\\x04\\x16\\n"] [“ \\ x1a \\ x0c \\ x04 \\ x19'y \\ x00 $ \\ xf7 \\ xff \\ xff \\ x1a \\ x1d \\ x04 \\ x01 \\ x00 \\ x007 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x02 \\ x1f \\ x0c \\ x01 \\ x00 +:\\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x01 \\ r \\ x07&-\\ x9f \\ x00 \\ xff \\ x01 \\ x00 \\ x00 \\ x17 \\ xc1 \\ x0c \\ x04 \\ x16 \\ n“]
In order to interpret/decode the ublox messages have two format types. 为了解释/解码ublox消息有两种格式类型。 Some of the messages are in NMEA format(basically comma delimited)
一些消息采用NMEA格式(基本以逗号分隔)
$MSG, 1, 2, 3, 4
Where the other messages are straight hexidecimal, where each byte or set of bytes represent some information 其他消息是正十六进制的,其中每个字节或一组字节代表一些信息
[AA BB CC DD EE]
So my question is: is there a way I can interpret/convert the data from serial connection to a readable or more usable format so I can actually work with the messages. 所以我的问题是:有没有一种方法可以将串行连接中的数据解释/转换为可读或更可用的格式,从而使我可以实际使用消息。 Like I said, I am new to python and more used to C++ style strings or array of characters
就像我说的那样,我是python的新手,并且更习惯于C ++样式字符串或字符数组
` `
A typical parsing task. 典型的解析任务。 In this case, it'll probably be the simplest to make tokenization two-stage:
在这种情况下,将令牌化分为两个阶段可能是最简单的:
re.split(", *",text)
re.split(", *",text)
Regarding why serial.Serial.readline
returns a list. 关于为什么
serial.Serial.readline
返回一个列表。 I consulted the sources - serial.Serial
delegates readline
to io.IOBase
, and its source indeed shows that it should return a bytestring
. 我查阅了sources-
serial.Serial
委托readline
到io.IOBase
, 它的来源确实表明它应该返回一个bytestring
。
So, the function might be overridden in your code by something. 因此,该函数可能会在代码中被某些东西覆盖。 Eg what do
print connection.readline
and print serial.Serial.readline
show? 例如,
print connection.readline
和print serial.Serial.readline
显示什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.