[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
Calculating and Validating NMEA Checksums » CppBuilder » CMOS_C » The GPRMC Sentence » TurboCpp » BeginnersGuideToCurl » Curl Content Language » QbasicFAQ_Mouse » rs 485 » Euphoria » Taking Out the Garbage: NMEA Checksums
[C#]
[VB.NET]
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
Calculating and Validating NMEA Checksums » CppBuilder » CMOS_C » The GPRMC Sentence » TurboCpp » BeginnersGuideToCurl » Curl Content Language » QbasicFAQ_Mouse » rs 485 » Euphoria » Taking Out the Garbage: NMEA Checksums
Taking Out the Garbage: NMEA Checksums
An NMEA checksum is calculated as the XOR of bytes between (but not including) the dollar sign and asterisk. This checksum is then compared with the checksum from the sentence. If the checksums do not match, the sentence is typically discarded. This is okay to do because the GPS devices tend to repeat the same information every few seconds. With the ability to compare checksums, the interpreter is able to throw out any sentence with an invalid checksum.[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);
}
// Calculates the checksum for a sentence
public string GetChecksum(string sentence)
{
// Loop through all chars to get a checksum
int Checksum = 0;
foreach (char Character in sentence)
{
if (Character == '$')
{
// Ignore the dollar sign
}
else if (Character == '*')
{
// Stop processing before the asterisk
break;
}
else
{
// 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);
}
}
}
// Return the checksum formatted as a two-character hexadecimal
return Checksum.ToString("X2");
}
[VB.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
' 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
Written By Jon Person (http://www.gpsdotnet.com)
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
