Skip to content

Instantly share code, notes, and snippets.

@mendhak
Created November 24, 2018 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mendhak/8d67b2412d97f593dbbb3abee06e5293 to your computer and use it in GitHub Desktop.
Save mendhak/8d67b2412d97f593dbbb3abee06e5293 to your computer and use it in GitHub Desktop.
Calculate checksum from the data part of an NMEA sentence
import sys
import operator
# Given the data part of an NMEA sentence (Remove the $, remove the * onwards, keep the last comma)
# calculates the checksum
def checksum_calculate(nmeadata):
csum = 0
for c in nmeadata:
csum ^= ord(c)
return csum
if __name__=='__main__':
nmeadata=sys.argv[1] if len(sys.argv)>1 else "GPGGA,174134.000,4345.9112,N,09643.8029,W,1,05,2.7,452.6,M,-27.1,M,,0000"
checksum=checksum_calculate(nmeadata)
print("$" + nmeadata + "*" + format(checksum,'x').zfill(2).upper())
print(hex(checksum))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment