diff --git a/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp b/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp index 3170beb..0cba6fb 100644 --- a/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp +++ b/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp @@ -514,6 +514,30 @@ bool SFE_UBLOX_GNSS::begin(Stream &serialPort, uint16_t maxWait, bool assumeSucc //New in v2.0: allocate memory for the file buffer - if required. (The user should have called setFileBufferSize already) createFileBuffer(); + //Get rid of any stale serial data already in the processor's RX buffer + while (_serialPort->available()) + _serialPort->read(); + + //If assumeSuccess is true, the user must really want begin to succeed. So, let's empty the module's serial transmit buffer too! + //Keep discarding new serial data until we see a gap of 2ms - hopefully indicating that the module's TX buffer is empty. + if (assumeSuccess) + { + unsigned long startTime = millis(); + unsigned long lastActivity = startTime; + bool keepGoing = true; + while (keepGoing && (millis() < (startTime + (unsigned long)maxWait))) + { + while (_serialPort->available()) // Discard any new data + { + _serialPort->read(); + lastActivity = millis(); + } + + if (millis() > (lastActivity + (unsigned long)2)) // Check if we have seen no new data for at least 2ms + keepGoing = false; + } + } + // Call isConnected up to three times - tests on the NEO-M8U show the CFG RATE poll occasionally being ignored bool connected = isConnected(maxWait); @@ -3642,7 +3666,7 @@ sfe_ublox_status_e SFE_UBLOX_GNSS::waitForACKResponse(ubxPacket *outgoingUBX, ui packetAuto.classAndIDmatch = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED; unsigned long startTime = millis(); - while (millis() - startTime < maxTime) + while (millis() < (startTime + (unsigned long)maxTime)) { if (checkUbloxInternal(outgoingUBX, requestedClass, requestedID) == true) //See if new data is available. Process bytes as they come in. { @@ -3781,7 +3805,7 @@ sfe_ublox_status_e SFE_UBLOX_GNSS::waitForACKResponse(ubxPacket *outgoingUBX, ui } //checkUbloxInternal == true delay(1); // Allow an RTOS to get an elbow in (#11) - } //while (millis() - startTime < maxTime) + } //while (millis() < (startTime + (unsigned long)maxTime)) // We have timed out... // If the outgoingUBX->classAndIDmatch is VALID then we can take a gamble and return DATA_RECEIVED diff --git a/src/SparkFun_u-blox_GNSS_Arduino_Library.h b/src/SparkFun_u-blox_GNSS_Arduino_Library.h index 2f23313..4b3194b 100644 --- a/src/SparkFun_u-blox_GNSS_Arduino_Library.h +++ b/src/SparkFun_u-blox_GNSS_Arduino_Library.h @@ -599,6 +599,7 @@ public: void setPacketCfgPayloadSize(size_t payloadSize); // Set packetCfgPayloadSize //Begin communication with the GNSS. Advanced users can assume success if required. Useful if the port is already outputting messages at high navigation rate. + //Begin will then return true if "signs of life" have been seen: reception of _any_ valid UBX packet or _any_ valid NMEA header. //By default use the default I2C address, and use Wire port bool begin(TwoWire &wirePort = Wire, uint8_t deviceAddress = 0x42, uint16_t maxWait = defaultMaxWait, bool assumeSuccess = false); //Returns true if module is detected //serialPort needs to be perviously initialized to correct baud rate