is data in buffer kept of overwriten by next transfer

it says that arduino mega has buffer 64 bytes but 64th number is always -1 when I read it

for (int i=0; i<64, i++)
{ data*=Serial1.read();}*
data[63] is always -1
so if I transfer next 63 numbers, will they be accepted by buffer if previous was not read
or the next transfer will not be received (lost) until the buffer is read (emptied)

The buffer size is 64. Data goes in, until the buffer is full. Then, data is discarded. When you make room in the buffer, by reading data, more data can be added, but you have no idea how much data was discarded before you made room.

Your improperly posted snippet is useless for demonstrating any kind of problem. You should ONLY read when you KNOW that there is data to be read. And, you should NOT be waiting to read data until there are 64 bytes to be read, because you WILL lose data that way.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

it is impossible to synchronize the data to let arduno know that there a data waiting.

they only way I found is to read in loop status of a pin if it is high then I can start reading.

I am trying to send array of 500 from nucleo board to arduino,

then only way I found is to acknowledge each other with pins going high and low, that a pocket of 63 was send and received.

ED201:
then only way I found is to acknowledge each other with pins going high and low, that a pocket of 63 was send and received.

You could try providing a better description of what is going on so we don't have to guess.

It seems to me you want to send 500 somethings (bytes, chars, ints?) to the Arduino. And I think you are trying some sort of hand-shaking system involving setting an I/O pin to signal when the Arduino is capable of receiving another chunk of data. But that is a form of synchronization and you say that synchronization is not possible.

Can you change the program that is sending the data?

If the problem is due to overflowing the Arduino's 64 byte Serial Input Buffer then why not send the data in 32 byte chunks?

Another option would be to increase the size of the Input Buffer. With a Mega you would have enough SRAM to increase it to 512 bytes. I am working on a project where I needed a larger buffer so I just made a copy of the complete IDE under another name and in that copy I increased the buffer size. That way I can easily use either the standard buffer size with the original IDE or the larger buffer with the modified version.

If none of that is useful then post your program.

...R

if I knew how to increase the buffer to 512 it would solve my problem.

I am sending 500 bytes array[500] from nucleo board to arduino mega.

serial is working fine I am trying to cut the data and send piece by piece in 64 byte chunks.

I cannot do it without having some extra communition between the boards to have the transfers synchronized.

So I have to let the nucleo board that data was read and another 64 bytes can be sent.

But 512 buffer would fix the problem.

The buffer size is defined in the file arduino-1.8.6-linux64/hardware/arduino/avr/cores/arduino/HardwareSerial.h on my Linux laptop.

I think it should be somewhere very similar on any other installation.

I just added these two lines at the top of the file

#define SERIAL_RX_BUFFER_SIZE 128
#define SERIAL_TX_BUFFER_SIZE 128

You could change the 128 to 512 and see what happens.

…R

The Arduino can read serial data orders of magnitude faster than it can arrive. That yours can’t means that it is doing something wrong, or is not the right hardware to be using.

You need to post ALL of your code, so that we can see why it can’t read the data faster than it arrives.

PaulS:
The Arduino can read serial data orders of magnitude faster than it can arrive.

From recent personal experience it is a bit more complicated than that.

What you say is perfectly true as long as the total message fits in the Serial Input Buffer.

However if the Arduino has other work to do after it takes the first 64 bytes from the buffer it is quite likely that the buffer will overflow before the Arduino finds an opportunity to check the buffer again. At 115200 baud the next 64 bytes will arrive in about 5 millisecs. It would not be unreasonable for the other work of the Arduino to take longer than that even with a well written program.

Yes, there may be optimizations that could be done to save some time and allow loop() to repeat a little faster but isn't it a lot simpler just to increase the buffer size if you have plenty of spare memory?

...R

Yes, there may be optimizations that could be done to save some time and allow loop() to repeat a little faster but isn't it a lot simpler just to increase the buffer size if you have plenty of spare memory?

Increasing the buffer gives more time for data to accumulate, yes. But, now you've got 8 times as much data to read, and process, before getting back to reading more data. Which is likely going to take 8 times as long to do, so, have you really obtained any relief?

PaulS:
Which is likely going to take 8 times as long to do, so, have you really obtained any relief?

I don't see it like that. The full message would be needed in any case - it's just a question of how to gather it reliably.

...R

Robin2:
The buffer size is defined in the file arduino-1.8.6-linux64/hardware/arduino/avr/cores/arduino/HardwareSerial.h on my Linux laptop.

I think it should be somewhere very similar on any other installation.

I just added these two lines at the top of the file

#define SERIAL_RX_BUFFER_SIZE 128

#define SERIAL_TX_BUFFER_SIZE 128





You could change the 128 to 512 and see what happens.


...R

when I changed to 512 it would freeze. The only way I see is to send 64 bytes and acknowledge back and for and send request to send another 64 bytes. the last byte would be the number of the data pocket

I am doing it with combination of pins both nucleo and arduino read the pins for instructions.

ED201:
when I changed to 512 it would freeze.

First off, that does not tell me EXACTLY what you did.

Have you tried using a smaller value that is bigger than 64? I have not studied the library code to see if it has some upper limit.

The library would be limited to a max of 255 if it uses a byte as the counter for the position within the buffer.

…R

Robin I do not know if you are familiar with nucleo boards

this is the code in nucleo

HAL_UART_Transmit_DMA(&huart1, (uint8_t*)array, 63);

which sends to arduino array of 500 which is cut into 63 pieces (bytes)

after receiving PIN high

arduino is waiting in while(1) loop and reading pin ,digitalRead(45)

when it goes up it exits the wait loop while(1)

and reads

if (Serial1.available())
{
for(uint8_t i = 0; i<64; i++)
{
serialBuffer*=Serial1.read();*

  • }*
  • }*
    there are of course a few extra lines of code but you have the idea, it is working
    also notice I am sending only 63 bytes not 64, because the 64th is always -1
    and it repeats the cycle until it gets all 500 elements

ED201:
Robin I do not know if you are familiar with nucleo boards

I am not.

But good to hear that you now have a solution.

...R