简体   繁体   中英

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.

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']

["\\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. Some of the messages are in NMEA format(basically comma delimited)

$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

`

A typical parsing task. In this case, it'll probably be the simplest to make tokenization two-stage:

  1. read the data until you run into a message boundary (you didn't give enough info on how to recognize it)
  2. split the read message into its meaningful parts
  3. display the parts however you want

Regarding why serial.Serial.readline returns a list. I consulted the sources - serial.Serial delegates readline to io.IOBase , and its source indeed shows that it should return a bytestring .

So, the function might be overridden in your code by something. Eg what do print connection.readline and print serial.Serial.readline show?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM