From 44af8c908664f4e7c274a6fd0dabc422def2dd68 Mon Sep 17 00:00:00 2001 From: sellensr Date: Tue, 16 Apr 2019 19:49:01 -0400 Subject: [PATCH] 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. --- Adafruit_GPS.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Adafruit_GPS.cpp b/Adafruit_GPS.cpp index c53aea4..4f0214a 100755 --- a/Adafruit_GPS.cpp +++ b/Adafruit_GPS.cpp @@ -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 :(