Skip to content

Commit

Permalink
uart fixes (device not readable fix)
Browse files Browse the repository at this point in the history
  • Loading branch information
fivesixzero committed Nov 29, 2020
1 parent 689de27 commit 1d8dbb2
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions adafruit_pm25/uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
PLANTOWER_CMD_SLEEP = b"\xE4\x00\x00"
PLANTOWER_CMD_WAKEUP = b"\xE4\x00\x01"

UART_RETRY_COUNT = 3
UART_RETRY_COUNT = 5
FRAME_RETRY_COUNT = 3
MAX_FRAME_SIZE = 32


Expand Down Expand Up @@ -203,6 +204,7 @@ def _read_uart(self):
Ignores bytes that are not frame headers to avoid reading in frames mid-stream
"""
uart_timeouts = 0
error_count = 0
first_bytes_tried = 0
while True:
Expand Down Expand Up @@ -241,12 +243,15 @@ def _read_uart(self):
# First bit isn't a header high bit, ignore and retry until we get a header bit
first_bytes_tried += 1
else:
# If we didn't get a byte during our read, that's fine, just move on
pass
# If we didn't get a byte during our read, increment timeouts and move on
uart_timeouts += 1

if error_count >= UART_RETRY_COUNT:
if error_count >= FRAME_RETRY_COUNT:
raise RuntimeError("Frame error count exceded retry threshold")

if uart_timeouts >= UART_RETRY_COUNT:
raise RuntimeError("Unable to read from PM2.5")

if first_bytes_tried > MAX_FRAME_SIZE:
# If we haven't found a frame header then increment count and try again
error_count += 1

0 comments on commit 1d8dbb2

Please sign in to comment.