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
|
// do checksum check
|
||||||
|
|
||||||
// first look if we even have one
|
// first look if we even have one
|
||||||
size_t len = strlen(nmea);
|
char *ast = strchr(nmea,'*');
|
||||||
if (nmea[len-5] == '*') {
|
if (ast != NULL) {
|
||||||
uint16_t sum = parseHex(nmea[len-4]) * 16;
|
uint16_t sum = parseHex(*(ast+1)) * 16;
|
||||||
sum += parseHex(nmea[len-3]);
|
sum += parseHex(*(ast+2));
|
||||||
|
|
||||||
// check checksum
|
// check checksum
|
||||||
for (uint8_t i=1; i < (len-5); i++) {
|
for (char *p = strchr(nmea,'$')+1; p < ast; p++) {
|
||||||
sum ^= nmea[i];
|
sum ^= *p;
|
||||||
}
|
}
|
||||||
if (sum != 0) {
|
if (sum != 0) {
|
||||||
// bad checksum :(
|
// bad checksum :(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue