how to understand buffer size for RX

Hello everyone,

I was wondering how to interpret the RX buffer size. For an arduino mega I assume it is 64 bytes for RX and 16 bytes for TX. But does this mean the following:

If a string of 40 bytes is sended to an RX channel the arduino mega will store these 40 bytes in its memory (and thus leaving 24 bytes free). And for this storage no command has to be done (it is done on hardware-level). As long as no other string arrives these 40 bytes will stay in the buffer. Once I start reading the buffer with serial.read() each time I read a byte, a byte will be removed from the buffer. So if I do serial.read() 40 times all the bytes will be read from the buffer and the buffer will be empty.
The same story goes for the TX buffer. Here only 16 bytes can be stored.

Am I correct with this assumption?

Thanks in advance,
Bart

Why assume when you can look in the code!. Oh and your wrong!.

Mark

holmes4:
Why assume when you can look in the code!. Oh and your wrong!.

Mark

and the code can be found......?????
if the op knew he wouldnt need to ask, and if you dont why be so rude?
and i dont cos if i did i would have added a link at least.

Link : https://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/HardwareSerial.cpp

Peter_n:
Link : https://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/HardwareSerial.cpp

cheers and knowing the file name led me to the home of the system sources
.......\Arduino\hardware\arduino\cores\arduino

If a string of 40 bytes is sended to an RX channel the arduino mega will store these 40 bytes in its memory (and thus leaving 24 bytes free). And for this storage no command has to be done (it is done on hardware-level). As long as no other string arrives these 40 bytes will stay in the buffer. Once I start reading the buffer with serial.read() each time I read a byte, a byte will be removed from the buffer. So if I do serial.read() 40 times all the bytes will be read from the buffer and the buffer will be empty.
The same story goes for the TX buffer. Here only 16 bytes can be stored.

Apart from the fact that they are both 64 bytes that is the rough idea. It’s not totally done at the hardware level, an interrupt service routine takes incoming data and puts it in the buffer, and another one empties the output buffer.

However, note that if you send 40 bytes to the arduino, they appear ONE AT A TIME in the buffer, at an interval dependent on the transmission speed of the serial port (1ms per byte at 9600bps, approximately.)

It’s a relatively common mistake for beginners to write code like:

if (Serial.available() > 0) {  // Did data arrive ?
   for (int i=0; i<10; i++) {
      mycommand[i] = Serial.read();  // read the 10 bytes that I know I've been sent.
   }
}

And the problem is that when the first byte is available, it’s the ONLY byte that is available; you have another 9 ms (or whatever) before the rest of the data arrives. (unlike programming some desktop systems, where “console input” is transferred from the operating system to your program in large chunks (for example, a line that was terminated when the user hit enter.))