简体   繁体   English

Raspberry Pi:通过I2C进行通信时出现问题

[英]Raspberry Pi: issue communicating via I2C

I'm trying to create a simple project with I2C interface. 我正在尝试使用I2C接口创建一个简单的项目。 For that I've create a sketch in Arduino that always sends single byte: 为此,我在Arduino中创建了一个始终发送单个字节的草图:

#include <Wire.h>

void setup() {
  Wire.begin(8);
  Wire.onRequest(requestEvent);
}

void loop() {
  delay(100);
}

void requestEvent() {
  Wire.write(0x11);
}

On Raspberry Pi there's a Python script: 在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)

And here's its output: 这是它的输出:

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

What I'm trying to figure out is why at some random points in time I2C returns error instead of returning data? 我要弄清楚的是为什么在某些随机时间点I2C返回错误而不是返回数据? No hardware changes, nothing else is running on RPi, literally nothing is changing, but I2C stops working. 无需更改硬件,RPi上没有其他任何东西运行,实际上什么也没有改变,但是I2C停止工作。

Any ideas? 有任何想法吗?

Have you solved the problem? 你解决了这个问题吗?

I had the same problem also when configuring Raspberry as a master. 将Raspberry配置为主服务器时,我也遇到相同的问题。 I think they are due to the missing of synchronization between writing and reading operations. 我认为它们是由于缺少读写操作之间的同步所致。 Ie it my happen that reading can be attempted when writing is being done. 也就是说,我碰巧在完成书写时可以尝试阅读。 Unfortunately raspberry does always something also if you think the contrary. 不幸的是,如果您认为相反的话,覆盆子也会做一些事情。 this is because raspberry is a multithread platform and only one bus is available(at my knowledge). 这是因为raspberry是一个多线程平台,并且只有一个总线可用(据我所知)。

I solved the problem adding a synchronization between raspberry and picaxe by using a GPIO pin on raspberry and one on picaxe. 我通过使用树莓派上的GPIO引脚和picaxe上的GPIO引脚解决了在树莓派和picaxe之间添加同步的问题。 In this way reading (as well as writing) my happen only when there is an ok signal from the other system. 这样,只有在另一个系统发出确定的信号时,我才会读(写)。

I hope this delay can be useful also with such a delay. 我希望这种延迟在这种延迟下也能有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM