【问题标题】:how can I fix this runtime error when doing crc checking python 3 scd-30 co2 Sensor在进行 crc 检查 python 3 scd-30 co2 Sensor 时如何解决此运行时错误
【发布时间】: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


【解决方案1】:

我遇到了同样的问题。我终于发现我需要在我的树莓派上降低我的 I2C 上的时钟速度。不确定您是否也在使用 pi,但这里有一个链接,可以很好地说明如何做到这一点 https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/i2c-clock-stretching 。我正在以 10khz 或 10000hz 运行传感器,并且从那以后我的代码没有出现 crc 错误崩溃。

【讨论】:

猜你喜欢
  • 2011-02-25
  • 2021-10-20
  • 2020-01-19
  • 2023-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多