【问题标题】:Raspberry Pi: issue communicating via I2CRaspberry Pi:通过 I2C 进行通信的问题
【发布时间】: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


【解决方案1】:

你的问题解决了吗?

在将 Raspberry 配置为主服务器时,我也遇到了同样的问题。我认为它们是由于写入和读取操作之间缺少同步。 IE。我碰巧在写的时候可以尝试读。不幸的是,如果你有相反的想法,覆盆子也会做一些事情。这是因为 raspberry 是一个多线程平台,并且只有一个总线可用(据我所知)。

我通过在 raspberry 上使用 GPIO 引脚和在 picaxe 上使用一个 GPIO 引脚,解决了在 raspberry 和 picaxe 之间添加同步的问题。这样,只有在其他系统发出 ok 信号时才会读取(以及写入)。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多