Skip to content

Commit 9d8f124

Browse files
committed
Fix parser start issue. Remove extra variables and functions.
1 parent 835ddc3 commit 9d8f124

File tree

5 files changed

+174
-197
lines changed

5 files changed

+174
-197
lines changed

src/SparkFun_Unicore_GNSS_Arduino_Library.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,22 @@ bool UM980::isConnected()
105105
serialPrintln("UNLOG"); // Blindly tell unit to stop transmitting
106106

107107
// Wait until serial stops coming in
108+
uint16_t maxTime = 500;
109+
unsigned long startTime = millis();
108110
while (1)
109111
{
110112
delay(50);
113+
111114
if (serialAvailable() == 0)
112115
break;
113116
while (serialAvailable())
114117
serialRead();
115-
}
116118

117-
char response[200];
118-
119-
uint16_t maxResponseLength = sizeof(response);
119+
if(millis() - startTime > maxTime)
120+
return (false);
121+
}
120122

121-
// debugPrintf("UM980: Sending MODE query."); // Response to query should start with #
122-
if (sendQuery("MODE", response, &maxResponseLength) == UM980_RESULT_OK)
123+
if (sendQuery("MODE") == UM980_RESULT_OK)
123124
return (true);
124125
debugPrintf("UM980 failed to connect. Trying again.");
125126
delay(500);
@@ -628,15 +629,13 @@ bool UM980::sendCommand(const char *command, uint16_t maxWaitMs)
628629

629630
//'$' begins the responses to commands, ie 'MODE ROVER', ends with OK
630631
// Contains reponse for caller
631-
Um980Result UM980::sendQuery(const char *command, char *response, uint16_t *maxResponseLength, uint16_t maxWaitMs)
632+
Um980Result UM980::sendQuery(const char *command, uint16_t maxWaitMs)
632633
{
633634
Um980Result result;
634635

635-
// Serial.println("Sending query");
636-
637636
clearBuffer();
638637

639-
// Send command and check COMMAND OK response
638+
// Send command and check for OK response
640639
result = sendString(command, maxWaitMs);
641640
if (result != UM980_RESULT_OK)
642641
return (result);
@@ -676,11 +675,6 @@ Um980Result UM980::sendQuery(const char *command, char *response, uint16_t *maxR
676675
return (UM980_RESULT_OK);
677676
}
678677

679-
Um980Result UM980::sendQuery(const char *command, char *response, int *maxResponseLength, uint16_t maxWaitMs)
680-
{
681-
return (sendQuery(command, response, (uint16_t *)maxResponseLength, maxWaitMs));
682-
}
683-
684678
// Send a string to the UM980
685679
// Looks for a command response ('#' or '$')
686680
//'#' begins the responses to queries, ie 'MODE', ends with the result (ie MODE ROVER)
@@ -764,7 +758,7 @@ Um980Result UM980::checkCRC(char *response)
764758
break; // Exclude * from CRC
765759
}
766760

767-
calculatedCRC = crcTable[(calculatedCRC ^ response[packetLength]) & 0xFF] ^ (calculatedCRC >> 8);
761+
calculatedCRC = crc32Table[(calculatedCRC ^ response[packetLength]) & 0xFF] ^ (calculatedCRC >> 8);
768762
}
769763

770764
if (packetLength == strlen(response))

src/SparkFun_Unicore_GNSS_Arduino_Library.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,7 @@ class UM980
211211
void clearBuffer();
212212

213213
bool sendCommand(const char *command, uint16_t maxWaitMs = 1500);
214-
Um980Result sendQuery(const char *command, char *response, uint16_t *maxResponseLength, uint16_t maxWaitMs = 1500);
215-
Um980Result sendQuery(const char *command, char *response, int *maxResponseLength, uint16_t maxWaitMs = 1500);
214+
Um980Result sendQuery(const char *command, uint16_t maxWaitMs = 1500);
216215
Um980Result sendString(const char *command, uint16_t maxWaitMs = 1500);
217216
Um980Result checkCRC(char *response);
218217

src/parser.cpp

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
extern UM980 *ptrUM980; // Global pointer for external parser access into library class
55

66
// End of message handler
7-
// Crack the response into various endpoints
87
// If it's a response to a command, is it OK or BAD? $command,badResponse,response:
98
// PARSING FAILD NO MATCHING FUNC BADRESPONSE*40
109
// If it's Unicore binary, load into target variables If it's NMEA or RTCM, discard
@@ -38,37 +37,33 @@ void eomHandler(PARSE_STATE *parse)
3837
}
3938
else if (parse->messageType == SFE_SENTENCE_TYPE_UNICORE_POUND_RESPONSE)
4039
{
41-
Serial.println("Handling Unicore pound response");
40+
// Serial.println("Handling Unicore pound response");
4241

43-
Serial.printf("parse->nmeaMessageName: %s\r\n", (char *)parse->nmeaMessageName);
42+
// Serial.printf("parse->nmeaMessageName: %s\r\n", (char *)parse->nmeaMessageName);
4443

4544
// Does this response contain the command we are looking for?
4645
if (strcasecmp((char *)parse->nmeaMessageName, ptrUM980->commandName) == 0) // Found
47-
{
48-
Serial.println("Command name matches");
49-
5046
ptrUM980->commandResponse = UM980_RESULT_RESPONSE_COMMAND_OK;
51-
}
5247
}
5348
else if (parse->messageType == SFE_SENTENCE_TYPE_NMEA)
5449
{
55-
//Serial.println("NMEA handler");
50+
// Serial.println("NMEA handler");
5651

5752
// ID the NMEA message type
5853

5954
// Serial.print("NMEA Handler: ");
6055
// for (int x = 0; x < parse->length; x++)
6156
// Serial.write(parse->buffer[x]);
62-
//Serial.println();
57+
// Serial.println();
6358
}
6459
else if (parse->messageType == SFE_SENTENCE_TYPE_UNICORE_BINARY)
6560
{
6661
ptrUM980->unicoreHandler(parse->buffer, parse->length);
67-
//Serial.println("Unicore handler");
62+
// Serial.println("Unicore handler");
6863
}
6964
else if (parse->messageType == SFE_SENTENCE_TYPE_RTCM)
7065
{
71-
Serial.println("RTCM handler");
66+
// Serial.println("RTCM handler");
7267
}
7368
}
7469

@@ -99,7 +94,6 @@ void waitForPreamble(PARSE_STATE *parse, uint8_t data)
9994
// Use the same NMEA parser
10095

10196
parse->check = 0;
102-
parse->computeCrc = false;
10397
parse->nmeaMessageNameLength = 0;
10498
parse->state = PARSE_STATE_NMEA_FIRST_COMMA;
10599
return;
@@ -120,12 +114,7 @@ void waitForPreamble(PARSE_STATE *parse, uint8_t data)
120114
// |<------------------------ CRC -------------------------->|
121115
//
122116

123-
Serial.println("rtcm");
124-
125117
// Start the CRC with this byte
126-
parse->check = 0;
127-
parse->check = COMPUTE_CRC24Q(parse, data);
128-
parse->computeCrc = true;
129118
parse->state = PARSE_STATE_RTCM_LENGTH1;
130119
return;
131120

@@ -145,11 +134,13 @@ void waitForPreamble(PARSE_STATE *parse, uint8_t data)
145134
// |<------------------------ CRC --------------->|
146135
//
147136

148-
parse->length = 0;
149-
parse->buffer[parse->length++] = data; // This byte is part of the CRC
150137
parse->state = PARSE_STATE_UNICORE_SYNC2;
151138
return;
152139
}
140+
141+
// Preamble byte not found
142+
parse->length = 0;
143+
parse->state = PARSE_STATE_WAITING_FOR_PREAMBLE;
153144
}
154145

155146
// Read the message name
@@ -198,8 +189,6 @@ void nmeaChecksumByte2(PARSE_STATE *parse, uint8_t data)
198189
// Read the line termination
199190
void nmeaLineTermination(PARSE_STATE *parse, uint8_t data)
200191
{
201-
int checksum;
202-
203192
// We expect a \r\n termination, but may vary between receiver types
204193
if (data == '\r' || data == '\n')
205194
{
@@ -214,7 +203,7 @@ void nmeaLineTermination(PARSE_STATE *parse, uint8_t data)
214203
parse->length--;
215204

216205
// Convert the checksum characters into binary
217-
checksum = AsciiToNibble(parse->buffer[parse->nmeaLength - 2]) << 4;
206+
int checksum = AsciiToNibble(parse->buffer[parse->nmeaLength - 2]) << 4;
218207
checksum |= AsciiToNibble(parse->buffer[parse->nmeaLength - 1]);
219208

220209
parse->messageType = SFE_SENTENCE_TYPE_NMEA;
@@ -289,6 +278,8 @@ void nmeaLineTermination(PARSE_STATE *parse, uint8_t data)
289278
{
290279
// Serial.println("nmeaTermination Returning after EOM handler");
291280
eomHandler(parse);
281+
282+
parse->length = 0;
292283
parse->state = PARSE_STATE_WAITING_FOR_PREAMBLE; // Move to next state
293284
return;
294285
}
@@ -390,6 +381,18 @@ void unicoreBinaryReadLength(PARSE_STATE *parse, uint8_t data)
390381
// The overall message length is header (24) + data (expectedLength) + CRC (4)
391382
parse->bytesRemaining = um980HeaderLength + expectedLength + 4;
392383

384+
if (parse->bytesRemaining > PARSE_BUFFER_LENGTH)
385+
{
386+
Serial.println("Length overflow");
387+
388+
// Invalid length, place this byte at the beginning of the buffer
389+
parse->length = 0;
390+
parse->buffer[parse->length++] = data;
391+
392+
// Start searching for a preamble byte
393+
return waitForPreamble(parse, data);
394+
}
395+
393396
// Account for the bytes already received
394397
parse->bytesRemaining -= parse->length;
395398
parse->state = PARSE_STATE_UNICORE_READ_DATA; // Move on
@@ -439,7 +442,7 @@ uint32_t calculateCRC32(uint8_t *charBuffer, uint16_t bufferSize)
439442
{
440443
uint32_t crc = 0;
441444
for (uint16_t x = 0; x < bufferSize; x++)
442-
crc = crcTable32[(crc ^ charBuffer[x]) & 0xFF] ^ (crc >> 8);
445+
crc = crc32Table[(crc ^ charBuffer[x]) & 0xFF] ^ (crc >> 8);
443446
return crc;
444447
}
445448

@@ -452,7 +455,6 @@ void rtcmReadLength1(PARSE_STATE *parse, uint8_t data)
452455
// Invalid length, place this byte at the beginning of the buffer
453456
parse->length = 0;
454457
parse->buffer[parse->length++] = data;
455-
parse->computeCrc = false;
456458

457459
// Start searching for a preamble byte
458460
return waitForPreamble(parse, data);
@@ -467,21 +469,33 @@ void rtcmReadLength1(PARSE_STATE *parse, uint8_t data)
467469
void rtcmReadLength2(PARSE_STATE *parse, uint8_t data)
468470
{
469471
parse->bytesRemaining |= data;
472+
473+
// Check the length
474+
if (parse->bytesRemaining > PARSE_BUFFER_LENGTH)
475+
{
476+
Serial.println("RTCM length overflow");
477+
478+
// Invalid length, place this byte at the beginning of the buffer
479+
parse->length = 0;
480+
parse->buffer[parse->length++] = data;
481+
482+
// Start searching for a preamble byte
483+
return waitForPreamble(parse, data);
484+
}
485+
470486
parse->state = PARSE_STATE_RTCM_MESSAGE1;
471487
}
472488

473489
// Read the upper 8 bits of the message number
474490
void rtcmReadMessage1(PARSE_STATE *parse, uint8_t data)
475491
{
476-
parse->message = data << 4;
477492
parse->bytesRemaining -= 1;
478493
parse->state = PARSE_STATE_RTCM_MESSAGE2;
479494
}
480495

481496
// Read the lower 4 bits of the message number
482497
void rtcmReadMessage2(PARSE_STATE *parse, uint8_t data)
483498
{
484-
parse->message |= data >> 4;
485499
parse->bytesRemaining -= 1;
486500
parse->state = PARSE_STATE_RTCM_DATA;
487501
}
@@ -495,7 +509,6 @@ void rtcmReadData(PARSE_STATE *parse, uint8_t data)
495509
// Wait until all the data is received
496510
if (parse->bytesRemaining <= 0)
497511
{
498-
// parse->rtcmCrc = parse->check & 0x00ffffff;
499512
parse->bytesRemaining = 3;
500513
parse->state = PARSE_STATE_RTCM_CRC;
501514
}
@@ -511,28 +524,37 @@ void rtcmReadCrc(PARSE_STATE *parse, uint8_t data)
511524
if (parse->bytesRemaining > 0)
512525
return;
513526

514-
// Update the maximum message length
515-
if (parse->length > parse->maxLength)
527+
// Get CRC
528+
uint32_t sentenceCRC = (parse->buffer[parse->length - 3] << 16) | (parse->buffer[parse->length - 2] << 8) |
529+
(parse->buffer[parse->length - 1] << 0);
530+
531+
// Calculate CRC
532+
parse->check = 0;
533+
for (int x = 0; x < parse->length - 3; x++) // Exclude CRC
516534
{
517-
parse->maxLength = parse->length;
518-
// debugPrintf("RTCM parser error maxLength: %d bytes\r\n", parse->maxLength);
535+
parse->check = ((parse)->check << 8) ^ crc24q[parse->buffer[x] ^ (((parse)->check >> 16) & 0xff)];
519536
}
520-
521537
parse->check &= 0x00ffffff;
522538

523539
// Process the message if CRC is valid
524-
if (parse->check == 0)
540+
if (parse->check == sentenceCRC)
525541
{
542+
//Serial.printf("RTCM CRC Good length: %d\r\n", parse->length);
526543
parse->messageType = SFE_SENTENCE_TYPE_RTCM;
527544
eomHandler(parse);
528545
}
529546
else
530547
{
531-
Serial.println("RTCM CRC failed");
548+
Serial.printf("RTCM CRC failed length: %d sentence CRC: 0x%02X calc CRC: 0x%02X\r\n", parse->length,
549+
sentenceCRC, parse->check);
550+
for (int x = 0; x < parse->length; x++)
551+
{
552+
Serial.write(parse->buffer[x]);
553+
}
554+
Serial.println();
532555
}
533556

534557
// Search for another preamble byte
535558
parse->length = 0;
536-
parse->computeCrc = false;
537559
parse->state = PARSE_STATE_WAITING_FOR_PREAMBLE;
538560
}

0 commit comments

Comments
 (0)