build a bigger array from Serial.readBytes


I've got an array that I read with serial.readBytes , and now I want to build a bigger array until a fixed size is reached.

I try with serial.available but it doesn't work this way

char serial_array[800]; 
void loop() {
  if (Serial.available() == 4000 ) {   
    Serial.readBytes ( serial_array, 4000);
     // do stuff

I have to fill a bigger array (4000) from this little ones (800)

Have I to build it byte by byte, or is there a solution to group the entire arrays until the size is reached ?

thanks, cb

The receive buffer for Serial is only 64 bytes, so this condition will never be true.

if (Serial.available() == 4000 )

This statement is trying to read 4,000 bytes into an 800 byte buffer. That would not work, even if the receive buffer were bigger.

Serial.readBytes ( serial_array, 4000);

Which type of Arduino are you using? The Uno and many of the other types only have 2,000 bytes of SRAM, so you can't hold a 4,000 byte array.

Can you tell us more about your application, what you are trying to achieve overall? Maybe there is a different approach that does not need such a large array.

ok thanks for this precision.

I use a teensy 3.1 for controling 1800 led ( ws2811 shift ) , and use octows2811 library. I send 1800x3 values from max/msp (5400), which is limited to send chunk of 1024 on his serial object.

The receive buffer for Serial is only 64 bytes

(you speak about multiple of 64,) 1024 is ok?

(you speak about multiple of 64,) 1024 is ok?

No, it's fixed at 64, unless you hack the Arduino core files.

I use a teensy 3.1

So you have 64K of RAM? No problem holding the 5,400 byte array :) My mistake.

You don't have to use readBytes() to read the incoming data. You can read it one byte at a time using read() so the receive buffer size would not be a constraint.

is it not faster to use directly the array than split it byte by byte to do another one bigger?

I was thinking that using serial.readBytes is better? isn't it?

What baud rate is your Serial port?

I would not expect a material performance difference in using read() rather than readBytes(). The dominant time in both cases will be the time waiting for the next byte to arrive in the Serial port.

To clarify, i'm suggesting reading bytes directly into your larger array, not going via a smaller one.

baud rate = 115200

Looking at the Arduino source code, readBytes() calls a private method timedRead() to read each of the specified number of bytes from the Serial port.

The timedRead() method calls the normal read() method for each byte but also has code to check if a timeout has happened.

So read() will be quicker than readBytes().