I have this code to request data from I2C slave, the problem is that Wire.available() returns always 10 no matter how many data is actually sent by slave. If I change 10 in “Wire.requestFrom(_address, 10);” to something else, say 20, Wire.available() returns me 20.
What is wrong? Thanks ahead.
byte p = 0;
A small note: it’s checked that slave sends 4 bytes of data.
Perhaps a silly question, but, if you know that the slave responds with 4 bytes, why are you asking for 10?
because I have many slaves. And a different slave might return 10 or 20 bytes. Say it's a char* array containing text or something.
In official arduino example they say // slave may send less than requested
Soooo... what's the use of Wire.available() ?
Wire.requestFrom(2, 6); // request 6 bytes from slave device #2
while(Wire.available()) // slave may send less than requested
char c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
The wire object gets the bytes from the slave device, and it takes care
of the timing and handshaking required, so you don't have to worry about it.
Your code then reads the bytes from the wire object. The Wire.available() function
tells you how many bytes the Wire object got from the slave device, so you can then get
them from the Wire object.