@@ -583,16 +583,8 @@ byte MHZ19::read(byte inBytes[MHZ19_DATA_LEN], Command_Type commandnumber)
583
583
this ->errorCode = RESULT_TIMEOUT;
584
584
585
585
/* clear incomplete 9 byte values, limit is finite */
586
- inBytes[1 ] = mySerial->available ();
587
- for (uint8_t x = 0 ; x < inBytes[1 ]; x++)
588
- {
589
- inBytes[0 ] = mySerial->read ();
590
- #if defined (ESP32) && (MHZ19_ERRORS)
591
- ESP_LOGW (TAG_MHZ19, " Clearing Byte: %d" , inBytes[0 ]);
592
- #elif MHZ19_ERRORS
593
- Serial.print (" !Warning: Clearing Byte: " ); Serial.println (inBytes[0 ]);
594
- #endif
595
- }
586
+ cleanUp (mySerial->available ());
587
+
596
588
// return error condition
597
589
return RESULT_TIMEOUT;
598
590
}
@@ -612,7 +604,11 @@ byte MHZ19::read(byte inBytes[MHZ19_DATA_LEN], Command_Type commandnumber)
612
604
613
605
/* construct error code */
614
606
if (inBytes[0 ] != this ->storage .constructedCommand [0 ] || inBytes[1 ] != this ->storage .constructedCommand [2 ])
607
+ {
608
+ /* clear rx buffer for deysnc correction */
609
+ cleanUp (mySerial->available ());
615
610
this ->errorCode = RESULT_MATCH;
611
+ }
616
612
617
613
/* if error has been assigned */
618
614
if (this ->errorCode == RESULT_NULL)
@@ -625,6 +621,20 @@ byte MHZ19::read(byte inBytes[MHZ19_DATA_LEN], Command_Type commandnumber)
625
621
return this ->errorCode ;
626
622
}
627
623
624
+ void MHZ19::cleanUp (uint8_t cnt)
625
+ {
626
+ uint8_t eject = 0 ;
627
+ for (uint8_t x = 0 ; x < cnt; x++)
628
+ {
629
+ eject = mySerial->read ();
630
+ #if defined (ESP32) && (MHZ19_ERRORS)
631
+ ESP_LOGW (TAG_MHZ19, " Clearing Byte: %d" , eject);
632
+ #elif MHZ19_ERRORS
633
+ Serial.print (" !Warning: Clearing Byte: " ); Serial.println (eject);
634
+ #endif
635
+ }
636
+ }
637
+
628
638
void MHZ19::handleResponse (Command_Type commandtype)
629
639
{
630
640
if (this ->storage .constructedCommand [2 ] == Commands[RAWCO2]) // compare commands byte
0 commit comments