def test_read_data_obj(self): test_file = os.path.join(os.path.dirname(__file__), 'test_data', 'test_data_small.gps') with open(test_file, 'r') as test_file_fd: streamer = NMEAStream(stream_obj=test_file_fd) next_data = streamer.get_objects() nmea_objects = [] while next_data: nmea_objects += next_data next_data = streamer.get_objects() expected_object_types = ['RMC', 'GGA', 'RMC', 'GGA', 'GLL', 'VTG', 'RMC', 'GGA', 'GLL', 'VTG'] self.assertEqual(expected_object_types[0], nmea_objects[0].sen_type) self.assertEqual(expected_object_types[1], nmea_objects[1].sen_type) self.assertEqual(expected_object_types[2], nmea_objects[2].sen_type) self.assertEqual(expected_object_types[3], nmea_objects[3].sen_type) self.assertEqual(expected_object_types[4], nmea_objects[4].sen_type) self.assertEqual(expected_object_types[5], nmea_objects[5].sen_type) self.assertEqual(expected_object_types[6], nmea_objects[6].sen_type) self.assertEqual(expected_object_types[7], nmea_objects[7].sen_type) self.assertEqual(expected_object_types[8], nmea_objects[8].sen_type) self.assertEqual(expected_object_types[9], nmea_objects[9].sen_type)
def test_read_data_str_raw(self): data = """$GPRMC,184332.07,A,1929.459,S,02410.381,E,74.00,16.78,210410,0.0,E,A*2B $GPGGA,184333.07,1929.439,S,02410.387,E,1,04,2.8,100.00,M,-33.9,M,,0000*65 $GPRMC,184444.08,A,1928.041,S,02410.809,E,74.00,16.78,210410,0.0,E,A*25 $GPGGA,184445.08,1928.021,S,02410.814,E,1,04,2.7,100.00,M,-33.9,M,,0000*6E $GPGLL,1928.001,S,02410.820,E,184446.08,A,A*79 $GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36 $GPRMC,184448.08,A,1927.962,S,02410.832,E,74.00,16.78,210410,0.0,E,A*26 $GPGGA,184449.08,1927.942,S,02410.838,E,1,04,1.7,100.00,M,-33.9,M,,0000*6C $GPGLL,1927.922,S,02410.844,E,184450.08,A,A*7B $GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36 """ streamer = NMEAStream() nmea_objects = streamer.get_strings(data=data) nmea_objects += streamer.get_strings(data='') expected_result = [ 'GPRMC,184332.07,A,1929.459,S,02410.381,E,74.00,16.78,210410,0.0,E,A*2B', 'GPGGA,184333.07,1929.439,S,02410.387,E,1,04,2.8,100.00,M,-33.9,M,,0000*65', 'GPRMC,184444.08,A,1928.041,S,02410.809,E,74.00,16.78,210410,0.0,E,A*25', 'GPGGA,184445.08,1928.021,S,02410.814,E,1,04,2.7,100.00,M,-33.9,M,,0000*6E', 'GPGLL,1928.001,S,02410.820,E,184446.08,A,A*79', 'GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36', 'GPRMC,184448.08,A,1927.962,S,02410.832,E,74.00,16.78,210410,0.0,E,A*26', 'GPGGA,184449.08,1927.942,S,02410.838,E,1,04,1.7,100.00,M,-33.9,M,,0000*6C', 'GPGLL,1927.922,S,02410.844,E,184450.08,A,A*7B', 'GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36' ] self.assertEqual(expected_result, nmea_objects)
def test__read_data(self): expected_result = ['GPRMC,184332.07,A,1929.459,S,02410.381,E,74.00,16.78,210410,0.0,E*46', 'GPGGA,184333.07,1929.439,S,02410.387,E,1,04,2.8,100.00,M,-33.9,M,,0000*65', 'GPRMC,184444.08,A,1928.041,S,02410.809,E,74.00,16.78,210410,0.0,E*48', 'GPGGA,184445.08,1928.021,S,02410.814,E,1,04,2.7,100.00,M,-33.9,M,,0000*6E', 'GPGLL,1928.001,S,02410.820,E,184446.08,A,A*79', 'GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36', 'GPRMC,184448.08,A,1927.962,S,02410.832,E,74.00,16.78,210410,0.0,E*4B', 'GPGGA,184449.08,1927.942,S,02410.838,E,1,04,1.7,100.00,M,-33.9,M,,0000*6C', 'GPGLL,1927.922,S,02410.844,E,184450.08,A,A*7B', 'GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36'] input_data = """$GPRMC,184332.07,A,1929.459,S,02410.381,E,74.00,16.78,210410,0.0,E*46 $GPGGA,184333.07,1929.439,S,02410.387,E,1,04,2.8,100.00,M,-33.9,M,,0000*65 $GPRMC,184444.08,A,1928.041,S,02410.809,E,74.00,16.78,210410,0.0,E*48 $GPGGA,184445.08,1928.021,S,02410.814,E,1,04,2.7,100.00,M,-33.9,M,,0000*6E $GPGLL,1928.001,S,02410.820,E,184446.08,A,A*79 $GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36 $GPRMC,184448.08,A,1927.962,S,02410.832,E,74.00,16.78,210410,0.0,E*4B $GPGGA,184449.08,1927.942,S,02410.838,E,1,04,1.7,100.00,M,-33.9,M,,0000*6C $GPGLL,1927.922,S,02410.844,E,184450.08,A,A*7B $GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36 """ streamer = NMEAStream() data = streamer._read(data=input_data) data += streamer._read(data='') self.assertEqual(data, expected_result)
def test_read_data_obj(self): test_file = os.path.join(os.path.dirname(__file__), 'test_data', 'test_data_small.gps') with open(test_file, 'r') as test_file_fd: streamer = NMEAStream(stream_obj=test_file_fd) next_data = streamer.get_objects() nmea_objects = [] while next_data: nmea_objects += next_data next_data = streamer.get_objects() expected_object_types = [ 'GPRMC', 'GPGGA', 'GPRMC', 'GPGGA', 'GPGLL', 'GPVTG', 'GPRMC', 'GPGGA', 'GPGLL', 'GPVTG' ] self.assertEqual(expected_object_types[0], nmea_objects[0].sen_type) self.assertEqual(expected_object_types[1], nmea_objects[1].sen_type) self.assertEqual(expected_object_types[2], nmea_objects[2].sen_type) self.assertEqual(expected_object_types[3], nmea_objects[3].sen_type) self.assertEqual(expected_object_types[4], nmea_objects[4].sen_type) self.assertEqual(expected_object_types[5], nmea_objects[5].sen_type) self.assertEqual(expected_object_types[6], nmea_objects[6].sen_type) self.assertEqual(expected_object_types[7], nmea_objects[7].sen_type) self.assertEqual(expected_object_types[8], nmea_objects[8].sen_type) self.assertEqual(expected_object_types[9], nmea_objects[9].sen_type)
def nmea_parse(infile): ''' nmea_parse() - Read a GIS waypoint file and write lat-lon values Parameters ---------- infile : str waypoint file name Returns ------- nothing ''' from pynmea.streamer import NMEAStream nmea_file = open(infile) nmea_stream = NMEAStream(stream_obj=nmea_file) next_data = nmea_stream.get_objects() nmea_objects = [] while next_data: nmea_objects += next_data next_data = nmea_stream.get_objects() for nmea_obj in nmea_objects: if hasattr(nmea_obj, 'lat'): print(f' Lat/Lon: ({nmea_obj.lat}, {nmea_obj.lon})') return
def test_read_data_str_raw(self): data = """$GPRMC,184332.07,A,1929.459,S,02410.381,E,74.00,16.78,210410,0.0,E*46 $GPGGA,184333.07,1929.439,S,02410.387,E,1,04,2.8,100.00,M,-33.9,M,,0000*65 $GPRMC,184444.08,A,1928.041,S,02410.809,E,74.00,16.78,210410,0.0,E*48 $GPGGA,184445.08,1928.021,S,02410.814,E,1,04,2.7,100.00,M,-33.9,M,,0000*6E $GPGLL,1928.001,S,02410.820,E,184446.08,A,A*79 $GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36 $GPRMC,184448.08,A,1927.962,S,02410.832,E,74.00,16.78,210410,0.0,E*4B $GPGGA,184449.08,1927.942,S,02410.838,E,1,04,1.7,100.00,M,-33.9,M,,0000*6C $GPGLL,1927.922,S,02410.844,E,184450.08,A,A*7B $GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36 """ streamer = NMEAStream() nmea_objects = streamer.get_strings(data=data) nmea_objects += streamer.get_strings(data='') expected_result = ['GPRMC,184332.07,A,1929.459,S,02410.381,E,74.00,16.78,210410,0.0,E*46', 'GPGGA,184333.07,1929.439,S,02410.387,E,1,04,2.8,100.00,M,-33.9,M,,0000*65', 'GPRMC,184444.08,A,1928.041,S,02410.809,E,74.00,16.78,210410,0.0,E*48', 'GPGGA,184445.08,1928.021,S,02410.814,E,1,04,2.7,100.00,M,-33.9,M,,0000*6E', 'GPGLL,1928.001,S,02410.820,E,184446.08,A,A*79', 'GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36', 'GPRMC,184448.08,A,1927.962,S,02410.832,E,74.00,16.78,210410,0.0,E*4B', 'GPGGA,184449.08,1927.942,S,02410.838,E,1,04,1.7,100.00,M,-33.9,M,,0000*6C', 'GPGLL,1927.922,S,02410.844,E,184450.08,A,A*7B', 'GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36'] self.assertEqual(expected_result, nmea_objects)
def test_read_data_obj_raw(self): data = """$GPRMC,184332.07,A,1929.459,S,02410.381,E,74.00,16.78,210410,0.0,E*46 $GPGGA,184333.07,1929.439,S,02410.387,E,1,04,2.8,100.00,M,-33.9,M,,0000*65 $GPRMC,184444.08,A,1928.041,S,02410.809,E,74.00,16.78,210410,0.0,E*48 $GPGGA,184445.08,1928.021,S,02410.814,E,1,04,2.7,100.00,M,-33.9,M,,0000*6E $GPGLL,1928.001,S,02410.820,E,184446.08,A,A*79 $GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36 $GPRMC,184448.08,A,1927.962,S,02410.832,E,74.00,16.78,210410,0.0,E*4B $GPGGA,184449.08,1927.942,S,02410.838,E,1,04,1.7,100.00,M,-33.9,M,,0000*6C $GPGLL,1927.922,S,02410.844,E,184450.08,A,A*7B $GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36 """ streamer = NMEAStream() nmea_objects = streamer.get_objects(data=data) nmea_objects += streamer.get_objects(data='') expected_object_types = ['RMC', 'GGA', 'RMC', 'GGA', 'GLL', 'VTG', 'RMC', 'GGA', 'GLL', 'VTG'] self.assertEqual(expected_object_types[0], nmea_objects[0].sen_type) self.assertEqual(expected_object_types[1], nmea_objects[1].sen_type) self.assertEqual(expected_object_types[2], nmea_objects[2].sen_type) self.assertEqual(expected_object_types[3], nmea_objects[3].sen_type) self.assertEqual(expected_object_types[4], nmea_objects[4].sen_type) self.assertEqual(expected_object_types[5], nmea_objects[5].sen_type) self.assertEqual(expected_object_types[6], nmea_objects[6].sen_type) self.assertEqual(expected_object_types[7], nmea_objects[7].sen_type) self.assertEqual(expected_object_types[8], nmea_objects[8].sen_type) self.assertEqual(expected_object_types[9], nmea_objects[9].sen_type)
def test__read_data(self): expected_result = [ 'GPRMC,184332.07,A,1929.459,S,02410.381,E,74.00,16.78,210410,0.0,E,A*2B', 'GPGGA,184333.07,1929.439,S,02410.387,E,1,04,2.8,100.00,M,-33.9,M,,0000*65', 'GPRMC,184444.08,A,1928.041,S,02410.809,E,74.00,16.78,210410,0.0,E,A*25', 'GPGGA,184445.08,1928.021,S,02410.814,E,1,04,2.7,100.00,M,-33.9,M,,0000*6E', 'GPGLL,1928.001,S,02410.820,E,184446.08,A,A*79', 'GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36', 'GPRMC,184448.08,A,1927.962,S,02410.832,E,74.00,16.78,210410,0.0,E,A*26', 'GPGGA,184449.08,1927.942,S,02410.838,E,1,04,1.7,100.00,M,-33.9,M,,0000*6C', 'GPGLL,1927.922,S,02410.844,E,184450.08,A,A*7B', 'GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36' ] input_data = """$GPRMC,184332.07,A,1929.459,S,02410.381,E,74.00,16.78,210410,0.0,E,A*2B $GPGGA,184333.07,1929.439,S,02410.387,E,1,04,2.8,100.00,M,-33.9,M,,0000*65 $GPRMC,184444.08,A,1928.041,S,02410.809,E,74.00,16.78,210410,0.0,E,A*25 $GPGGA,184445.08,1928.021,S,02410.814,E,1,04,2.7,100.00,M,-33.9,M,,0000*6E $GPGLL,1928.001,S,02410.820,E,184446.08,A,A*79 $GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36 $GPRMC,184448.08,A,1927.962,S,02410.832,E,74.00,16.78,210410,0.0,E,A*26 $GPGGA,184449.08,1927.942,S,02410.838,E,1,04,1.7,100.00,M,-33.9,M,,0000*6C $GPGLL,1927.922,S,02410.844,E,184450.08,A,A*7B $GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36 """ streamer = NMEAStream() data = streamer._read(data=input_data) data += streamer._read(data='') self.assertEqual(data, expected_result)
def test__read(self): test_file = os.path.join(os.path.dirname(__file__), 'test_data', 'test_data_small.gps') expected_result = [ 'GPRMC,184332.07,A,1929.459,S,02410.381,E,74.00,16.78,210410,0.0,E,A*2B', 'GPGGA,184333.07,1929.439,S,02410.387,E,1,04,2.8,100.00,M,-33.9,M,,0000*65', 'GPRMC,184444.08,A,1928.041,S,02410.809,E,74.00,16.78,210410,0.0,E,A*25', 'GPGGA,184445.08,1928.021,S,02410.814,E,1,04,2.7,100.00,M,-33.9,M,,0000*6E', 'GPGLL,1928.001,S,02410.820,E,184446.08,A,A*79', 'GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36', 'GPRMC,184448.08,A,1927.962,S,02410.832,E,74.00,16.78,210410,0.0,E,A*26', 'GPGGA,184449.08,1927.942,S,02410.838,E,1,04,1.7,100.00,M,-33.9,M,,0000*6C', 'GPGLL,1927.922,S,02410.844,E,184450.08,A,A*7B', 'GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36' ] with open(test_file, 'r') as test_file_fd: streamer = NMEAStream(stream_obj=test_file_fd) next_data = streamer._read() data = [] while next_data: data += next_data next_data = streamer._read() pass self.assertEqual(data, expected_result)
def start_server(self): gps_parser = NMEAStream() while True: try: if not (self.src_data is None): gps_info = source_port.readline() else: print "Loading static nav data" gps_info = self.nav_data.split('\n') for line in gps_info: gps_sentence = gps_parser._get_type(line)() gps_sentence.parse(gps_feed) if gps_sentence.sen_type == 'GPRMC' or gps_sentence.sen_type == 'GPGGA': time = datetime.strptime('%s' % gps_sentence.timestamp, '%H%M%S.%f') time += datetime.timedelta(microseconds=1000) #GPGGA print line dest_port.writeline(line) time.sleep(1) except Exception as e: pass dest_port.close()
def test_read_data_obj_raw(self): data = """$GPRMC,184332.07,A,1929.459,S,02410.381,E,74.00,16.78,210410,0.0,E,A*2B $GPGGA,184333.07,1929.439,S,02410.387,E,1,04,2.8,100.00,M,-33.9,M,,0000*65 $GPRMC,184444.08,A,1928.041,S,02410.809,E,74.00,16.78,210410,0.0,E,A*25 $GPGGA,184445.08,1928.021,S,02410.814,E,1,04,2.7,100.00,M,-33.9,M,,0000*6E $GPGLL,1928.001,S,02410.820,E,184446.08,A,A*79 $GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36 $GPRMC,184448.08,A,1927.962,S,02410.832,E,74.00,16.78,210410,0.0,E,A*26 $GPGGA,184449.08,1927.942,S,02410.838,E,1,04,1.7,100.00,M,-33.9,M,,0000*6C $GPGLL,1927.922,S,02410.844,E,184450.08,A,A*7B $GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36 """ streamer = NMEAStream() nmea_objects = streamer.get_objects(data=data) nmea_objects += streamer.get_objects(data='') expected_object_types = [ 'GPRMC', 'GPGGA', 'GPRMC', 'GPGGA', 'GPGLL', 'GPVTG', 'GPRMC', 'GPGGA', 'GPGLL', 'GPVTG' ] self.assertEqual(expected_object_types[0], nmea_objects[0].sen_type) self.assertEqual(expected_object_types[1], nmea_objects[1].sen_type) self.assertEqual(expected_object_types[2], nmea_objects[2].sen_type) self.assertEqual(expected_object_types[3], nmea_objects[3].sen_type) self.assertEqual(expected_object_types[4], nmea_objects[4].sen_type) self.assertEqual(expected_object_types[5], nmea_objects[5].sen_type) self.assertEqual(expected_object_types[6], nmea_objects[6].sen_type) self.assertEqual(expected_object_types[7], nmea_objects[7].sen_type) self.assertEqual(expected_object_types[8], nmea_objects[8].sen_type) self.assertEqual(expected_object_types[9], nmea_objects[9].sen_type)
def create_nmea_objects(self,data): self.store_data_in_file(data) nmea_txt = 'receiver.txt' with open(nmea_txt, 'r') as data_file: nmea_stream=NMEAStream(stream_obj=data_file) nmea_data=nmea_stream.get_objects() nmea_objects=[] while nmea_data: nmea_objects+=nmea_data nmea_data=nmea_stream.get_objects() return nmea_objects
def extract_gps_data(filepath,these_sentences=('GPRMC','GPGGA',)): """Use the pynmea library to read data out of an nmea log file.""" with open(filepath, 'r') as data_file: streamer = NMEAStream(data_file) next_data = streamer.get_objects() data = [] while next_data: for nd in next_data: if nd.sen_type in these_sentences: data.append(nd) next_data = streamer.get_objects() return data
def extract_gps_data(filepath, these_sentences=( 'GPRMC', 'GPGGA', )): """Use the pynmea library to read data out of an nmea log file.""" with open(filepath, 'r') as data_file: streamer = NMEAStream(data_file) next_data = streamer.get_objects() data = [] while next_data: for nd in next_data: if nd.sen_type in these_sentences: data.append(nd) next_data = streamer.get_objects() return data
def __init__(self): self.MR['rpc_connect'] = {} self.MR['rpc_disconnect'] = {} self.MR['rpc_nmeainput'] = {'line': [str, "NMEA raw data"]} self.MR['rpc_getNMEADeltaLog'] = { 'oldest': [float, "Begin of data collection."], 'newest': [float, "End of data collection. (0 = now)", 0] } self.MR['rpc_getNMEATimeLog'] = { 'eventtime': [float, "Begin of data collection."], 'maxdeviation': [float, "Maximum time deviation in seconds.", 10] } super(NMEABaseSensor, self).__init__() self.Configuration.update( {'SerialPort': 'cape.Communication.SerialPort.SerialPort_15'}) self.nmeaLog = {} self.streamer = NMEAStream()
def load_data_file(self, file_name, type='gps'): from pynmea.streamer import NMEAStream from uuts.craton.cli import navigation # Start analyze recording file Vs. gps simulator file with open(file_name, 'r') as data_file_fd: if type == 'gps': nmea_stream = NMEAStream(stream_obj=data_file_fd) elif type == 'navfix': nmea_stream = navigation.NavigationDataAnalyzer( stream_obj=data_file_fd) else: raise globals.Error("GPS information type is mismatch") next_data = nmea_stream.get_objects() nmea_objects = [] while next_data: nmea_objects += next_data next_data = nmea_stream.get_objects() data_file_fd.close() return nmea_objects
def test__read(self): test_file = os.path.join(os.path.dirname(__file__), 'test_data', 'test_data_small.gps') expected_result = ['GPRMC,184332.07,A,1929.459,S,02410.381,E,74.00,16.78,210410,0.0,E*46', 'GPGGA,184333.07,1929.439,S,02410.387,E,1,04,2.8,100.00,M,-33.9,M,,0000*65', 'GPRMC,184444.08,A,1928.041,S,02410.809,E,74.00,16.78,210410,0.0,E*48', 'GPGGA,184445.08,1928.021,S,02410.814,E,1,04,2.7,100.00,M,-33.9,M,,0000*6E', 'GPGLL,1928.001,S,02410.820,E,184446.08,A,A*79', 'GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36', 'GPRMC,184448.08,A,1927.962,S,02410.832,E,74.00,16.78,210410,0.0,E*4B', 'GPGGA,184449.08,1927.942,S,02410.838,E,1,04,1.7,100.00,M,-33.9,M,,0000*6C', 'GPGLL,1927.922,S,02410.844,E,184450.08,A,A*7B', 'GPVTG,16.78,T,,M,74.00,N,137.05,K,A*36'] with open(test_file, 'r') as test_file_fd: streamer = NMEAStream(stream_obj=test_file_fd) next_data = streamer._read() data = [] while next_data: data += next_data next_data = streamer._read() pass self.assertEqual(data, expected_result)
def __init__(self): self.MR['rpc_connect'] = {} self.MR['rpc_disconnect'] = {} self.MR['rpc_nmeainput'] = {'line': [str, "NMEA raw data"]} self.MR['rpc_getNMEADeltaLog'] = {'oldest': [float, "Begin of data collection."], 'newest': [float, "End of data collection. (0 = now)", 0]} self.MR['rpc_getNMEATimeLog'] = {'eventtime': [float, "Begin of data collection."], 'maxdeviation': [float, "Maximum time deviation in seconds.", 10]} super(NMEABaseSensor, self).__init__() self.Configuration.update({'SerialPort': 'cape.Communication.SerialPort.SerialPort_15'}) self.nmeaLog = {} self.streamer = NMEAStream()
def cmd_GPS(args): '''set GPS device''' state = mpstate.GPS_state if len(args) != 1: print("Usage: GPS <device>") return state.device = args[0] state.port = serial.Serial(state.device, 38400, timeout=0, dsrdtr=False, rtscts=False, xonxoff=False) state.port.write("AT*E2GPSCTL=1,5,1\r\n") time.sleep(2) state.port.write("AT*E2GPSNPD\r\n") time.sleep(2) state.nmea = NMEAStream(stream_obj=state.port)
def test_splits_data_4(self): test_data = '$foo,bar,baz*77NOTHING$Meep,wibble,123,321NOTHING' streamer = NMEAStream() result = streamer._split(test_data, separator='NOTHING') self.assertEqual(result, ['foo,bar,baz*77', 'Meep,wibble,123,321'])
print "Type,Time,R1 Age, R2 Age, Age Matchs,R1 Q, R2 Q, Q Matches,R1 Cont,R2 Cont,R1 Total,R2 Total" #create an INET, STREAMing socket s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # - the normal http port s1.connect(("192.168.128.31", 28001)) s1.setblocking(0) s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s2.connect(("192.168.128.35", 28002)) s2.setblocking(0) #s2.settimeout(2) streamer1 = NMEAStream() streamer2 = NMEAStream() data_obs =[] run_main_loop = True s1_time=None s1_age=None s1_quaility=None s1_radio=0 s1_total_radio=0 s2_time=None s2_age=None s2_quaility=None s2_radio=0 s2_total_radio=0
def test_splits_data_1(self): test_data = '$foo,bar,baz*77\n$Meep,wibble,123,321\n' streamer = NMEAStream() result = streamer._split(test_data) self.assertEqual(result, ['foo,bar,baz*77', 'Meep,wibble,123,321'])
def test__get_type(self): streamer = NMEAStream() sentence = '$GPRMC,184332.07,A,1929.459,S,02410.381,E,74.00,16.78,210410,0.0,E,A*2B' sen_type = streamer._get_type(sentence) self.assertTrue(isinstance(sen_type(), GPRMC))
class NMEABaseSensor(RPCComponent): """ Basic NMEA capable sensor component Inherited components have an understanding of nmea data and can handle an attached SerialPort component's ouutgoing NMEA traffic. """ def __init__(self): self.MR['rpc_connect'] = {} self.MR['rpc_disconnect'] = {} self.MR['rpc_nmeainput'] = {'line': [str, "NMEA raw data"]} self.MR['rpc_getNMEADeltaLog'] = {'oldest': [float, "Begin of data collection."], 'newest': [float, "End of data collection. (0 = now)", 0]} self.MR['rpc_getNMEATimeLog'] = {'eventtime': [float, "Begin of data collection."], 'maxdeviation': [float, "Maximum time deviation in seconds.", 10]} super(NMEABaseSensor, self).__init__() self.Configuration.update({'SerialPort': 'cape.Communication.SerialPort.SerialPort_15'}) self.nmeaLog = {} self.streamer = NMEAStream() def main_prepare(self): self.loginfo("Subscribing to configured SerialPort") request = Message(sender=self.name, recipient=self.Configuration['SerialPort'], func="subscribe", arg={'function': 'nmeainput', 'name': self.name}) self.send(request, "outbox") def rpc_nmeainput(self, line): """ Called when a publisher sends a new nmea sentence to this sensor. The nmea data is parsed and further handling can happen. """ sen_time = time() # TODO: This is late due to message traversal etc. for sentence in self.streamer.get_objects(line): self.nmeaLog[sen_time] = {'raw': line, 'type': sentence.sen_type, 'obj': sentence} for recipient, func in self.subscribers.items(): msg = Message(sender=self.name, recipient=recipient, func=func, arg={'args': (sentence.sen_type, sentence)}) self.send(msg, "outbox") def rpc_getNMEATimeLog(self, eventtime, maxdeviation=10): if eventtime < 0: eventtime = time() - eventtime minimum, maximum = eventtime - maxdeviation, eventtime + maxdeviation #print((minimum, maximum)) reqdict = {} for key in self.nmeaLog.keys(): #print(key) if minimum < key < maximum: reqdict[key] = self.nmeaLog[key] return True, reqdict def rpc_getNMEADeltaLog(self, oldest, newest=0): if newest == 0: newest = time() if oldest < 0: oldest += newest reqdict = {} reqkeys = self.nmeaLog.keys() self.loginfo("Looking for nmea data in time '%f'-'%f'." % (oldest, newest)) for key in reqkeys: if oldest < key < newest: reqdict[key] = self.nmeaLog[key] return True, reqdict def rpc_connect(self): """ Tries to connect to the serial device. You have to set parameters before calling connect. """ self.loginfo("RPC Connect called.") pass def rpc_disconnect(self): """ Disconnects the serial device. """ self.loginfo("RPC Disconnect called.") pass
def start_gps_reader(self): # serial_port = self.start_serial_connection() #sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM ) #sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) #sock.setblocking(0) #sock.bind( ('', UDP_SERVER_PORT) ) context = zmq.Context() sock = context.socket(zmq.SUB) sock.setsockopt(zmq.SUBSCRIBE, '') sock.connect('tcp://localhost:%d' % UDP_SERVER_PORT) gps_parser = NMEAStream() sat_lock = 0 print "Starting reading from port" if sock: while True: try: #gps_feed = serial_port.readline() gps_feed = sock.recv() if gps_feed[0] != '$': print "JUNK feed : " + gps_feed continue # print "gps_feed : " + gps_feed gps_sentence = gps_parser._get_type(gps_feed)() gps_sentence.parse(gps_feed) if gps_sentence.sen_type == 'GPGSA': if (int(gps_sentence.mode_fix_type) > 1) and (sat_lock < 3): # Set last lock state sat_lock = int(gps_sentence.mode_fix_type) print "Sat is locked with value of {}".format( sat_lock) # Set computer clock if gps_sentence.sen_type == 'GPRMC': # print "date " + gps_sentence.datestamp + " time : " + gps_sentence.timestamp #date 240413 time : 201917.00 a = '%s %s' % (gps_sentence.datestamp, gps_sentence.timestamp) time_obj = time.strptime('%s' % a, '%d%m%y %H%M%S.%f') if ((datetime.datetime.strptime( '%s' % a, '%d%m%y %H%M%S.%f').microsecond) == 0): new_time = [ time_obj.tm_year, time_obj.tm_mon, time_obj.tm_wday, time_obj.tm_mday, time_obj.tm_hour, time_obj.tm_min, time_obj.tm_sec, 0 ] # new_time = [ time_obj.year, time_obj.month, time_obj.tm_wday, time_obj.tm_mday, time_obj.tm_hour, time_obj.tm_min, time_obj.tm_sec, 0] print "Updating clock to: date " + gps_sentence.datestamp + " time : " + gps_sentence.timestamp win32api.SetSystemTime(*new_time) except Exception, e: pass
from pynmea.streamer import NMEAStream data_file = '../tests/test_data/test_data.gps' with open(data_file, 'r') as data_file_fd: nmea_stream = NMEAStream(stream_obj=data_file_fd) next_data = nmea_stream.get_objects() nmea_objects = [] while next_data: nmea_objects += next_data next_data = nmea_stream.get_objects() # All nmea objects are now in variable nmea_objects for nmea_ob in nmea_objects: print nmea_ob.sen_type
d = line.split(',') print d[7] f.write(d[7] + "\n") #f.write(d[2] + "," + d[4] + "\n") #print line except: pass print gga print lines f.close() quit() time.sleep(5) with open('gpslog291112.txt','r') as data_file: streamer = NMEAStream(data_file) next_data = streamer.get_objects() data = [] i = 0 samples = 0 while next_data: try: datapack = [] data += next_data next_data = streamer.get_objects() #print type(next_data) try: #print data[i].latitude #print data[i].lat_direction #print data[i].longitude
def test_splits_data_3(self): test_data = '$foo,bar,baz*77\r\n$Meep,wibble,123,321' streamer = NMEAStream() result = streamer._split(test_data) self.assertEqual(result, ['foo,bar,baz*77', 'Meep,wibble,123,321'])
def readUbx(data_file): data_file = filterChar(data_file) with open(data_file, 'r') as data_file_fd: nmea_stream = NMEAStream(stream_obj=data_file_fd) #allstrs = nmea_stream.get_strings(size=2048) next_data = nmea_stream.get_objects() #print allstrs #exit() nmea_objects = [] while next_data: nmea_objects += next_data next_data = nmea_stream.get_objects() # All nmea objects are now in variable nmea_objects ubloxgps = {'ts':[], 'lat':[],'lon':[], 'bear':[], 'alt':[], 'hdop':[], } ubloxgpslist = [] lastts = None currentpt = {} for nmea_ob in nmea_objects: #print nmea_ob.sen_type, nmea_ob #if nmea_ob.sen_type == 'GPGGA': # print nmea_ob.timestamp, nmea_ob.latitude if nmea_ob.sen_type == 'GPRMC': lastts = nmea_ob.timestamp dt = getDateTime(nmea_ob.datestamp, nmea_ob.timestamp) ts = long(unix_time_millis(dt)) latlon = convertLatLon(nmea_ob.lat, nmea_ob.lat_dir, nmea_ob.lon, nmea_ob.lon_dir) tc = nmea_ob.true_course if tc != '': tc = float(tc) else: tc = 0.0 if latlon != None: ubloxgps['lat'].append(latlon[0]) ubloxgps['lon'].append(latlon[1]) ubloxgps['bear'].append(tc) ubloxgps['ts'].append(ts) currentpt['lat'] = (latlon[0]) currentpt['lon'] = (latlon[1]) currentpt['bear'] = (tc) currentpt['ts'] = (ts) if nmea_ob.spd_over_grnd != '': currentpt['spd'] = float(nmea_ob.spd_over_grnd) else: currentpt['spd'] = 0.0 #ubloxgpslist.append({}) if nmea_ob.sen_type == 'GPGGA': if nmea_ob.timestamp == lastts and nmea_ob.latitude != '' and nmea_ob.longitude != '': if nmea_ob.antenna_altitude != '': currentpt['alt'] = float(nmea_ob.antenna_altitude) else: currentpt['alt'] = 0.0 currentpt['hdop'] = float(nmea_ob.horizontal_dil) if currentpt != {}: ubloxgpslist.append(currentpt) currentpt = {} return ubloxgps, ubloxgpslist
def __init__(self): super(NMEAParser, self).__init__() self.streamer = NMEAStream()
class NMEAParser(component): """Parses raw data (e.g. from a serialport) for NMEA data and sends single sentences out. """ Inboxes = { "inbox" : "Items", "control" : "Shutdown signalling", } Outboxes = { "outbox" : "Items tagged with a sequence number, in the form (seqnum, item)", "signal" : "Shutdown signalling", } def __init__(self): super(NMEAParser, self).__init__() self.streamer = NMEAStream() #self.streamer.get_objects("\n") def finished(self): while self.dataReady("control"): msg = self.recv("control") if type(msg) in (producerFinished, shutdownMicroprocess): self.send(msg, "signal") return True return False def _parse(self, data): """ Called when a publisher sends a new nmea sentence to this sensor. The nmea data is parsed and returned as NMEASentence object """ sentences = [] sen_time = time.time() # TODO: This is late due to message traversal etc. # TODO: Something here is fishy, as in the first received packet # gets lost in the NMEAStream object. WTF. for sentence in self.streamer.get_objects(data, size=len(data)+1): nmeadata = sentence.__dict__ del(nmeadata['parse_map']) del(nmeadata['nmea_sentence']) nmeadata['time'] = sen_time nmeadata['type'] = sentence.sen_type #sentences.append(nmeadata) self.send(nmeadata, "outbox") #return sentences def main(self): """Main loop.""" while not self.finished(): while not self.anyReady(): self.pause() yield 1 while self.dataReady("inbox"): data = self.recv("inbox") if len(data) > 0: if data[-1] != "\n": data += "\n" self._parse(data) # # if result: # for sentence in result: # self.send(sentence, "outbox") yield 1
d = line.split(',') print d[7] f.write(d[7] + "\n") #f.write(d[2] + "," + d[4] + "\n") #print line except: pass print gga print lines f.close() quit() time.sleep(5) with open('gpslog291112.txt', 'r') as data_file: streamer = NMEAStream(data_file) next_data = streamer.get_objects() data = [] i = 0 samples = 0 while next_data: try: datapack = [] data += next_data next_data = streamer.get_objects() #print type(next_data) try: #print data[i].latitude #print data[i].lat_direction #print data[i].longitude
"""Parse NMEA GPS strings""" from pynmea.streamer import NMEAStream nmeaFile = open("nmea.txt") nmea_stream = NMEAStream(stream_obj=nmeaFile) next_data = nmea_stream.get_objects() nmea_objects = [] while next_data: nmea_objects += next_data next_data = nmea_stream.get_objects() # The NMEA stream is parsed! # Let's loop through the # Python object types: for nmea_ob in nmea_objects: if hasattr(nmea_ob, "lat"): print "Lat/Lon: (%s, %s)" % (nmea_ob.lat, nmea_ob.lon)
def test__get_type(self): streamer = NMEAStream() sentence = '$GPRMC,184332.07,A,1929.459,S,02410.381,E,74.00,16.78,210410,0.0,E*46' sen_type = streamer._get_type(sentence) self.assertTrue(isinstance(sen_type(), RMC))
class NMEABaseSensor(RPCComponent): """ Basic NMEA capable sensor component Inherited components have an understanding of nmea data and can handle an attached SerialPort component's ouutgoing NMEA traffic. """ def __init__(self): self.MR['rpc_connect'] = {} self.MR['rpc_disconnect'] = {} self.MR['rpc_nmeainput'] = {'line': [str, "NMEA raw data"]} self.MR['rpc_getNMEADeltaLog'] = { 'oldest': [float, "Begin of data collection."], 'newest': [float, "End of data collection. (0 = now)", 0] } self.MR['rpc_getNMEATimeLog'] = { 'eventtime': [float, "Begin of data collection."], 'maxdeviation': [float, "Maximum time deviation in seconds.", 10] } super(NMEABaseSensor, self).__init__() self.Configuration.update( {'SerialPort': 'cape.Communication.SerialPort.SerialPort_15'}) self.nmeaLog = {} self.streamer = NMEAStream() def main_prepare(self): self.loginfo("Subscribing to configured SerialPort") request = Message(sender=self.name, recipient=self.Configuration['SerialPort'], func="subscribe", arg={ 'function': 'nmeainput', 'name': self.name }) self.send(request, "outbox") def rpc_nmeainput(self, line): """ Called when a publisher sends a new nmea sentence to this sensor. The nmea data is parsed and further handling can happen. """ sen_time = time() # TODO: This is late due to message traversal etc. for sentence in self.streamer.get_objects(line): self.nmeaLog[sen_time] = { 'raw': line, 'type': sentence.sen_type, 'obj': sentence } for recipient, func in self.subscribers.items(): msg = Message(sender=self.name, recipient=recipient, func=func, arg={'args': (sentence.sen_type, sentence)}) self.send(msg, "outbox") def rpc_getNMEATimeLog(self, eventtime, maxdeviation=10): if eventtime < 0: eventtime = time() - eventtime minimum, maximum = eventtime - maxdeviation, eventtime + maxdeviation #print((minimum, maximum)) reqdict = {} for key in self.nmeaLog.keys(): #print(key) if minimum < key < maximum: reqdict[key] = self.nmeaLog[key] return True, reqdict def rpc_getNMEADeltaLog(self, oldest, newest=0): if newest == 0: newest = time() if oldest < 0: oldest += newest reqdict = {} reqkeys = self.nmeaLog.keys() self.loginfo("Looking for nmea data in time '%f'-'%f'." % (oldest, newest)) for key in reqkeys: if oldest < key < newest: reqdict[key] = self.nmeaLog[key] return True, reqdict def rpc_connect(self): """ Tries to connect to the serial device. You have to set parameters before calling connect. """ self.loginfo("RPC Connect called.") pass def rpc_disconnect(self): """ Disconnects the serial device. """ self.loginfo("RPC Disconnect called.") pass