mirror of
https://github.com/airgradienthq/arduino.git
synced 2025-07-28 16:07:16 +02:00
update timeout handle
This commit is contained in:
@ -37,6 +37,33 @@ bool PMSBase::begin(HardwareSerial &serial) {
|
|||||||
* @param serial
|
* @param serial
|
||||||
*/
|
*/
|
||||||
void PMSBase::readPackage(HardwareSerial &serial) {
|
void PMSBase::readPackage(HardwareSerial &serial) {
|
||||||
|
/** If readPackage has process as period larger than READ_PACKAGE_TIMEOUT,
|
||||||
|
* should be clear the lastPackage and readBufferIndex */
|
||||||
|
if (lastReadPackage) {
|
||||||
|
unsigned long ms = (unsigned long)(millis() - lastReadPackage);
|
||||||
|
if (ms >= READ_PACKGE_TIMEOUT) {
|
||||||
|
/** Clear buffer */
|
||||||
|
readBufferIndex = 0;
|
||||||
|
|
||||||
|
/** Disable check read package timeout */
|
||||||
|
lastPackage = 0;
|
||||||
|
|
||||||
|
Serial.println("Last process timeout, clear buffer and last handle package");
|
||||||
|
}
|
||||||
|
|
||||||
|
lastReadPackage = millis();
|
||||||
|
if (!lastReadPackage) {
|
||||||
|
lastReadPackage = 1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
lastReadPackage = millis();
|
||||||
|
if (!lastReadPackage) {
|
||||||
|
lastReadPackage = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Count to call delay() to release the while loop MCU resource for avoid the
|
||||||
|
* watchdog time reset */
|
||||||
uint8_t delayCount = 0;
|
uint8_t delayCount = 0;
|
||||||
while (serial.available()) {
|
while (serial.available()) {
|
||||||
/** Get value */
|
/** Get value */
|
||||||
@ -108,7 +135,7 @@ void PMSBase::readPackage(HardwareSerial &serial) {
|
|||||||
/** Check that sensor removed */
|
/** Check that sensor removed */
|
||||||
if (lastPackage) {
|
if (lastPackage) {
|
||||||
unsigned long ms = (unsigned long)(millis() - lastPackage);
|
unsigned long ms = (unsigned long)(millis() - lastPackage);
|
||||||
if (ms >= 1500) {
|
if (ms >= READ_PACKGE_TIMEOUT) {
|
||||||
lastPackage = 0;
|
lastPackage = 0;
|
||||||
_connected = false;
|
_connected = false;
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,11 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
static const uint8_t package_size = 32;
|
static const uint8_t package_size = 32;
|
||||||
|
|
||||||
|
/** In normal package interval is 200-800ms, In case small changed on sensor
|
||||||
|
* it's will interval reach to 2.3sec
|
||||||
|
*/
|
||||||
|
const uint16_t READ_PACKGE_TIMEOUT = 3000; /** ms */
|
||||||
const int failCountMax = 10;
|
const int failCountMax = 10;
|
||||||
int failCount = 0;
|
int failCount = 0;
|
||||||
|
|
||||||
@ -53,6 +58,8 @@ private:
|
|||||||
unsigned long lastPackage = 0;
|
unsigned long lastPackage = 0;
|
||||||
bool _connected;
|
bool _connected;
|
||||||
|
|
||||||
|
unsigned long lastReadPackage = 0;
|
||||||
|
|
||||||
uint16_t pms_raw0_1;
|
uint16_t pms_raw0_1;
|
||||||
uint16_t pms_raw2_5;
|
uint16_t pms_raw2_5;
|
||||||
uint16_t pms_raw10;
|
uint16_t pms_raw10;
|
||||||
|
Reference in New Issue
Block a user