Index checksum explicitly on $ and *
parse() Improves slightly on https://github.com/adafruit/Adafruit_GPS/pull/86 by indexing on the position of the $ and the * in the string instead of the beginning and the end of the string. Does not require sentence string have a particular termination, e.g. CRLF.
This commit is contained in:
parent
b431f20491
commit
44af8c9086
|
|
@ -57,14 +57,13 @@ boolean Adafruit_GPS::parse(char *nmea) {
|
|||
// do checksum check
|
||||
|
||||
// first look if we even have one
|
||||
size_t len = strlen(nmea);
|
||||
if (nmea[len-5] == '*') {
|
||||
uint16_t sum = parseHex(nmea[len-4]) * 16;
|
||||
sum += parseHex(nmea[len-3]);
|
||||
|
||||
char *ast = strchr(nmea,'*');
|
||||
if (ast != NULL) {
|
||||
uint16_t sum = parseHex(*(ast+1)) * 16;
|
||||
sum += parseHex(*(ast+2));
|
||||
// check checksum
|
||||
for (uint8_t i=1; i < (len-5); i++) {
|
||||
sum ^= nmea[i];
|
||||
for (char *p = strchr(nmea,'$')+1; p < ast; p++) {
|
||||
sum ^= *p;
|
||||
}
|
||||
if (sum != 0) {
|
||||
// bad checksum :(
|
||||
|
|
|
|||
Loading…
Reference in New Issue