diff --git a/adafruit_pm25/__init__.py b/adafruit_pm25/__init__.py index 1a9ca45..82af02d 100644 --- a/adafruit_pm25/__init__.py +++ b/adafruit_pm25/__init__.py @@ -82,7 +82,7 @@ def read(self): # print([hex(i) for i in self._buffer]) # check packet header - if (self._buffer[0] != 0x42) or (self._buffer[1] != 0x4D): + if not self._buffer[0:2] == b"BM": raise RuntimeError("Invalid PM2.5 header") # check frame length @@ -96,7 +96,6 @@ def read(self): raise RuntimeError("Invalid PM2.5 checksum") # unpack data - frame = struct.unpack(">HHHHHHHHHHHH", self._buffer[4:28]) ( self.aqi_reading["pm10 standard"], self.aqi_reading["pm25 standard"], @@ -110,6 +109,6 @@ def read(self): self.aqi_reading["particles 25um"], self.aqi_reading["particles 50um"], self.aqi_reading["particles 100um"], - ) = frame + ) = struct.unpack(">HHHHHHHHHHHH", self._buffer[4:28]) return self.aqi_reading diff --git a/adafruit_pm25/uart.py b/adafruit_pm25/uart.py index 1b91e05..f8569ae 100644 --- a/adafruit_pm25/uart.py +++ b/adafruit_pm25/uart.py @@ -77,6 +77,7 @@ def _read_into_buffer(self): remain = self._uart.read(31) if not remain or len(remain) != 31: 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[1:] = remain + + +# print([hex(i) for i in self._buffer])