Wire write buffer duplicating problem

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:

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:

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

2.nd loop:
image

3.th loop:
image

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

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:

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.

You should check this call for error codes.

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?

Is there any debug option to use in function?

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

In docs it says: Used by the master to request bytes from a slave device. The bytes may then be retrieved with the available() and read() 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.

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

So I should use onReceive?

I'm out.

Your 'WIRE' is a Serial/UART communication ?

Yes, UART sc16is752 via RS485 communication.

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

How did you connect the SC16IS752 to the ESP32 ?

The ESP32 has an extra serial port itself.

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:

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

Received:

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();

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?

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/
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