How to Increase the Arduino Serial Buffer Size?

Hello everybody,
I need to increase the buffer size of the hardware serial port (from 64 to 256 bytes). The .ino version of the project works properly on Arduino IDE with HardwareSerial.h library modified. How can we traslate this modification to PlataformIO?
I have tried do the same in lib directory, creating .h and .cpp private libs modified but didn’t worked, compile and build correctly but still loosing data on serial port.
I appreciate any help.
Thanks in advance.

1 Like

Where do you modify it in Arduino IDE?

In the core library HardwareSerial.h, modify:

define SERIAL_RX_BUFFER_SIZE 64

to:

define SERIAL_RX_BUFFER_SIZE 256

Save, restart IDE, compile, upload and ok. It is very well described in Arduino foras.
Regards.

You don’t need to modify original framework’s files with PlatformIO. Please use build_flags option in platformio.ini file:

[env:uno]
platform = atmelavr
board = uno
framework = arduino
build_flags = -D SERIAL_RX_BUFFER_SIZE=256
2 Likes

Thanks very much!
I have already tested and it works perfectly.
Great!

hello please I would like to know more about platformio. I have the same problem but I do not find how to use your solution

What step exactly are you stuck at? If you have platformio project you have a platformio.ini there. If you there a new line with build flags as build_flags = -D SERIAL_RX_BUFFER_SIZE=256 you redefine the buffer size to 256 on the Arduino platform. It’s just an extra flag for the GCC compiler.

So my question is…

If you have a define XX=y in a .h (or any) file, does that mean any -D XX=YY will always override it?

Just take a look at the code in question. You can search for SERIAL_RX_BUFFER_SIZE in .platformio\packages\framework-arduinoavr\cores\arduino then you’ll see HardwareSerial.h with

#if !defined(SERIAL_TX_BUFFER_SIZE)
#if ((RAMEND - RAMSTART) < 1023)
#define SERIAL_TX_BUFFER_SIZE 16
#else
#define SERIAL_TX_BUFFER_SIZE 64
#endif
#endif
#if !defined(SERIAL_RX_BUFFER_SIZE)
#if ((RAMEND - RAMSTART) < 1023)
#define SERIAL_RX_BUFFER_SIZE 16
#else
#define SERIAL_RX_BUFFER_SIZE 64
#endif
#endif

[..]
class HardwareSerial : public Stream
{
  protected:
[..]
    unsigned char _rx_buffer[SERIAL_RX_BUFFER_SIZE];
    unsigned char _tx_buffer[SERIAL_TX_BUFFER_SIZE];

If SERIAL_TX_BUFFER_SIZE wasn’t yet defined (#if !defined(..)) the above series of #if figure out a standard value for you. But you can also set it yourself.

Since this is core file you should not modify this header file directly. If you write the #define SERIAL_RX_BUFFER_SIZE Y in on of your own hedaer files the HardwareSerial.h file won’t see it because it doesn’t include it. Thus it it correct to use the compiler switch -D for defining this macro to some value.

Hello,

When I try to modify the “SERIAL_BUFFER_SIZE” directly in “RingBuffer.h”, I’m able to receive more characters and also like expected to see my "memory-usage increase. But when using the “build_flags” option in “platformio.ini” the size of “SERIAL_BUFFER_SIZE” is not increasing. Below my “platformio.ini” code.
Am I doing anything wrong? Thanks

[env:adafruit_feather_m0]
platform = atmelsam
board = adafruit_feather_m0
framework = arduino
;upload_port = COM64
build_flags = -D SERIAL_RX_BUFFER_SIZE=1024

The Feather M0 and the Arduino Uno uses a completely different Serial implementation because the Feather has a built-in USB interface while the UNO goes via a UART interface to a UART-to-USB converter chip.

As you have correctly identified, the relevant code is in RingBuffer.h instead of HardwareSerial.h. What you seem to have missed is that the relevant constant is called SERIAL_BUFFER_SIZE instead of SERIAL_RX_BUFFER_SIZE (note the missing RX).

So the correct line for platformio.ini file is:

build_flags = -D SERIAL_BUFFER_SIZE=256
2 Likes

Hello manuelbl,
This was indeed the problem.

Thanks

1 Like

Hi, if board have more than one serial (in example stm blue pill has 3 uart), this build flag (build_flags = -D SERIAL_RX_BUFFER_SIZE=256) affect all the 3 uarts?

Always read the source code of your Arduino core implementation, therein lies the truth. This is not standard across all core implementations.

When the bluepill is usesd with the STM32Duino core (board_build.core = stm32, default), the code

takes effect, so every instance of the HardwareSerial class has its own buffer, all of which are of the declared sizes SERIAL_RX_BUFFER_SIZE and SERIAL_TX_BUFFER_SIZE respectively, so this is uniform.

For the Maple core (board_build.core = maple), the code

applies accordingly.

Sorry, I’m not sure if i understand: i use STM32Duino core and i want to increase rx buffer of uart 2. In this case i have to write:
SERIAL2_RX_BUFFER_SIZE=256 ?

Is this right or only one SERIAL_RX_BUFFER_SIZE=256 flag affect all the 3 buffers?

As I said here

SERIAL_RX_BUFFER_SIZE applies to all serials. See code.

1 Like