# Wire write buffer duplicating problem

**URL:** <https://forum.arduino.cc/t/wire-write-buffer-duplicating-problem/959183>\
**Category:** Interfacing w/ Software on the Computer\
**Created:** [February 14, 2022, 8:08pm UTC](https://forum.arduino.cc/t/wire-write-buffer-duplicating-problem/959183 "2022-02-14T20:08:59Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter1ainternet](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@peter1ainternet](https://forum.arduino.cc/u/peter1ainternet)\
**Post date:** [February 14, 2022, 8:08pm UTC](https://forum.arduino.cc/t/wire-write-buffer-duplicating-problem/959183/1 "2022-02-14T20:08:59Z")

</div>

I want to send buffer data it works only first time after first time of sending / receiving it starts teasing and displays some repetitive numbers.

Transmitter use this code:

```cpp
uint8_t val[10] = { 32, 33, 34, 35, 36, 37, 38, 39, 40, 41 };
WIRE.flush();
WIRE.beginTransmission(device_address_sspin);
WIRE.write((reg_addr<<3 | channel<<1));
WIRE.write(val, 10);
WIRE.endTransmission(1);

```

Receiver use this code:

```cpp
WIRE.flush();
WIRE.beginTransmission(device_address_sspin);
WIRE.write((reg_addr<<3 | channel<<1));
WIRE.endTransmission(0);
WIRE.requestFrom(device_address_sspin, (uint8_t) 10);
uint8_t buff[10];
result = WIRE.readBytes(buff, 10);

Serial.println("Reading " + String(result));
for(int i = 0; i < 10; i++) {
	Serial.print(buff[i]);
	Serial.print(" ");
}

```

Output on receiver should alway be:  
32 33 34 35 36 37 38 39 40 41

What I get from output:  
1.st loop:  
 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/f/6/f/f6f32801e6a06e5e293709feac7887a7c341fcac.png)

2.nd loop:  
 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/6/6/2/662b7a1be950bcc10095b0e13b2fc1ca8267931f.png)

3.th loop:  
 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/0/2/5/0257e54b55c7f76e588061c8a88bfd2153376bdd.png)

Why after 1st loop it starts messing with outputs bassically it start outputing 4 times buffer instead of only one with strange numbers?

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [February 14, 2022, 8:19pm UTC](https://forum.arduino.cc/t/wire-write-buffer-duplicating-problem/959183/2 "2022-02-14T20:19:36Z")

</div>

Could you give an introduction ? Are you transferring data between two Arduino boards ? Which Arduino boards do you use ? What is your project about ?

Please give a complete (minimal) sketch. If you are using two Arduino boards, then please give two complete sketches.

A Arduino as a Slave uses the `onRequest` and the `onReceive` handlers. The Wire.requestFrom is for reading data from a sensor as a Master.

The Wire.flush() has no meaning.  
This is the source code:

```cpp
void TwoWire::flush(void)
{
  // XXX: to be implemented.
}

```

There is nothing to implement, because the Arduino Wire library is blocking.

The Wire.readBytes() is from the Stream class. It is okay to use, bu only if you are sure that those bytes are available, since the Wire.readBytes() might wait. That waiting has no meaning for the I2C bus.

Could you use the parameters 'true' and 'false' for Wire.endTransmission() instead of 0 and 1 ?

[EDIT] I found that the Wire library has indeed a flush() function and I changed this post.

---

<div class="post-metadata">

**Author:** ![DrDiettrich](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drdiettrich/32/219048_2.png) [@DrDiettrich](https://forum.arduino.cc/u/DrDiettrich)\
**Post date:** [February 14, 2022, 10:09pm UTC](https://forum.arduino.cc/t/wire-write-buffer-duplicating-problem/959183/3 "2022-02-14T22:09:59Z")

</div>

> [@peter1ainternet](#):
>
> `WIRE.requestFrom(device_address_sspin, (uint8_t) 10);`

You should check this call for error codes.

---

<div class="post-metadata">

**Author:** ![peter1ainternet](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@peter1ainternet](https://forum.arduino.cc/u/peter1ainternet)\
**Post date:** [February 15, 2022, 4:53am UTC](https://forum.arduino.cc/t/wire-write-buffer-duplicating-problem/959183/4 "2022-02-15T04:53:50Z")

</div>

I'm using it for sending basically file (.bin) from one ESP32 using RS485 to another ESP32 with RS485.  
So main thing what would like to do is to download .bin file from API and while receiving bytes it should transmit it to slave saving to SPIFF. When it's over with receiving on SPIFF it starts with update of FW. Download, save to SPIFF and flash is working now only thing left is to send data from master which download .bin to slave which would update FW.

This is just some of code, whole code is really big, don't know how would I present it here?

Also what is biggest number of bytes to transmit via Write?

---

<div class="post-metadata">

**Author:** ![peter1ainternet](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@peter1ainternet](https://forum.arduino.cc/u/peter1ainternet)\
**Post date:** [February 15, 2022, 4:55am UTC](https://forum.arduino.cc/t/wire-write-buffer-duplicating-problem/959183/5 "2022-02-15T04:55:58Z")

</div>

Is there any debug option to use in function?

---

<div class="post-metadata">

**Author:** ![DrDiettrich](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drdiettrich/32/219048_2.png) [@DrDiettrich](https://forum.arduino.cc/u/DrDiettrich)\
**Post date:** [February 15, 2022, 6:30am UTC](https://forum.arduino.cc/t/wire-write-buffer-duplicating-problem/959183/6 "2022-02-15T06:30:49Z")

</div>

Have you ever read the description of Wire.requestFrom()?

---

<div class="post-metadata">

**Author:** ![peter1ainternet](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@peter1ainternet](https://forum.arduino.cc/u/peter1ainternet)\
**Post date:** [February 15, 2022, 6:48am UTC](https://forum.arduino.cc/t/wire-write-buffer-duplicating-problem/959183/7 "2022-02-15T06:48:48Z")

</div>

In docs it says: Used by the master to request bytes from a slave device. The bytes may then be retrieved with the [available()](https://www.arduino.cc/en/Reference/WireAvailable) and [read()](https://www.arduino.cc/en/Reference/WireRead) functions.

Is this still good practice to use it for receiving on slave or should I use onReceived? BUt problem is that onReceived should be called inside loop, all of those functions I'm calling inside looop.

---

<div class="post-metadata">

**Author:** ![DrDiettrich](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drdiettrich/32/219048_2.png) [@DrDiettrich](https://forum.arduino.cc/u/DrDiettrich)\
**Post date:** [February 15, 2022, 6:52am UTC](https://forum.arduino.cc/t/wire-write-buffer-duplicating-problem/959183/8 "2022-02-15T06:52:38Z")

</div>

> [@peter1ainternet](#):
>
> In docs it says:

also that it returns the number of received bytes. IMO you should read more and more intelligent instead of asking over and over again.

---

<div class="post-metadata">

**Author:** ![peter1ainternet](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@peter1ainternet](https://forum.arduino.cc/u/peter1ainternet)\
**Post date:** [February 15, 2022, 6:55am UTC](https://forum.arduino.cc/t/wire-write-buffer-duplicating-problem/959183/9 "2022-02-15T06:55:49Z")

</div>

So I should use onReceive?

---

<div class="post-metadata">

**Author:** ![DrDiettrich](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drdiettrich/32/219048_2.png) [@DrDiettrich](https://forum.arduino.cc/u/DrDiettrich)\
**Post date:** [February 15, 2022, 7:07am UTC](https://forum.arduino.cc/t/wire-write-buffer-duplicating-problem/959183/10 "2022-02-15T07:07:01Z")

</div>

I'm out.

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [February 15, 2022, 8:37am UTC](https://forum.arduino.cc/t/wire-write-buffer-duplicating-problem/959183/11 "2022-02-15T08:37:51Z")

</div>

Your 'WIRE' is a Serial/UART communication ?

---

<div class="post-metadata">

**Author:** ![peter1ainternet](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@peter1ainternet](https://forum.arduino.cc/u/peter1ainternet)\
**Post date:** [February 15, 2022, 8:39am UTC](https://forum.arduino.cc/t/wire-write-buffer-duplicating-problem/959183/12 "2022-02-15T08:39:21Z")

</div>

Yes, UART sc16is752 via RS485 communication.

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [February 15, 2022, 8:42am UTC](https://forum.arduino.cc/t/wire-write-buffer-duplicating-problem/959183/13 "2022-02-15T08:42:03Z")

</div>

Then you need a device driver for the SC16IS752 ?  
Manufacturer's page of the SC16IS752: [https://www.nxp.com/products/peripherals-and-logic/signal-chain/bridges/dual-uart-with-ic-bus-spi-interface-64-bs-of-transmit-and-receive-fifos-irda-sir-built-in-support:SC16IS752\_SC16IS762](https://www.nxp.com/products/peripherals-and-logic/signal-chain/bridges/dual-uart-with-ic-bus-spi-interface-64-bs-of-transmit-and-receive-fifos-irda-sir-built-in-support:SC16IS752_SC16IS762)

How did you connect the SC16IS752 to the ESP32 ?

The ESP32 has an extra serial port itself.

---

<div class="post-metadata">

**Author:** ![peter1ainternet](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@peter1ainternet](https://forum.arduino.cc/u/peter1ainternet)\
**Post date:** [February 15, 2022, 9:17am UTC](https://forum.arduino.cc/t/wire-write-buffer-duplicating-problem/959183/14 "2022-02-15T09:17:42Z")

</div>

Its connected via I2C (SDA, SCL pins).

I have driver, communication on sending each byte separately is already working, what I want is to send bigger amount of data as know .bin has really big amount of bytes and if would be sending each byte separately it would take soo much time....

For one byte transmition and receiving I'm using this code:  
Transmitter:

```cpp
WIRE.beginTransmission(device_address_sspin);
		WIRE.write((reg_addr<<3 | channel<<1));
		WIRE.write(val);
		WIRE.endTransmission(1);

```

Received:

```cpp
WIRE.beginTransmission(device_address_sspin);
		WIRE.write((reg_addr<<3 | channel<<1));
		WIRE.endTransmission(0);
		WIRE.requestFrom(device_address_sspin,(uint8_t)1);
		result = WIRE.read();

```

---

<div class="post-metadata">

**Author:** ![peter1ainternet](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@peter1ainternet](https://forum.arduino.cc/u/peter1ainternet)\
**Post date:** [February 15, 2022, 7:23pm UTC](https://forum.arduino.cc/t/wire-write-buffer-duplicating-problem/959183/15 "2022-02-15T19:23:47Z")

</div>

After all day which I'm into this I have everything done, download FW from API, save it on SPIFF, start transmiting bytes to slave and saving to SPIFF on slave, after transmition ends it flash slave from SPIFF. Everything works but biggest problem is too slow transmition, basically for example 100kb of .bin file size transmit from master to slave around 4min, which means in my case for .bin which is about 1.8mb of size would take about 72 minutes (1 hour and 12 minutes) which is WAY tooo much. Any ideas what to do now?

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [February 15, 2022, 7:41pm UTC](https://forum.arduino.cc/t/wire-write-buffer-duplicating-problem/959183/16 "2022-02-15T19:41:31Z")

</div>

Meanwhile I have been reading the datasheet.  
The read byte and write byte register at register address 0 is for a single byte.  
If the FIFO mode is turned on, then that single byte changes into a FIFO buffer.  
The FIFO is often used with a interrupt, but the FIFO will also work without interrupt.  
You have to turn on the FIFO for both receiving and transmitting.  
But the datasheet itself is not enough, not everything is explained.

How fast do you think that the I2C bus is ?  
Perhaps you can achieve 20kbyte per second, with a 400kHz I2C bus.  
I don't know the baud rate of your RS-485 bus.

Perhaps you can try to find what the data transfer rate is for ESP-NOW : [https://randomnerdtutorials.com/esp-now-esp32-arduino-ide/](https://randomnerdtutorials.com/esp-now-esp32-arduino-ide/)  
There are troubles when more than one wireless (Wifi, Bluetooth, ESP-NOW) is used together.

What is the distance between the ESP32 boards ? Do you really need RS-485 ?  
I would use the extra serial port in full-duplex mode without going through all the extra layers. I read that some have tested a baudrate of 4Mbps, that is 400kbyte per second. Assume some overhead, so it could be between 100kbyte and 200kbyte per second.

Asking a good question to get the answer that you need can be just as hard as answering a question. Have you read the guidelines of this forum ? [How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 14, 2022, 7:41pm UTC](https://forum.arduino.cc/t/wire-write-buffer-duplicating-problem/959183/17 "2022-08-14T19:41:58Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
