[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
Calculating and Validating NMEA Checksums

Calculating and Validating NMEA Checksums

Error correction and detection in NMEA data is handled through the use of checksums. A checksum is a two-character hexadecimal number, located at the end of each NMEA sentence, representing the "two's complement" of the sentence:

$GPGSA,A,2,29,19,28,,,,,,,,,,23.4,12.1,20.0*0F


In other words, each byte value between the dollar sign ($) and asterisk (*) is XOR'ed.

If the calculated checksum does not match the checksum in the sentence, the entire sentence is typically discarded. This is an acceptable practice because GPS receivers typically re-transmit the same kind of sentence every few seconds.

Calculating an NMEA Checksum

This function will calculate the checksum of the specified sentence. The function will work with sentences which already have a checksum.

[Visual Basic.NET]
  ' Calculates the checksum for a sentence
  Public Function GetChecksum(ByVal sentence As String) As String
    ' Loop through all chars to get a checksum
    Dim Character As Char
    Dim Checksum As Integer
    For Each Character In sentence
      Select Case Character
        Case "$"c
          ' Ignore the dollar sign
        Case "*"c
          ' Stop processing before the asterisk
          Exit For
        Case Else
          ' Is this the first value for the checksum?
          If Checksum = 0 Then
            ' Yes. Set the checksum to the value
            Checksum = Convert.ToByte(Character)
          Else
            ' No. XOR the checksum with this character's value
            Checksum = Checksum Xor Convert.ToByte(Character)
          End If
      End Select
    Next
    ' Return the checksum formatted as a two-character hexadecimal
    Return Checksum.ToString("X2")
  End Function


[C#]
// Calculates the checksum for a sentence
string GetChecksum(string sentence)
{
	// Loop through all chars to get a checksum
	char Character;
	int Checksum;
	foreach(char Character in sentence)
	{
		switch(Character)
		{
			case "$":
				// Ignore the dollar sign
				break;
			case "*":
				// Stop processing before the asterisk
				continue;
			default:
				// Is this the first value for the checksum?
				if (Checksum == 0)
				{
					// Yes. Set the checksum to the value
					Checksum = Convert.ToByte(Character);
				}
				else
				{
					// No. XOR the checksum with this character's value
					Checksum = Checksum ^ Convert.ToByte(Character);
				}
				break;
		}
	}
	// Return the checksum formatted as a two-character hexadecimal
	return Checksum.ToString("X2");
}
//above code doesnt work. This is better:
			// Calculates the checksum for a sentence
		private static string getChecksum(string sentence) {
			//Start with first Item
			int checksum= Convert.ToByte(sentence[sentence.IndexOf('$')+1]);
			// Loop through all chars to get a checksum
			for (int i=sentence.IndexOf('$')+2 ; i<sentence.IndexOf('*') ; i++){
					// No. XOR the checksum with this character's value
					checksum^=Convert.ToByte(sentence[i]);				
			}
			// Return the checksum formatted as a two-character hexadecimal
			return checksum.ToString("X2");
		}


Comparing a Calculated Checksum to the Actual Checksum

This function will return true if the calculated checksum of the specified sentence matches the checksum in the sentence.

[Visual Basic.NET]
  ' Returns True if a sentence's checksum matches the calculated checksum
  Public Function IsValid(ByVal sentence As String) As Boolean
    ' Compare the characters after the asterisk to the calculation
    Return sentence.Substring(sentence.IndexOf("*") + 1) = GetChecksum(sentence)
  End Function


[C#]
  // Returns true if a sentence's checksum matches the calculated checksum
  public bool IsValid(string sentence) 
  {
    // Compare the characters after the asterisk to the calculation
    return sentence.Substring(sentence.IndexOf("*") + 1) = GetChecksum(sentence)
  }


If you need help with this page, please feel free to contact Jon Person at info@gpsdotnet.com.

last edited (August 28, 2006) by twhitehead, Number of views: 25688, Current Rev: 15 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.