2017-05-05 84 views
0

我想用python和GPS模塊做一個應用程序,但是,我得到的數據真的不一致。這是一個例子;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'] 

我正在使用的代碼;因此,正如你所看到的,我使用的是串口設備......我只需要一些數據,但是我無法訪問它,它只是發生故障。

如果我嘗試使用解析數據的pynmea模塊,它也不起作用...請幫助我。

回答

0

所以,我已經自己修復了。現在,我改變了一些東西。

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() 

是我現在使用:)

代碼