python-3.x NMEA消息不准确[重复]

bfrts1fy  于 2022-11-19  发布在  Python
关注(0)|答案(1)|浏览(216)

此问题在此处已有答案

Getting different GPS coordinates in different environments(2个答案)
2天前关闭。
当我在u-center中查看纬度和经度时,我得到了非常准确的坐标,但是u-center的文本控制台中的NMEA消息,以及我在VSCode中的Python脚本输出,都相差了大约0.3度。
下面是我的代码:

import serial

gps = serial.Serial('com5', baudrate=9600)

while True:
    ser_bytes = gps.readline()
    decoded_bytes = ser_bytes.decode("utf-8")
    data = decoded_bytes.split(",")
    if data[0] == '$GNRMC':
        lat_nmea = (data[3],data[4])
        lat_degrees = float(lat_nmea[0][0:2])
        if lat_nmea[1] == 'S':
            lat_degrees = -lat_degrees
        lat_minutes = float(lat_nmea[0][2:])
        lat = lat_degrees + (lat_minutes/60)
        lon_nmea = (data[5],data[6])
        lon_degrees = float(lon_nmea[0][:3])
        if lon_nmea[1] == 'W':
            lon_degrees = -lon_degrees
        lon_minutes = float(lon_nmea[0][3:])
        lon = lon_degrees + (lon_minutes/60)
        print("%0.8f" %lat,", " "%0.8f" %lon)

预期输出(如u-center数据视图所示):
-12.63900217、111.85371867美元
实际输出(奇怪的是,似乎经度按预期打印,但纬度未按预期打印):
-11.36120217、111.85371867等单位
但是,NMEA消息与上述值都不匹配。例如,$GNGLL消息:


任何帮助都将不胜感激!谢谢:)

ql3eal8s

ql3eal8s1#

感谢注解中的@GiacomoCatenazzi,下面是按预期打印值的代码:

import serial

gps = serial.Serial('com5', baudrate=9600)

while True:
    ser_bytes = gps.readline()
    decoded_bytes = ser_bytes.decode("utf-8")
    data = decoded_bytes.split(",")
    if data[0] == '$GNRMC':
        lat_nmea = (data[3],data[4])
        lat_degrees = float(lat_nmea[0][0:2])
        lat_minutes = float(lat_nmea[0][2:])
        lat = lat_degrees + (lat_minutes/60)
        lon_nmea = (data[5],data[6])
        lon_degrees = float(lon_nmea[0][:3])
        lon_minutes = float(lon_nmea[0][3:])
        lon = lon_degrees + (lon_minutes/60)
        if lat_nmea[1] == 'S':
            lat = -lat
        if lon_nmea[1] == 'W':
            lon = -lon
        print("%0.8f" %lat, "%0.8f" %lon)

相关问题