Skip to content

Commit 1d1c159

Browse files
committed
add crc check
1 parent f0c035f commit 1d1c159

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

adafruit_scd30.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,12 @@ def _read_register(self, reg_addr):
285285
self._buffer[1] = reg_addr & 0xFF
286286
with self.i2c_device as i2c:
287287
i2c.write(self._buffer, end=2)
288-
# separate readinto because the SCD30 wants an i2c stop before the read (non-repeated start)
289-
time.sleep(0.005) # min 3 ms delay
290-
with self.i2c_device as i2c:
291-
i2c.readinto(self._buffer, end=2)
292-
return unpack_from(">H", self._buffer)[0]
288+
# separate readinto because the SCD30 wants an i2c stop before the read (non-repeated start)
289+
time.sleep(0.005) # min 3 ms delay
290+
i2c.readinto(self._buffer, end=3)
291+
if not self._check_crc(self._buffer[:2], self._buffer[2]):
292+
raise RuntimeError("CRC check failed while reading data")
293+
return unpack_from(">H", self._buffer[0:2])[0]
293294

294295
def _read_data(self):
295296
self._send_command(_CMD_READ_MEASUREMENT)

0 commit comments

Comments
 (0)