Python串行GPS-奇怪的行为

Python串行GPS-奇怪的行为,python,gps,serial-port,Python,Gps,Serial Port,我试图用python和GPS模块制作一个应用程序,但是,我得到的数据确实不一致。这是一个例子 [''] ['$GPGGA', '172154.000', '5048.7304', 'N', '00305.1466', 'E', '1', '6', '1.45', '20.5', 'M', '47.2', 'M', '', "*62\\r\\n'", " b'$GPGLL", '5048.7304', 'N', '00305.1466', 'E', '172154.000', 'A', "A*57

我试图用python和GPS模块制作一个应用程序,但是,我得到的数据确实不一致。这是一个例子

['']
['$GPGGA', '172154.000', '5048.7304', 'N', '00305.1466', 'E', '1', '6', '1.45', '20.5', 'M', '47.2', 'M', '', "*62\\r\\n'", " b'$GPGLL", '5048.7304', 'N', '00305.1466', 'E', '172154.000', 'A', "A*57\\r\\n'", " b'$GPGSA", 'A', '3', '18', '21', '29', '05', '20', '31', '', '', '', '', '', '', '1.68', '1.45', "0.84*05\\r\\n'", " b'$GPGSV", '3', '1', '10', '26', '78', '205', '15', '21', '73', '118', '14', '16', '62', '297', '', '27', '31', '271', "*74\\r\\n'", " b'$GPGSV", '3', '2', '10', '20', '30', '071', '32', '29', '25', '079', '29', '18', '19', '142', '20', '31', '15', '199', "18*7C\\r\\n'", " b'$GPGSV", '3', '3', '10', '05', '08', '027', '12', '07', '02', '338', "*7E\\r\\n'", " b'$GPRMC", '172154.000', 'A', '5048.7304', 'N', '00305.1466', 'E', '0.54', '60.66', '050517', '', '', "A*51\\r\\n'", " b'$GPVTG", '60.66', 'T', '', 'M', '0.54', 'N', '1.01', 'K', "A*0A\\r\\n'", " b'$GPGGA", '172155.0']
['0', '5048.7309', 'N', '00305.1460', 'E', '1', '6', '1.45', '20.4', 'M', '47.2', 'M', '', "*69\\r\\n'", " b'$GPGLL", '5048.7309', 'N', '00305.1460', 'E', '172155.000', 'A', "A*5D\\r\\n'", " b'$GPGSA", 'A', '3', '18', '21', '29', '05', '20', '31', '', '', '', '', '', '', '1.68', '1.45', "0.84*05\\r\\n'", " b'$GPGSV", '3', '1', '10', '26', '78', '204', '15', '21', '73', '118', '16', '16', '62', '297', '', '27', '31', '271', "*77\\r\\n'", " b'$GPGSV", '3', '2', '10', '20', '30', '071', '33', '29', '25', '079', '30', '18', '19', '142', '21', '31', '15', '199', "20*7F\\r\\n'", " b'$GPGSV", '3', '3', '10', '05', '08', '027', '12', '07', '02', '338', "*7E\\r\\n'", " b'$GPRMC", '172155.000', 'A', '5048.7309', 'N', '00305.1460', 'E', '0.42', '43.24', '050517', '', '', "A*5B\\r\\n'", " b'$GPVTG", '43.24', 'T', '', 'M', '0.42', 'N', '0.78', 'K', "A*05\\r\\n'", " b'$GPGGA", '172156.000', '5048.7313', 'N', '00305.1453', 'E', '1', '6', '1.45', '20.2', 'M', '47.2', 'M', '', "*67\\r\\n'", " b'$GPGLL", '5048.7313', 'N', '00305.1453', 'E', '172156.000', 'A', "A*55\\r\\n'", " b'$GPGSA", 'A', '3', '18', '21', '29', '05', '20', '31', '', '', '', '', '', '', '1.68', '1.45', "0.84*05\\r\\n'", " b'$GPGSV", '3', '1', '10', '26', '78', '204', '15', '21', '73', '118', '1']
['', '16', '62', '297', '', '27', '31', '271', "*79\\r\\n'", " b'$GPGSV", '3', '2', '10', '20', '30', '071', '34', '29', '25', '079', '30', '18', '19', '142', '22', '31', '15', '199', "20*7B\\r\\n'", " b'$GPGSV", '3', '3', '10', '05', '08', '027', '13', '07', '02', '338', "*7F\\r\\n'", " b'$GPRMC", '172156.000', 'A', '5048.7313', 'N', '00305.1453', 'E', '0.32', '16.16', '050517', '', '', "A*55\\r\\n'", " b'$GPVTG", '16.16', 'T', '', 'M', '0.32', 'N', '0.59', 'K', "A*00\\r\\n'", " b'$GPGGA", '172157.000', '5048.7315', 'N', '00305.1445', 'E', '1', '6', '1.45', '20.1', 'M', '47.2', 'M', '', "*64\\r\\n'", " b'$GPGLL", '5048.7315', 'N', '00305.1445', 'E', '172157.000', 'A', "A*55\\r\\n'", " b'$GPGSA", 'A', '3', '18', '21', '29', '05', '20', '31', '', '', '', '', '', '', '1.68', '1.45', "0.84*05\\r\\n'", " b'$GPGSV", '3', '1', '10', '26', '78', '204', '14', '21', '73', '118', '17', '16', '62', '297', '', '27', '31', '271', "*77\\r\\n'", " b'$GPGSV", '3', '2', '10', '20', '30', '071', '34', '29', '25', '079', '30', '18', '19', '142', '22', '31', '15', '199', "21*7A\\r\\n'", " b'$GPGSV", '3', '3', '10', '05', '08', '027', '13', '07', '02', '338', "*7F\\r\\n'", " b'$GPRMC", '172157.000', 'A', '5048.7315', 'N', '00305.1445', 'E', '0.31', '347.38', '050517', '', '', "A*6D\\r\\n'", " b'$GPVT'", " b'G", '347.3']
['', 'T', '', 'M', '0.31', 'N', '0.57', 'K', "A*36\\r\\n'", " b'$GPGGA", '172158.000', '5048.7316', 'N', '00305.1445', 'E', '1', '5', '2.49', '20.1', 'M', '47.2', 'M', '', "*64\\r\\n'", " b'$GPGLL", '5048.7316', 'N', '00305.1445', 'E', '172158.000', 'A', "A*59\\r\\n'", " b'$GPGSA", 'A', '3', '18', '21', '29', '20', '31', '', '', '', '', '', '', '', '3.35', '2.49', "2.25*0C\\r\\n'", " b'$GPGSV", '3', '1', '10', '26', '78', '204', '14', '21', '73', '118', '17', '16', '62', '297', '', '27', '31', '271', "*77\\r\\n'", " b'$GPGSV", '3', '2', '10', '20', '30', '071', '34', '29', '24', '079', '30', '18', '19', '142', '22', '31', '15', '199', "21*7B\\r\\n'", " b'$GPGSV", '3', '3', '10', '05', '08', '027', '', '07', '02', '338', "*7D\\r\\n'", " b'$GPRMC", '172158.000', 'A', '5048.7316', 'N', '00305.1445', 'E', '0.34', '332.33', '050517', '', '', "A*6D\\r\\n'", " b'$GPVTG", '332.33', 'T', '', 'M', '0.34', 'N', '0.62', 'K', 'A*3C\\r\\']
['$GPGGA', '172159.000', '5048.7316', 'N', '00305.1444', 'E', '1', '4', '2.96', '20.1', 'M', '47.2', 'M', '', "*67\\r\\n'", " b'$GPGLL", '5048.7316', 'N', '00305.1444', 'E', '172159.000', 'A', "A*59\\r\\n'", " b'$GPGSA", 'A', '3', '18', '21', '29', '20', '', '', '', '', '', '', '', '', '3.12', '2.96', "1.00*0D\\r\\n'", " b'$GPGSV", '3', '1', '10', '26', '78', '204', '14', '21', '73', '118', '16', '16', '62', '297', '', '27', '31', '271', "*76\\r\\n'", " b'$GPGSV", '3', '2', '10', '20', '30', '071', '34', '29', '24', '079', '30', '18', '19', '142', '22', '31', '15', '199', "*78\\r\\n'", " b'$GPGSV", '3', '3', '10', '05', '08', '027', '', '07', '02', '338', "*7D\\r\\n'", " b'$GPRMC", '172159.000', 'A', '5048.7316', 'N', '00305.1444', 'E', '0.24', '333.32', '050517', '', '', "A*6C\\r\\n'", " b'$GPVTG", '333.32', 'T', '', 'M', '0.24', 'N', '0.45', 'K', 'A*38\\r\\']
['$GPGGA', '172200.000', '5048.7317', 'N', '00305.1444', 'E', '1', '4', '2.96', '20.1', 'M', '47.2', 'M', '', "*69\\r\\n'", " b'$GPGLL", '5048.7317', 'N', '00305.1444', 'E', '172200.000', 'A', "A*57\\r\\n'", " b'$GPGSA", 'A', '3', '18', '21', '29', '20', '', '', '', '', '', '', '', '', '3.12', '2.96', "1.00*0D\\r\\n'", " b'$GPGSV", '3', '1', '10', '26', '78', '204', '14', '21', '73', '118', '16', '16', '62', '297', '', '27', '31', '271', "*76\\r\\n'", " b'$GPGSV", '3', '2', '10', '20', '30', '071', '34', '29', '24', '079', '30', '18', '19', '142', '22', '31', '14', '199', "*79\\r\\n'", " b'$GPGSV", '3', '3', '10', '05', '08', '027', '', '07', '02', '338', "*7D\\r\\n'", " b'$GPRMC", '172200.000', 'A', '5048.7317', 'N', '00305.1444', 'E', '0.19', '337.40', '050517', '', '', "A*6D\\r\\n'", " b'$GPVTG", '337.40', 'T', '', 'M', '0.19', 'N', '0.35', 'K', 'A*30\\r\\']
['$GPGGA', '172201.000', '5048.7318', 'N', '00305.1443', 'E', '1', '4', '2.96', '20.1', 'M', '47.2', 'M', '', "*60\\r\\n'", " b'$GPGLL", '5048.7318', 'N', '00305.1443', 'E', '172201.000', 'A', "A*5E\\r\\n'", " b'$GPGSA", 'A', '3', '18', '21', '29', '20', '', '', '', '', '', '', '', '', '3.12', '2.96', "1.00*0D\\r\\n'", " b'$GPGSV", '3', '1', '10', '26', '78', '204', '13', '21', '73', '118', '16', '16', '62', '297', '', '27', '31', '271', "*71\\r\\n'", " b'$GPGSV", '3', '2', '10', '20', '30', '071', '34', '29', '24', '079', '30', '18', '19', '142', '22', '31', '14', '199', "*79\\r\\n'", " b'$GPGSV", '3', '3', '10', '05', '08', '027', '', '07', '02', '338', "*7D\\r\\n'", " b'$GPRMC", '172201.000', 'A', '5048.7318', 'N', '00305.1443', 'E', '0.19', '344.62', '050517', '', '', "A*60\\r\\n'", " b'$GPVTG", '344.62', 'T', '', 'M', '0.19', 'N', '0.36', 'K', "A*37\\r\\n'", " b'$GPGGA", '172202.000', '5048.7318', 'N', '00305.1443', 'E', '1', '5', '2.18', '20.1', 'M', '47.2', 'M', '', "*64\\r\\n'", " b'$GPGLL", '5048.7318', 'N', '00305.1443', 'E', '172202.']
['00', 'A', "A*5D\\r\\n'", " b'$GPGSA", 'A', '3', '18', '21', '29', '20', '31', '', '', '', '', '', '', '', '2.36', '2.18', "0.91*07\\r\\n'", " b'$GPGSV", '3', '1', '10', '26', '78', '204', '12', '21', '73', '118', '16', '16', '62', '297', '', '27', '31', '271', "*70\\r\\n'", " b'$GPGSV", '3', '2', '10', '20', '30', '071', '34', '29', '24', '079', '30', '18', '19', '142', '22', '31', '14', '199', "20*7B\\r\\n'", " b'$GPGSV", '3', '3', '10', '05', '08', '027', '', '07', '02', '338', "*7D\\r\\n'", " b'$GPRMC", '172202.000', 'A', '5048.7318', 'N', '00305.1443', 'E', '0.20', '7.43', '050517', '', '', "A*6E\\r\\n'", " b'$GPVTG", '7.43', 'T', '', 'M', '0.20', 'N', '0.36', 'K', "A*3A\\r\\n'", " b'$GPGGA", '172203.000', '5048.7319', 'N', '00305.1443', 'E', '1', '4', '28.94', '20.1', 'M', '47.2', 'M', '', "*59\\r\\n'", " b'$GPGLL", '5048.7319', 'N', '00305.1443', 'E', '172203.000', 'A', "A*5D\\r\\n'", " b'$GPGSA", 'A', '3', '18', '21', '29', '20', '', '', '', '', '', '', '', '', '37.79', '28.94', "24.30*39\\r\\n'", " b'$GPGSV", '3', '1', '10', '26', '78', '204', '12', '21', '73', '118', '16', '16', '62', '297', '', '27', '31', '271', "*70\\r\\n'", " b'$GPGSV", '3', '2', '10', '20', '30', '071', '35', '29', '24', '079', '30', '18', '19', '142', '22', '31', '14', '199', "*78\\r\\n'", " b'$GPGSV", '3', '3', '10', '05']
我正在使用的代码

import serial
import time
import pynmea2

#########################################
# This is the main code to setup the
# serial connection with the GPS module.
# it needs to be OR runt as root OR as
# pi with all the root rights.
#########################################
port = "/dev/ttyAMA0"
ser = serial.Serial(port, 9600, timeout=0)

#########################################
# These are all the global variables
# to be used. All defined and set to
# zero or their standard 'Null' value.
#########################################
lat = 0.0
lon = 0.0
cur_speed = 0.0


while True:

    try:
        # Get the data from the serial monitor.
        data = str(ser.readlines()).lstrip("[b'")[:-3].split(",")
        print(data)

        # Wait a bit to not overload the Serial port
        time.sleep(0.95)

    ############################################################
    # The error handling
    ############################################################
    except serial.serialutil.SerialException:
        ser.close()
        port = "/dev/ttyAMA0"
        ser = serial.Serial(port, 9600, timeout=0)
        continue
    except pynmea2.ParseError:
        # print("Error on parsing object, continuing...")
        continue
    except BlockingIOError:
        # print("Blocking I/O error, continuing...")
        continue
    except TypeError:
        # print("Type error, continuing...")
        continue
    except IndexError:
        # print("Python fucked up...")
        continue
    except KeyboardInterrupt:
        print("\nProgram stopped.")
        exit()
如你所见,我用的是串行设备。。。我只需要一些数据,但我无法访问它,它只是坏了


如果我尝试使用解析数据的pynmea模块,它也不起作用。。。请帮帮我。

所以,我自己把它修好了。现在,我改变了一些事情

from model.DB import DB
import serial
import time
import datetime
import pynmea2

#########################################
# This is the main code to setup the
# serial connection with the GPS module.
# it needs to be OR runt as root OR as
# pi with all the root rights.
#########################################
port = "/dev/ttyAMA0"
baudrate = 9600
ser = serial.Serial(port, baudrate, timeout=0)

#########################################
# These are all the global variables
# to be used. All defined and set to
# zero or their standard 'Null' value.
#########################################
lat = 0.0
lon = 0.0
cur_speed = 0.0
data = None


while True:

    try:
        # Get the data from the serial monitor.
        raw_data = str(ser.readlines()).lstrip("[b'")[:-3].split(",")
        # print(data)

        if raw_data[0] == "$GPGGA":
            # Set the raw data to correct data to work with
            data = raw_data
            # print(data)

            lat = data[2]
            lon = data[4]
            speed_index = (data.index(" b'$GPVTG") + 7)
            cur_speed = data[speed_index]
            # print(len(data))
            # print(cur_speed)

            if float(cur_speed) > 5.0 and float(cur_speed) < 130.0:
                db = DB()
                db.insert_coordinates(lat, lon, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
                # print("Data submitted to the database.")
                # print(data)
                # print(cur_speed)

        # Wait a bit to not overload the Serial port
        time.sleep(0.90)

    ############################################################
    # The error handling
    ############################################################
    except serial.serialutil.SerialException:
        ser.close()
        port = "/dev/ttyAMA0"
        ser = serial.Serial(port, 9600, timeout=0)
        continue
    except pynmea2.ParseError:
        # print("Error on parsing object, continuing...")
        continue
    except BlockingIOError:
        # print("Blocking I/O error, continuing...")
        continue
    except TypeError:
        # print("Type error, continuing...")
        continue
    except IndexError:
        # print("I ran too far with the list :/")
        continue
    except ValueError:
        # print("Could not convert string to float...")
        continue
    except KeyboardInterrupt:
        print("\nProgram stopped.")
        exit()
从model.DB导入DB
导入序列号
导入时间
导入日期时间
进口pynmea2
#########################################
#这是设置
#与GPS模块的串行连接。
#它需要是或runt作为root或as
#具有所有根权限的pi。
#########################################
port=“/dev/ttyam0”
波特率=9600
ser=串行。串行(端口、波特率、超时=0)
#########################################
#这些都是全局变量
#待使用。所有已定义并设置为
#零或其标准“Null”值。
#########################################
lat=0.0
lon=0.0
电流速度=0.0
数据=无
尽管如此:
尝试:
#从串行监视器获取数据。
原始数据=str(ser.readlines()).lstrip(“[b'”)[:-3]。拆分(“,”)
#打印(数据)
如果原始数据[0]=“$GPGGA”:
#将原始数据设置为要使用的正确数据
数据=原始数据
#打印(数据)
lat=数据[2]
lon=数据[4]
速度指数=(数据指数(“b'$GPVTG”)+7)
当前速度=数据[速度索引]
#打印(透镜(数据))
#打印(当前速度)
如果浮动(电流速度)>5.0且浮动(电流速度)<130.0:
db=db()
db.insert_坐标(lat、lon、datetime.datetime.now().strftime(“%Y-%m-%d%H:%m:%S”))
#打印(“提交到数据库的数据”)
#打印(数据)
#打印(当前速度)
#请稍等,以免使串行端口过载
睡眠时间(0.90)
############################################################
#错误处理
############################################################
除serial.serialutil.SerialException外:
塞尔克洛斯()
port=“/dev/ttyam0”
ser=serial.serial(端口,9600,超时=0)
持续
除pynmea2.ParseError外:
#打印(“分析对象时出错,正在继续…”)
持续
除BlockingIOError外:
#打印(“阻止I/O错误,继续…”)
持续
除类型错误外:
#打印(“类型错误,继续…”)
持续
除索引器外:
#打印(“我用列表跑得太远了:/”)
持续
除值错误外:
#打印(“无法将字符串转换为浮点…”)
持续
除键盘中断外:
打印(“\n程序已停止。”)
退出()
是我现在使用的代码:)