ilaam
Posts: 30
Joined: Fri Mar 28, 2014 9:20 am

Read/Write data with SMBus- INPUT/output error

Wed May 21, 2014 3:04 pm

Hi,
I've been using SMBus on RPi for a while.
My program should send a number to the arduino (slave) then read from it. This should be done so many times and in a high frequence : it means every 5 to 10 ms the RPi writes then reads from the arduino.
That is my python side program:

Code: Select all

import smbus
bus= smbus.SMBus(1)
arduinoADDR = 0x12
While True:
bus.write_byte(arduinoADDR, 2)
time.sleep(0.005)
reponse=bus.read_byte(arduinoADDR);
time.sleep(0.001)
I put a sleep between the write and read instructions because Arduino takes a little time to prepare its data. The problem is that when I run the program, the communication turns well but just for one minute and then I get input/output error [errno = 5] on the write instruction.
I noticed that when I increase the duration of the sleep between write and read the communication lasts more but stops finally with the same error.
Does anyone know in general what can cause such error? What are the possible causes for it ?
And finally, is there a way to tell RPi to run again the program when the write is impossible (I mean tell him not to stop the execution but restart it)?
Thank you.

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Read/Write data with SMBus- INPUT/output error

Wed May 21, 2014 5:27 pm

Don't know what's causing the problem but for a quick fix try this

Code: Select all

import smbus
bus= smbus.SMBus(1)
arduinoADDR = 0x12
while True:
    try:
        bus.write_byte(arduinoADDR, 2)
    except:
        pass
    time.sleep(0.005)
    response=bus.read_byte(arduinoADDR);
    time.sleep(0.001)
Dave.
Apple say... Monkey do !!

User avatar
Douglas6
Posts: 4769
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Read/Write data with SMBus- INPUT/output error

Wed May 21, 2014 6:04 pm

Yup, I think you just have to expect IOErrors with I2C on the Pi. If the slave device is a little bit slow. it will tell the Pi to wait ("stretch the clock"), but the Pi doesn't like that. See this post on clock stretching. There are some things you can do:
1. Make the Arduino respond as quickly as possible. Prepare data in the main loop if possible, so the callback routine does nothing but a read/write (that may not be possible in your case).
2. You can slow the I2C clock rate down
3. Use the Try-Except as davef21370 suggests, and use the Except block to handle the missed read (ignore, repeat the previous value, etc.) or write (write again).

ilaam
Posts: 30
Joined: Fri Mar 28, 2014 9:20 am

Re: Read/Write data with SMBus- INPUT/output error

Thu May 22, 2014 9:06 am

Thank you so much guys ! It worked using the try except and I don't lose data.

Return to “Python”