Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing 1 in 256 edge-case that can lead to continuous no-output. #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions Adafruit_PM25AQI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,31 @@ bool Adafruit_PM25AQI::read(PM25_AQI_Data *data) {
return false;
}
}
if (serial_dev->peek() != 0x42) {
serial_dev->read();
if (serial_dev->read() != 0x42) { // read assuming you got the first byte
return false;
}
// Now read all 32 bytes
if (serial_dev->available() < 32) {
// now catching 1 in 256 edge-case where last byte of checksum is 0x42 too,
// being mistaken for first start byte, leading to byte shift and checksum
// mismatch
if (serial_dev->peek() ==
0x42) { // peek if next byte might match first start byte
serial_dev->read(); // if yes last assumption was wrong, read again
// assuming you now got the start
}
if (serial_dev->read() != 0x4d) { // read & check the second byte
return false;
}
serial_dev->readBytes(buffer, 32);
// Now the first two bytes are guaranteed to be 0x42 0x4d but have already
// been read
buffer[0] = 0x42;
buffer[1] = 0x4d;
// Now read remaining 30 bytes
if (serial_dev->available() < 30) {
return false;
}
for (uint8_t i = 2; i < 32; i++) {
buffer[i] = serial_dev->read();
}
} else {
return false;
}
Expand Down