如何解决如何将此代码从 Uart 更改为 I2c 设备
我有 PMS5003 空气质量传感器的代码,可以在 Raspberry Pi 上通过 uart 正常工作 我现在有这个传感器的 I2C 版本,我想让代码通过 I2C 而不是串行端口工作 我对 Python 还是很陌生,不知道该怎么做 有人可以帮忙吗?
这是相关的代码部分
import struct
import time
import pdb
import threading
import logging
import sys
if sys.platform != "win32":
import RPi.GPIO as GPIO
__version__ = '0.0.1'
PMS5003_SOF = bytearray(b'\x42\x4d')
MIN_FRAME_LENGTH = 28
class ChecksumMismatchError(RuntimeError):
pass
class ReadTimeoutError(RuntimeError):
pass
class SerialTimeoutError(RuntimeError):
pass
class PMS5003Data():
def __init__(self,raw_data):
self.raw_data = raw_data
self.data = struct.unpack(">HHHHHHHHHHHHHH",raw_data)
self.checksum = self.data[13]
def __str__(self):
return self.__repr__()
class PMS5003():
def __init__(self,serial,pin_enable=22,pin_reset=27):
"""pin_enable and pin_reset are really GPIO ids,not pin numbers on the Pi header."""
self._serial = serial
self._pin_enable = pin_enable
self._pin_reset = pin_reset
self.data = None
self.setup()
def setup(self):
if (self._pin_enable is not None) and (self._pin_reset is not None):
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(self._pin_enable,GPIO.OUT,initial=GPIO.LOW)
GPIO.setup(self._pin_reset,initial=GPIO.LOW)
def getData(self):
"""Get most recently retrieved consistent set of data."""
#self.lock.acquire()
data = self.data
#self.lock.release()
return data
我不需要 I2C 版本上的复位和启用引脚,因为这些功能没有被使用
谁能告诉我我需要做什么才能使用 SMBUS2 在 Pi 上运行它?
我已经有另一台设备在使用 SMBUS2
我想我需要添加这样的东西
import smbus2
import struct
import time
class PMSA003
"""Python I2C driver for the PMSA003 Air Quality Sensor."""
def __init__(self):
self._i2c_addr = 0x0x12 #I2c Address used for this device
self._i2c = smbus2.SMBus(1)
谁能告诉我如何让设备使用 I2c 而不是它使用的串行读取?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。