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:
sellensr 2019-04-16 19:49:01 -04:00
parent b431f20491
commit 44af8c9086
1 changed files with 6 additions and 7 deletions

View File

@ -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 :(