【发布时间】:2017-03-26 20:43:14
【问题描述】:
我正在尝试使用 I2C 接口创建一个简单的项目。为此,我在 Arduino 中创建了一个始终发送单字节的草图:
#include <Wire.h>
void setup() {
Wire.begin(8);
Wire.onRequest(requestEvent);
}
void loop() {
delay(100);
}
void requestEvent() {
Wire.write(0x11);
}
在 Raspberry Pi 上有一个 Python 脚本:
#!/usr/bin/env python3
import smbus
import time
bus = smbus.SMBus(1)
while True:
try:
data = bus.read_byte_data(0x8, 0)
print(data)
except Exception as e:
print(e)
time.sleep(1)
这是它的输出:
17
17
17
17
17
17
17
[Errno 5] Input/output error
[Errno 5] Input/output error
[Errno 5] Input/output error
[Errno 5] Input/output error
[Errno 5] Input/output error
[Errno 5] Input/output error
[Errno 5] Input/output error
[Errno 5] Input/output error
[Errno 5] Input/output error
[Errno 5] Input/output error
17
17
17
17
17
[Errno 5] Input/output error
[Errno 5] Input/output error
17
17
我想弄清楚为什么在某些随机时间点 I2C 返回错误而不是返回数据?没有硬件变化,RPi 上没有运行其他任何东西,实际上没有任何变化,但 I2C 停止工作。
有什么想法吗?
【问题讨论】:
-
I2C 通常只有一个主机,寻址的设备是从机——通常你编程的东西是主机。在主控向它们发送一个主控期望响应的命令之前,从属永远不会发送。如果 Arduino 正在发送,Pi 需要作为从机工作,不确定这是默认设置。
标签: python linux arduino raspberry-pi i2c