Raspberry Pi:通过I2C进行通信

时间:2017-03-26 20:43:14

标签: python linux arduino raspberry-pi i2c

我试图用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停止工作。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

你解决了这个问题吗?

将Raspberry配置为主服务器时,我也遇到了同样的问题。我认为它们是由于缺少写入和读取操作之间的同步。即我在写作完成时可以尝试阅读。不幸的是,如果您认为相反,覆盆子总会有所作为。这是因为raspberry是一个多线程平台,只有一条总线可用(据我所知)。

我通过在树莓上使用GPIO引脚和在picaxe上使用GPIO引脚解决了在覆盆子和picaxe之间添加同步的问题。通过这种方式,只有在来自其他系统的信号正确时才会读取(以及写入)。

我希望这种延迟对这种延迟也有用。

相关问题