[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
Dennis Ritchie » ImaginaryNumber » BeginnersGuideToVBNet » pixel » RAD » cppint » DotNet » Curl Content Language » userpage » QbasicFAQ_WhereToGetIt » Calculating and Validating NMEA Checksums
Displaying differences between revision 14 and the latest revision
=== 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:
[code]
$GPGSA,A,2,29,19,28,,,,,,,,,,23.4,12.1,20.0[blue][b]*0F[/b][/blue]
[/code]
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]
[code]
' 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
[/code]
[C#]
[code]
// 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");
}
[/code]
[code]
//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");
}
[/code]
==== Comparing a Calculated Checksum to the Actual Checksum ====
This function will return [blue]true[/blue] if the calculated checksum of the specified sentence matches the checksum in the sentence.
[Visual Basic.NET]
[code]
' 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
[/code]
[C#]
[code]
// 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)
}
[/code]
If you need help with this page, please feel free to contact Jon Person at info@gpsdotnet.com.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
Dennis Ritchie » ImaginaryNumber » BeginnersGuideToVBNet » pixel » RAD » cppint » DotNet » Curl Content Language » userpage » QbasicFAQ_WhereToGetIt » Calculating and Validating NMEA Checksums
Displaying differences between revision 14 and the latest revision
=== 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:
[code]
$GPGSA,A,2,29,19,28,,,,,,,,,,23.4,12.1,20.0[blue][b]*0F[/b][/blue]
[/code]
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]
[code]
' 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
[/code]
[C#]
[code]
// 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");
}
[/code]
[code]
//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
// Loop through all chars to get a checksum
for (int i=sentence.IndexOf
// 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");
}
[/code]
==== Comparing a Calculated Checksum to the Actual Checksum ====
This function will return [blue]true[/blue] if the calculated checksum of the specified sentence matches the checksum in the sentence.
[Visual Basic.NET]
[code]
' 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
[/code]
[C#]
[code]
// 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)
}
[/code]
If you need help with this page, please feel free to contact Jon Person at info@gpsdotnet.com.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
