Electrical – Generating Hexadecimal checksum for a NMEA (PMTK) message

gps

I am trying to use quectel L80 GPS receiver for my project. I am trying to configure the L80 using the PMTK strings. But the problem is the string should also contain a hexadecimal check sum.

eg. $PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29

This is the example string given in the "protocol specification" document.
In this string '29' is the check sum. My question is how do I generate the checksum? I know that for NMEA strings, the check sum is generated by 'XOR' operation of consecutive characters between '$' and '*' . while calculating the check sum do I also need to include 'comma'?

Thanks

Best Answer

You just exclusive-or all the characters between the '$' and the '*' (including commas) and use the two-character hex representation of that byte (leading 0 if required to pad to two characters). Here's an example code fragment in Python:

st = "PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
i = 0
checksum = 0
while i < len(st):
   checksum ^= ord(st[i])
   i+= 1
print "%02X"%checksum

Result is 29

For "GPGSA,A,3,10,07,05,02,29,04,08,13,,,,,1.72,1.03,1.38", the result is 0A