Skip to content

Instantly share code, notes, and snippets.

@dzonesasaki
Created August 2, 2022 08:13
Show Gist options
  • Save dzonesasaki/dffaf446458dbf4c5954d6a7fac2f567 to your computer and use it in GitHub Desktop.
Save dzonesasaki/dffaf446458dbf4c5954d6a7fac2f567 to your computer and use it in GitHub Desktop.
get lat,lon from NMEA message of GPS module GYSFDMAXB , GYSFFMANC
#!python3
# read_NMEA_tst.py
# hardware : GYSFFMANC :GYSFDMAXB : MT3333 : https://akizukidenshi.com/catalog/g/gK-13850/
# only for JAPAN
TIME_ZONE = 9
fLat = 0
fLon = 0
fAlt = 0
fHour = 0
fMinute = 0
fSec = 0
fYear = 0
fMonth = 0
fDay = 0
def degmin2deg(x):
# ddmm.mmmm to dd.ddddd , sexagesimal to decimal
dd = float(int(x/100))
m = x - dd*100
fd = m/60
deg = dd + fd
return deg
def sep_hourminsec(s):
sH = s[0:2]
sM = s[2:4]
sS = s[4:]
fHour = float(sH) + TIME_ZONE
fMinute = float(sM)
fSec = float(sS)
fADay = 0
if fHour >= 24:
fHour -=24
fADay += 1
return fHour,fMinute,fSec,fADay
def str2float(s):
x = float(s)
if x==float('nan'):
x=0
return(x)
def process_GPGGA(s):
ls = s.split(',')
sTim = ls[1]
sLat = ls[2]
sNS = ls[3]
sLon = ls[4]
sWE = ls[5]
sQua = ls[6]
sNoS = ls[7]
sHEr = ls[8]
sAlt = ls[9]
sUnA = ls[10]
sGAl = ls[11]
sUnG = ls[12]
sDgp = ls[13]
sDid = ls[14]
if sLat == '' or sNS =='S' or sWE =='W':
# not JAPAN
return 0,0,0,0,0,0,0
fLat = degmin2deg(str2float(sLat))
fLon = degmin2deg(str2float(sLon))
fAlt = str2float(sAlt)
fHour,fMinute,fSec,fADay = sep_hourminsec(sTim)
return fLat,fLon,fAlt,fHour,fMinute,fSec,fADay
def switch_process(s):
global fLat
global fLon
global fAlt
global fHour
global fMinute
global fSec
if s.startswith('$GPGGA'):
fLat,fLon,fAlt,fHour,fMinute,fSec,fADay = process_GPGGA(s)
#----- test ------
tststr = '$GPGGA,055045.000,,,,,0,0,,,M,,M,,*49'
switch_process(tststr)
print(fLat,fLon,fAlt,fHour,fMinute,fSec)
tststr = '$GPGGA,060610.000,3943.5180,N,14039.0526,E,1,5,3.14,49.2,M,37.0,M,,*6A'
switch_process(tststr)
print(fLat,fLon,fAlt,fHour,fMinute,fSec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment