Skip to content

Commit af36390

Browse files
committed
For Serial, if assumeSuccess is true, also try to clear the buffers
For Serial, if the Serial port is congested, .begin can fail when the NAV RATE messages are delayed. This is usually a sign that the baud rate is too low. Clearing the serial buffer first seems to help.
1 parent 4b582c8 commit af36390

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/SparkFun_u-blox_GNSS_Arduino_Library.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,30 @@ bool SFE_UBLOX_GNSS::begin(Stream &serialPort, uint16_t maxWait, bool assumeSucc
514514
//New in v2.0: allocate memory for the file buffer - if required. (The user should have called setFileBufferSize already)
515515
createFileBuffer();
516516

517+
//Get rid of any stale serial data already in the processor's RX buffer
518+
while (_serialPort->available())
519+
_serialPort->read();
520+
521+
//If assumeSuccess is true, the user must really want begin to succeed. So, let's empty the module's serial transmit buffer too!
522+
//Keep discarding new serial data until we see a gap of 2ms - hopefully indicating that the module's TX buffer is empty.
523+
if (assumeSuccess)
524+
{
525+
unsigned long startTime = millis();
526+
unsigned long lastActivity = startTime;
527+
bool keepGoing = true;
528+
while (keepGoing && (millis() < (startTime + (unsigned long)maxWait)))
529+
{
530+
while (_serialPort->available()) // Discard any new data
531+
{
532+
_serialPort->read();
533+
lastActivity = millis();
534+
}
535+
536+
if (millis() > (lastActivity + (unsigned long)2)) // Check if we have seen no new data for at least 2ms
537+
keepGoing = false;
538+
}
539+
}
540+
517541
// Call isConnected up to three times - tests on the NEO-M8U show the CFG RATE poll occasionally being ignored
518542
bool connected = isConnected(maxWait);
519543

@@ -3642,7 +3666,7 @@ sfe_ublox_status_e SFE_UBLOX_GNSS::waitForACKResponse(ubxPacket *outgoingUBX, ui
36423666
packetAuto.classAndIDmatch = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED;
36433667

36443668
unsigned long startTime = millis();
3645-
while (millis() - startTime < maxTime)
3669+
while (millis() < (startTime + (unsigned long)maxTime))
36463670
{
36473671
if (checkUbloxInternal(outgoingUBX, requestedClass, requestedID) == true) //See if new data is available. Process bytes as they come in.
36483672
{
@@ -3781,7 +3805,7 @@ sfe_ublox_status_e SFE_UBLOX_GNSS::waitForACKResponse(ubxPacket *outgoingUBX, ui
37813805
} //checkUbloxInternal == true
37823806

37833807
delay(1); // Allow an RTOS to get an elbow in (#11)
3784-
} //while (millis() - startTime < maxTime)
3808+
} //while (millis() < (startTime + (unsigned long)maxTime))
37853809

37863810
// We have timed out...
37873811
// If the outgoingUBX->classAndIDmatch is VALID then we can take a gamble and return DATA_RECEIVED

src/SparkFun_u-blox_GNSS_Arduino_Library.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ class SFE_UBLOX_GNSS
599599
void setPacketCfgPayloadSize(size_t payloadSize); // Set packetCfgPayloadSize
600600

601601
//Begin communication with the GNSS. Advanced users can assume success if required. Useful if the port is already outputting messages at high navigation rate.
602+
//Begin will then return true if "signs of life" have been seen: reception of _any_ valid UBX packet or _any_ valid NMEA header.
602603
//By default use the default I2C address, and use Wire port
603604
bool begin(TwoWire &wirePort = Wire, uint8_t deviceAddress = 0x42, uint16_t maxWait = defaultMaxWait, bool assumeSuccess = false); //Returns true if module is detected
604605
//serialPort needs to be perviously initialized to correct baud rate

0 commit comments

Comments
 (0)