I have a problem with calculating the checksum for NMEA sentences. I am using the following java code:
private static String getSum(String in) {
int checksum = 0;
if (in.startsWith("$")) {
in = in.substring(1, in.length());
}
int end = in.indexOf('*');
if (end == -1)
end = in.length();
for (int i = 0; i < end; i++) {
checksum = checksum ^ in.charAt(i);
}
String hex = Integer.toHexString(checksum);
if (hex.length() == 1)
hex = "0" + hex;
return hex.toUpperCase();
}
This code is similar to many other examples around the internet and everything works fine until I try a sentence like this..
$PSRF101,-2686700,-4304200,3851624,96000,497260,921,12,3*1C
This sentence is from the NMEA Reference Manual and so I assume the checksum will be correct. But when I calculate it, I get *2F as the checksum and not 1C.
I think this is because of the negative values in the sentence, but I have no clue how to deal with them. Does anybody have a suggestion?
Index = first, checkSum = 0, while index < msgLen, checkSum = checkSum + message[index], checkSum = checkSum AND (2^15-1). increment index. problem is that we are getting 3588 but it should be 3845(0F05).
To calculate the checksum of an API frame: Add all bytes of the packet, except the start delimiter 0x7E and the length (the second and third bytes). Keep only the lowest 8 bits from the result. Subtract this quantity from 0xFF.
A checksum is a value that represents the number of bits in a transmission message and is used by IT professionals to detect high-level errors within data transmissions. Prior to transmission, every piece of data or file can be assigned a checksum value after running a cryptographic hash function.
The difference of the assumed and the calculated checksums equals to omitting (or having an extra character '3'); so I'd be tempted to believe in error in the NMEA Reference Manual.
You can try some online NMEA calculator to verify the results.
e.g. http://www.hhhh.org/wiml/proj/nmeaxor.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With