我在这里重复一个我没有收到回复的问题的简化版本:
我在我的 RaspberryPi 3 Python 3.4 上安装了wiringpi。
我可以在 raspberrypi 命令行下运行接线命令(如 i2cdetect),但我无法使其在 Python shell 中工作。
我尝试了各种安装和导入,但它似乎没有成为 Python shell 识别的模块。
任何人都可以帮忙吗?
问问题
265 次
1 回答
0
经过 2 天的谷歌搜索和测试,我找到了最终解决方案,如下所示。
问题是 Python 3 似乎不支持SMBus(反之亦然),这是标准的 Rasbian I2C 工具,并且需要进行复杂的调整才能使其在 Python 3 下运行,如所附链接所示。 一旦我完成了调整,一切都运行得很好。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的贡献!
于 2018-08-01T04:34:51.730 回答