Skip to content

Instantly share code, notes, and snippets.

@maxp
Created February 7, 2013 03:52
Show Gist options
  • Select an option

  • Save maxp/4728292 to your computer and use it in GitHub Desktop.

Select an option

Save maxp/4728292 to your computer and use it in GitHub Desktop.
def parse( data ):
"""
@return {ok:<0|1>,ts:<utc_datetime>,coord:[<lon>,<lat>],speed:<km/h>,dir:<bearing>}
or None on error or not gps ok
"""
try:
gp = data.index("GPRMC")
cp = data.index("*",gp+4)
i = data.index('imei:', cp)
imei = data[i+5:data.index(',',i+6)]
gprmc = data[gp:cp]
csum = 0
for c in gprmc:
csum ^= ord(c)
if csum != int(data[cp+1:cp+3], 16):
return None # bad checksum
f = gprmc.split(',',10)
return {
'imei': imei,
'ok': [0,1][f[2] == 'A'],
'ts': datetime.strptime(f[9]+'-'+f[1]+"000","%d%m%y-%H%M%S.%f"),
'coord': [
(float(f[5][0:3]) + (float(f[5][3:])/60))*{'E':1,'W':-1}[f[6]],
(float(f[3][0:2]) + (float(f[3][2:])/60))*{'N':1,'S':-1}[f[4]],
],
'speed': float(f[7])*1.852, # knots to km/h
'dir': float(f[8]), # azimuth (deg.)
}
except (ValueError, IndexError):
# wrong format
pass
return None
#--
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment