我有一个连接到树莓派 3 的 GPS 模块 GY-NEO6MV2。 当我使用 gpsmon 命令时,我完美地获取了数据 但我正在尝试使用 python 从串口读取数据。
这是我的代码
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
输出
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'
我尝试使用
decoded_bytes = ser_bytes.decode('utf-8')
我收到此错误
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
并尝试使用 unicode_escape
decoded_bytes = ser_bytes.decode('unicode_escape')
并得到了这些垃圾输出
òj:2�*rþÿÿ
xÿ|:Ü
´("gæµb0°`ÉUxÅUýédÔ_BD¾Éµb xÅUç�Dwµb4`ÉUw·Ý�¼l▒¿P°¢ì
9q
*IK;þÿÿ
CðÿÿYPuTTY
▒
%åYþÿÿ
xÿ|:Ü
我正在尝试阅读这行内容
谢谢你
数据被打包为信息字节数组,以使用 Python 结构库解包数据。
>>> 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)
我为这个gps模块制作了一个python程序: https://github.com/framboise-pi/Pi-GPS--Raspberry-Tkinter-GPS6MV2-module
此版本使用 Tkinter(Python 的图形窗口系统) 希望有人会觉得它很好:)
Framboise-pi