Arduino – Serial port buffer size mod

Source: https://www.hobbytronics.co.uk

The Arduino core code contains a nice little round robin data buffer where you can keep throwing data at it and the arduino code will read the data and process it in order. However, this data buffer is by default only 64 bytes in size. This value is hard coded in the Arduino core source code and applies to all Arduino boards, even those with a vast amount of RAM available.

The 64 byte limit meant that sending a burst of data longer than 64 bytes would cause data to be truncated as the ATmega328 could not process the data sent fast enough.

The solution is easy, increase the buffer size to 256 bytes.

This is easy enough to implement but is a real pain because the Arduino core code which includes this setting is compiled before your actual program code is, so you cannot simply setup a #define or similar function to be able to select buffer size at compile time.

We found a number of convoluted methods on the internet which claim to work, but we thought an easier solution might be of use to others who need to increase the buffer size. The method described below is the option we chose to implement, and while not exactly elegant is easy to use and understand.

Solution

The solution is to create a complete copy of the arduino core code (it’s really not that big and disk space is cheap), modify the buffer size in the new core code and then to create a new board which is listed in the Arduino IDE which uses this new core directory. The steps to follow are as follows..

The whole of the Arduino core code is located in a directory similar to

C:\Program Files\arduino-1.0.1\hardware\arduino\cores\arduino

Make a complete copy of this directory and save it to

C:\Program Files\arduino-1.0.1\hardware\arduino\cores\arduino_256_serialbuf

The hard coded buffer size is stored in a file called HardwareSerial.cpp (or USBAPI.h in more recent versions)

Here is the standard definition located near the top of the file

#define SERIAL_BUFFER_SIZE 64

Edit the HardwareSerial.cpp file in the new directory and modify the buffer size

#define SERIAL_BUFFER_SIZE 256

Now we need to add an option to the boards.txt file to use this new directory. The boards.txt file should be in a directory similar to

C:\Program Files\arduino-1.0.1\hardware\arduino

Below is part of the boards.txt file. The first section is for the standard Arduino Uno. We have added a section below it which will display in the Arduino IDE as Arduino Uno (256 Serial Buffer). You can see the core directory is referenced to our new directory with the modified file (uno256.build.core=arduino_256_serialbuf)

That’s all you need to do. Now when you want a larger serial buffer (for a specific board) you just choose this as the board in the Arduino IDE and compile as normal.