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

Conversation

Setcover
Copy link

@Setcover Setcover commented Feb 6, 2021

If the last byte of the checksum is 0x42 it can be mistaken for the first start-byte, causing a byte-shift which leads to checksum mismatch followed by no ouput (continuously in the HEPA edge-case).
Therefore checking the secondy start-byte is necessary.

The probability of this happening is 1 in 256 and in addition it has 100% probability if testing HEPA-filtered air with 0 particles.
Reason: With HEPA-filtered air all bytes are zero except:
start 1 + start 2 + frame-length + reserved
Decimal: 66 + 77 + 28 + 151 checksum: 256 + 66
Hex: 0x42 0x4d 0x00 0x1c ... 0x97 checksum: 0x01 0x42
Testing with hepa-filtered air, an arduino nano, software serial (2,3) the byte-shift with continuous no-output happened repeatedly after less than 10 measurement cycles. With disabled checksum-check the 15 uint16_t components of data then look like this:
Byte-shift.txt

---- considering & disregarding further edge-cases ----
It is not possible for byte 31 (second checksum-byte) to be 0x42 too, as the sum of the first 30 bytes is at most 30*255 which is less than 66 * 256 which is the smallest checksum that would cause byte 31 to be 0x42 (=66).
The sequence 0x42 0x42 0x42 0x4d with the last two being the start bytes is therefore impossible.

Though it is not impossible for the sequence 0x42 0x42 0x4d or 0x42 0x4d to appear at another position in the sequence it can only happen with:

  • particle count above 19712 = 77*256 and next finer particle count being equal to 66 mod 256
  • particle count of precisely 16973 = 66*256 + 77

which is both really unlikely and very likely not a stable condition with a low chance of multiple byte-shift leading to checksum-mismatch and continuous no-output.

…cksum mismatch and therefore no output.

If the last byte of the checksum is 0x42 it can be mistaken for the first start-byte, therefore checking the secondy start-byte is neccessary.
The probability of this happening is 1 in 256 and in addition it has 100% probability if testing hepa-filtered air with 0 particels.
Reason: All non-zero bytes remaining are: 
Decimal: 66 + 77 + 28 + 151 checksum: 256 + 66 
Hex: 0x42 0x4d 0x00 0x1c ... 0x97 checksum: 0x01 0x42

It is not possible for byte 31 (second checksum-byte) to be 0x42 too, as the sum of the first 30 bytes is at most 30*255 which is less than 66 * 256 which is the smallest checksum that would cause byte 31 to be 0x42 (=66). The sequence 0x42 0x42 0x42 0x4d with the last two being the start bytes is therefore impossible.

Though it is not impossible for the sequence 0x42 0x42 0x4d or 0x42 0x4d to appear at another position in the sequence it can only happen with particle count above 16896 = 66*256 and then this event is extremely unlikely itself and will extremely likely lead to a checksum mismatch therefore be discarded.
@Setcover Setcover changed the title Fixing 1 in 256 edge-case that can lead to byte shift followed by che… Fixing 1 in 256 edge-case that can lead to continous no-output. Feb 6, 2021
fixed formatting
this time using clang-format
@ladyada
Copy link
Member

ladyada commented Feb 6, 2021

@dherrada wanna test?

@Setcover Setcover changed the title Fixing 1 in 256 edge-case that can lead to continous no-output. Fixing 1 in 256 edge-case that can lead to continuous no-output. Feb 6, 2021
@ladyada ladyada requested a review from caternuson September 6, 2021 00:45
@caternuson
Copy link

Would switching to passive mode be an easier way to deal with this?

It looks like the issue relates to determining the start byte(s) from a continuous stream of serial data (active mode). Datasheet is a bit sparse, but it looks like passive mode is more of a request/response setup.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants