Merge pull request #86 from srob1/read-cr-lf-fix
Fix read() so NMEA sentences are properly CR-LF terminated
This commit is contained in:
commit
c149283ea8
|
|
@ -36,6 +36,8 @@
|
||||||
|
|
||||||
#define MAXLINELENGTH 120 ///< how long are max NMEA lines to parse?
|
#define MAXLINELENGTH 120 ///< how long are max NMEA lines to parse?
|
||||||
|
|
||||||
|
static boolean strStartsWith(const char* str, const char* prefix);
|
||||||
|
|
||||||
volatile char line1[MAXLINELENGTH]; ///< We double buffer: read one line in and leave one for the main program
|
volatile char line1[MAXLINELENGTH]; ///< We double buffer: read one line in and leave one for the main program
|
||||||
volatile char line2[MAXLINELENGTH]; ///< Second buffer
|
volatile char line2[MAXLINELENGTH]; ///< Second buffer
|
||||||
volatile uint8_t lineidx=0; ///< our index into filling the current line
|
volatile uint8_t lineidx=0; ///< our index into filling the current line
|
||||||
|
|
@ -55,12 +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
|
||||||
if (nmea[strlen(nmea)-4] == '*') {
|
size_t len = strlen(nmea);
|
||||||
uint16_t sum = parseHex(nmea[strlen(nmea)-3]) * 16;
|
if (nmea[len-5] == '*') {
|
||||||
sum += parseHex(nmea[strlen(nmea)-2]);
|
uint16_t sum = parseHex(nmea[len-4]) * 16;
|
||||||
|
sum += parseHex(nmea[len-3]);
|
||||||
|
|
||||||
// check checksum
|
// check checksum
|
||||||
for (uint8_t i=2; i < (strlen(nmea)-4); i++) {
|
for (uint8_t i=1; i < (len-5); i++) {
|
||||||
sum ^= nmea[i];
|
sum ^= nmea[i];
|
||||||
}
|
}
|
||||||
if (sum != 0) {
|
if (sum != 0) {
|
||||||
|
|
@ -72,7 +75,7 @@ boolean Adafruit_GPS::parse(char *nmea) {
|
||||||
long minutes;
|
long minutes;
|
||||||
char degreebuff[10];
|
char degreebuff[10];
|
||||||
// look for a few common sentences
|
// look for a few common sentences
|
||||||
if (strstr(nmea, "$GPGGA")) {
|
if (strStartsWith(nmea, "$GPGGA")) {
|
||||||
// found GGA
|
// found GGA
|
||||||
char *p = nmea;
|
char *p = nmea;
|
||||||
// get time
|
// get time
|
||||||
|
|
@ -175,7 +178,7 @@ boolean Adafruit_GPS::parse(char *nmea) {
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (strstr(nmea, "$GPRMC")) {
|
if (strStartsWith(nmea, "$GPRMC")) {
|
||||||
// found RMC
|
// found RMC
|
||||||
char *p = nmea;
|
char *p = nmea;
|
||||||
|
|
||||||
|
|
@ -312,6 +315,10 @@ char Adafruit_GPS::read(void) {
|
||||||
// currentline[lineidx] = 0;
|
// currentline[lineidx] = 0;
|
||||||
// lineidx = 0;
|
// lineidx = 0;
|
||||||
// }
|
// }
|
||||||
|
currentline[lineidx++] = c;
|
||||||
|
if (lineidx >= MAXLINELENGTH)
|
||||||
|
lineidx = MAXLINELENGTH-1; // ensure there is someplace to put the next received character
|
||||||
|
|
||||||
if (c == '\n') {
|
if (c == '\n') {
|
||||||
currentline[lineidx] = 0;
|
currentline[lineidx] = 0;
|
||||||
|
|
||||||
|
|
@ -330,10 +337,6 @@ char Adafruit_GPS::read(void) {
|
||||||
recvdflag = true;
|
recvdflag = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
currentline[lineidx++] = c;
|
|
||||||
if (lineidx >= MAXLINELENGTH)
|
|
||||||
lineidx = MAXLINELENGTH-1;
|
|
||||||
|
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -477,23 +480,21 @@ uint8_t Adafruit_GPS::parseHex(char c) {
|
||||||
@brief Wait for a specified sentence from the device
|
@brief Wait for a specified sentence from the device
|
||||||
@param wait4me Pointer to a string holding the desired response
|
@param wait4me Pointer to a string holding the desired response
|
||||||
@param max How long to wait, default is MAXWAITSENTENCE
|
@param max How long to wait, default is MAXWAITSENTENCE
|
||||||
|
@param usingInterrupts True if using interrupts to read from the GPS (default is false)
|
||||||
@return True if we got what we wanted, false otherwise
|
@return True if we got what we wanted, false otherwise
|
||||||
*/
|
*/
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
boolean Adafruit_GPS::waitForSentence(const char *wait4me, uint8_t max) {
|
boolean Adafruit_GPS::waitForSentence(const char *wait4me, uint8_t max, boolean usingInterrupts) {
|
||||||
char str[20];
|
|
||||||
|
|
||||||
uint8_t i=0;
|
uint8_t i=0;
|
||||||
while (i < max) {
|
while (i < max) {
|
||||||
read();
|
if (!usingInterrupts)
|
||||||
|
read();
|
||||||
|
|
||||||
if (newNMEAreceived()) {
|
if (newNMEAreceived()) {
|
||||||
char *nmea = lastNMEA();
|
char *nmea = lastNMEA();
|
||||||
strncpy(str, nmea, 20);
|
|
||||||
str[19] = 0;
|
|
||||||
i++;
|
i++;
|
||||||
|
|
||||||
if (strstr(str, wait4me))
|
if (strStartsWith(nmea, wait4me))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -611,3 +612,20 @@ boolean Adafruit_GPS::wakeup(void) {
|
||||||
return false; // Returns false if not in standby mode, nothing to wakeup
|
return false; // Returns false if not in standby mode, nothing to wakeup
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**************************************************************************/
|
||||||
|
/*!
|
||||||
|
@brief Checks whether a string starts with a specified prefix
|
||||||
|
@param str Pointer to a string
|
||||||
|
@param prefix Pointer to the prefix
|
||||||
|
@return True if str starts with prefix, false otherwise
|
||||||
|
*/
|
||||||
|
/**************************************************************************/
|
||||||
|
static boolean strStartsWith(const char* str, const char* prefix)
|
||||||
|
{
|
||||||
|
while (*prefix) {
|
||||||
|
if (*prefix++ != *str++)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -148,7 +148,7 @@ class Adafruit_GPS {
|
||||||
uint8_t fixquality; ///< Fix quality (0, 1, 2 = Invalid, GPS, DGPS)
|
uint8_t fixquality; ///< Fix quality (0, 1, 2 = Invalid, GPS, DGPS)
|
||||||
uint8_t satellites; ///< Number of satellites in use
|
uint8_t satellites; ///< Number of satellites in use
|
||||||
|
|
||||||
boolean waitForSentence(const char *wait, uint8_t max = MAXWAITSENTENCE);
|
boolean waitForSentence(const char *wait, uint8_t max = MAXWAITSENTENCE, boolean usingInterrupts = false);
|
||||||
boolean LOCUS_StartLogger(void);
|
boolean LOCUS_StartLogger(void);
|
||||||
boolean LOCUS_StopLogger(void);
|
boolean LOCUS_StopLogger(void);
|
||||||
boolean LOCUS_ReadStatus(void);
|
boolean LOCUS_ReadStatus(void);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue