arduino / arduino/ArduinoCore-avr

Wire.requestFrom returns the number of bytes requested, regardless of what is sent

Open
#171 6 comments 0 reactions 1 assignee Claimed by @facchinm View on GitHub
Dominant language
C
Stars
1.5k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

Example slave:

```
#include
void setup ()
{
Wire.begin (42);
Wire.onRequest (requestEvent); // interrupt handler for when data is wanted
} // end of setup
void requestEvent ()
{
Wire.write ("ABCDE", 5);
} // end of receiveEvent
void loop() { }
```

This slave always returns 5 bytes.

- - -

Example master:

```
#include
void setup ()
{
Wire.begin ();
Serial.begin (115200); // start serial for output
} // end of setup
size_t count;
void loop()
{
count++;
if (count > 32)
while (true) {}
byte returnedCount = Wire.requestFrom (42, count);
Serial.println (int (returnedCount));
} // end of loop
```

This master requests from 1 to 32 bytes from the slave.

- - -

Results:

```

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
```

The master always gets what it **requested**, not 5, which is what the slave **sent**.

- - -

The reference page http://arduino.cc/en/Reference/WireRequestFrom says:

> Returns
>
> byte : the number of bytes returned from the slave device

It is not returning the number of bytes returned from the slave device, it is returning the number of bytes **requested** from the slave device.

Also relevant: https://github.com/arduino/Arduino/issues/1311

- - -

I doubt it is even possible for the master to obtain the number of bytes sent by the slave, as the **master** controls the clock, and thus the slave cannot send a "stop condition" when there is no more to send.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.