update timeout handle

This commit is contained in:
Phat Nguyen
2024-09-24 10:28:41 +07:00
parent 2a6fce674e
commit c1a4758c6c
2 changed files with 35 additions and 1 deletions

View File

@ -37,6 +37,33 @@ bool PMSBase::begin(HardwareSerial &serial) {
* @param 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;
while (serial.available()) {
/** Get value */
@ -108,7 +135,7 @@ void PMSBase::readPackage(HardwareSerial &serial) {
/** Check that sensor removed */
if (lastPackage) {
unsigned long ms = (unsigned long)(millis() - lastPackage);
if (ms >= 1500) {
if (ms >= READ_PACKGE_TIMEOUT) {
lastPackage = 0;
_connected = false;
}

View File

@ -40,6 +40,11 @@ public:
private:
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;
int failCount = 0;
@ -53,6 +58,8 @@ private:
unsigned long lastPackage = 0;
bool _connected;
unsigned long lastReadPackage = 0;
uint16_t pms_raw0_1;
uint16_t pms_raw2_5;
uint16_t pms_raw10;