【发布时间】:2021-07-17 23:59:46
【问题描述】:
如何解决此运行时错误? 如果出现错误,我希望它跳过阅读并继续程序,但它会停止程序
我有 Adafruit SCD-30 Co2 传感器,使用他们的示例代码读取数据时出现此错误
Traceback (most recent call last):
File "pm.py", line 43, in <module>
print("CO2:", scd.CO2, "PPM")
File "/usr/local/lib/python3.7/dist-packages/adafruit_scd30.py", line 183, in CO2
self._read_data()
File "/usr/local/lib/python3.7/dist-packages/adafruit_scd30.py", line 246, in _read_data
raise RuntimeError("CRC check failed while reading data")
RuntimeError: CRC check failed while reading data
这是失败的那一行
print("CO2:", scd.CO2, "PPM")
这是他们代码的 crc 检查部分
def _read_data(self):
self._send_command(_CMD_READ_MEASUREMENT)
with self.i2c_device as i2c:
i2c.readinto(self._buffer)
crcs_good = True
for i in range(0, 18, 3):
crc_good = self._check_crc(self._buffer[i : i + 2], self._buffer[i + 2])
if crc_good:
continue
crcs_good = False
if not crcs_good:
raise RuntimeError("CRC check failed while reading data")
self._co2 = unpack(">f", self._buffer[0:2] + self._buffer[3:5])[0]
self._temperature = unpack(">f", self._buffer[6:8] + self._buffer[9:11])[0]
self._relative_humidity = unpack(
">f", self._buffer[12:14] + self._buffer[15:17]
)[0]
有没有办法告诉它做这样的事情
if not crcs_good:
skip reading and wait for the next one
【问题讨论】:
-
你看过ty,除了pattern吗?
-
是的,它仍然可以。
标签: python-3.x error-handling runtime-error crc pi