I2C smbus2 block data - Raspberry Pi Forums
hello, i'm working on project exchanging data between rpi , arduino, , have been forced switch i2c communication protocol several reasons. i've had success using simple read , write requests pass single bytes of data , forth. i'm trying pass bunch of bytes, i'm trying use read_i2c_block_data smbus2 library. continue following error no matter try:
line 17 " x = bus.read_i2c_block_data(address, 0, numbytes)" line. i've read related there being nothing on other end of i2c bus, can't see problem is. i've tried several different ways of doing read_i2c_block_data , similar limited examples i've found. suggestion appreciated. many thanks.
here code pi: here arduino code:
code: select all
traceback (most recent call last): file "/home/pi/sketchbook/i2ctalk04/i2ctalk04.py", line 36, in <module> readbunchofdata(4) file "/home/pi/sketchbook/i2ctalk04/i2ctalk04.py", line 25, in readbunchofdata mydatahere = readblockdata(numbytes) file "/home/pi/sketchbook/i2ctalk04/i2ctalk04.py", line 17, in readblockdata x = bus.read_i2c_block_data(address, 0, numbytes) file "/usr/local/lib/python3.4/dist-packages/smbus2/smbus2.py", line 391, in read_i2c_block_data ioctl(self.fd, i2c_smbus, msg) oserror: [errno 121] remote i/o errorhere code pi:
code: select all
from smbus2 import smbus import time bus = smbus(1) address = 0x04 def writenumber(value): bus.write_byte(address, value) return -1 def readnumber(): number = bus.read_byte(address) return number def readblockdata(numbytes): res3 = [] x = bus.read_i2c_block_data(address, 0, numbytes) res3.extend(x) return res3 def readbunchofdata(numbytes): # send number of bytes requesting writenumber(numbytes) # receive data mydatahere = readblockdata(numbytes) # show we've got print ("we received ", numbytes, " of data") in range(0, numbytes): print (mydatahere[i]) while true: # now, hard-code 4 bytes of data print () print ("we requesting ", 4, " bytes of data") readbunchofdata(4) print () code: select all
#include <wire.h> #define slave_address 0x04 int number = 0; int datano = 0; void setup() { serial.begin(9600); wire.begin(slave_address); wire.onreceive(receivedata); wire.onrequest(senddata); serial.println("ready!"); } void loop() { doingsomething(10); } void doingsomething(int loops) { (int = 0; i<loops; i++) { serial.print("i'm doing else..."); serial.println(i); delay(50); } } void receivedata(int bytecount) { while(wire.available()) { number = wire.read(); if (datano == 0) { // datano, how many bytes send - hard-coded @ 4 datano = number; } else { // should not here serial.println("we got hole 1"); } } } void senddata() { byte output[] = {0x01,0x02,0x03,0x04}; // sample data testing wire.write(output, 4); datano = 0; }
normally pass 3 values bus.write_byte function, i2c address register address data
raspberrypi
Comments
Post a Comment