我在这里重复一条问题的简化版本,我没有得到任何答复:
我在我的RaspberryPi 3Python3.4上安装了wiringpi。
我可以在raspberrypi命令行下运行连接命令(比如i2cdetect),但是我不能让它在Python shell中工作。
我尝试了各种安装和导入,但它似乎不能被Python shell识别。
有人能帮上忙吗?
发布于 2018-08-01 12:34:52
经过两天的谷歌搜索和测试,我找到了最终的解决方案,可以看到here。
问题是Python3似乎不支持标准的Rasbian I2C工具SMBus (反之亦然),并且需要进行复杂的调整才能使其在Python3下运行,如所附链接所示。
一旦我完成了调整,一切都运行得很好。对于各种数据类型,SMBus具有相对大量的I2C命令。以下是来自SMBus站点的代码示例:
#!/usr/bin/python
import smbus
bus = smbus.SMBus(1) # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)
DEVICE_ADDRESS = 0x15 #7 bit address (will be left shifted to add the read write bit)
DEVICE_REG_MODE1 = 0x00
DEVICE_REG_LEDOUT0 = 0x1d
#Write a single register
bus.write_byte_data(DEVICE_ADDRESS, DEVICE_REG_MODE1, 0x80)
#Write an array of registers
ledout_values = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
bus.write_i2c_block_data(DEVICE_ADDRESS, DEVICE_REG_LEDOUT0, ledout_values)
bus.read_byte(DEVICE_ADDRESS)非常感谢JTech Engineering的贡献!
https://stackoverflow.com/questions/51578959
复制相似问题