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.
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.
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?
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?
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.
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