From 3e59013b0e80f307d9874db01fbf1dcd8b6001dd Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Mon, 17 Aug 2020 22:43:08 +0100 Subject: [PATCH] Check complete SoF packet, use slice to assign directly to buffer --- adafruit_pm25/uart.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/adafruit_pm25/uart.py b/adafruit_pm25/uart.py index 08180fd..a71694a 100644 --- a/adafruit_pm25/uart.py +++ b/adafruit_pm25/uart.py @@ -69,17 +69,14 @@ def __init__(self, uart, reset_pin=None): def _read_into_buffer(self): while True: - b = self._uart.read(1) - if not b: - print(b) ### <<< debug + sof = self._uart.read(2) + if not sof: raise RuntimeError("Unable to read from PM2.5 (no start of frame)") - if b[0] == 0x42: + if sof == b'BM': break - self._buffer[0] = b[0] # first byte and start of frame + self._buffer[0:2] = sof # SoF bytes - remain = self._uart.read(31) - if not remain or len(remain) != 31: + data = self._uart.read(30) + if not data or not len(data) == 30: raise RuntimeError("Unable to read from PM2.5 (incomplete frame)") - for i in range(31): - self._buffer[i + 1] = remain[i] - # print([hex(i) for i in self._buffer]) + self._buffer[2:] = data