Skip to content

Instantly share code, notes, and snippets.

@aderbas
Created November 8, 2018 17:24
Show Gist options
  • Save aderbas/967d3ad7536d2f768e93d428b1c492df to your computer and use it in GitHub Desktop.
Save aderbas/967d3ad7536d2f768e93d428b1c492df to your computer and use it in GitHub Desktop.
Read and parse NMEA data info from USB GPS dongle
#!/usr/bin/python
import re
from decimal import Decimal
import math
import time
import serial
class FormatNMEA(object):
@staticmethod
def format(string):
'''
Format string default NMEA GGA
'''
arr = []
arr = string.split(',')
if(arr.__len__() >= 9):
''' expected format '''
try:
lat = float( arr[2][2:arr[2].__len__()] )
latd = int( FormatNMEA.getLatDeg(arr[2]) )
latway = arr[3] # S = (-)
lng = float( arr[4][3:arr[4].__len__()] )
lngd = int( FormatNMEA.getLngDeg(arr[4]) )
lngway = arr[5] # W = (-)
#print FormatNMEA.calculeDecimal(latd, lat, latway)
#print FormatNMEA.calculeDecimal(lngd, lng, lngway)
return [FormatNMEA.calculeDecimal(latd, lat, latway), FormatNMEA.calculeDecimal(lngd, lng, lngway)]
except (IndexError):
return []
@staticmethod
def getLatDeg(lat):
return lat[0:2]
@staticmethod
def getLngDeg(lng):
return lng[1:3]
@staticmethod
def calculeDecimal(deg_part, min_part, way):
if(way == 'S' or way == 'W'):
way = "-"
else:
way = ""
return way + str(deg_part + (min_part / 60))
# Start serial coon
comport = serial.Serial('/dev/ttyUSB1', 4800)
comport.timeout = 1
while True:
data = comport.readline()
if data[0:6] == '$GPGGA':
coords = FormatNMEA.format(data)
print coords
comport.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment