pythonraspberry-pigpsdecode

python decoding bytes from a GPS Module GY-NEO6MV2 connected to raspberry pi


I have a GPS Module GY-NEO6MV2 connected to a raspberry pi 3. When I use the gpsmon command I get the data perfectly the output of gpsmon but I'm trying to use python to read the data from the serial.

This is my code

import serial
ser = serial.Serial('/dev/ttyACM0')
ser.flushInput()

while True:
    try:
        ser_bytes = ser.readline()
        decoded_bytes = ser_bytes
        print(decoded_bytes)
    except:
        break

output

b'\xb5b\x01\x064\x00\x00\xda@\x12T\xe9\x05\x00\xb3\x08\x03\xdd/\xb6l\x1b\xd0\xb6P\x16\xfc\x9a\xec\r\xb1\x05\x00\x00\x16\x00\x00\x00\xf0\xff\xff\xff\x04\x00\x00\x00\\\x00\x00\x00\xcb\x00\x03\x08"g\x01\x00\xa4\x84\xb5b\x010\xb0\x00\x00\xda@\x12\x0e\x02\x00\x00\x00\x01\r\x04\x12\x1d\xb7\x00K\x00\x00\x00\x05\x03\x04\x04\x10?\\\x00\x00\x00\x00\x00\x03\x04\r\x04\r.\t\x00\x19\x00\x00\x00\x01\x06\r\x05\x1f\x127\x01~\xfe\xff\xff\x0f\x07\r\x07%,\xe5\x00m\x00\x00\x00\t\t\r\x05\x1d(=\x016\x03\x00\x00\n'
b'T\x00\x00\x00\x00\x00\x07\x15\r\x04\x10\x0c\xab\x00k\x07\x00\x00\x0e\x1a\r\x04\x16\x119\x00\n'
b'\x10\x01\x00\xa5\x00\x00\x00\x00\x00\x00\x08\x1e\r\x04\x1c\n'
b'\xdf\x00{\xf9\xff\xff\x0cx\x10\x01\x00\x19\xff\x00\x00\x00\x00\x00\x04|\x10\x01\x00:\xdc\x00\x00\x00\x00\x00\x0b~\x10\x01\x00<\xd6\x00\x00\x00\x00\x00`\xc6\xb5b\x01\x04\x12\x00\x00\xda@\x12\xde\x00\xcb\x00X\x00\xb6\x00[\x007\x00H\x00\xd4\xb0\xb5b\x01 \x10\x00\x00\xda@\x12T\xe9\x05\x00\xb3\x08\x12\x07\x16\x00\x00\x00\x89\xa0'
b'\xb5b\x01\x064\x00\xe8\xdd@\x12U\t\x06\x00\xb3\x08\x03\xddY\xb6l\x1b\xce\xb6P\x16\x07\x9b\xec\r\xc1\x05\x00\x00\x11\x00\x00\x00\xf8\xff\xff\xff\x01\x00\x00\x00`\x00\x00\x00\xdc\x00\x03\x08"g\x01\x00\n'
b'\xb0\x00\xe8\xdd@\x12\x0e\x02\x00\x00\x00\x01\r\x04\x13\x1d\xb7\x00\xb2\x00\x00\x00\x05\x03\x04\x04\x10?\\\x00\x00\x00\x00\x00\x03\x04\r\x04\x0e.\t\x00\xa2\x00\x00\x00\x01\x06\r\x06 \x127\x01u\xfe\xff\xff\x0f\x07\r\x07&,\xe5\x00\x80\x00\x00\x00\t\t\r\x05\x1d(=\x01=\x03\x00\x00\n'

I tried using

decoded_bytes = ser_bytes.decode('utf-8')

I get this error

Traceback (most recent call last):
  File "/test10.py", line 8, in <module>
    decoded_bytes = ser_bytes.decode("utf-8")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb5 in position 0: invalid start byte

and tried using the unicode_escape

decoded_bytes = ser_bytes.decode('unicode_escape')

and got this rubbish output

òj:2�*rþÿÿ
xÿ|:Ü
´("gæµb0°`ÉUxÅUýédÔ_BD¾Éµb xÅUç�Dwµb4`ÉUw·Ý�¼l▒¿P°¢ì
9q
*IK;þÿÿ

CðÿÿYPuTTY
▒
%åYþÿÿ
xÿ|:Ü

I'm tring to read the line

Thank you


Solution

  • The data is packed info byte-array to unpack data use the Python struct library.

    >>> import struct
    >>> data =b'\x10\x01\x00\xa5\x00\x00\x00\x00\x00\x00\x08\x1e\r\x04\x1c\n'
    >>> struct.unpack("4f", data)
    (-1.1102590235254148e-16, 0.0, 7.199780051661553e-21, 7.511888650386347e-33)