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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
############################################################## uno.name=Arduino Uno uno.upload.protocol=arduino uno.upload.maximum_size=32256 uno.upload.speed=115200 uno.bootloader.low_fuses=0xff uno.bootloader.high_fuses=0xde uno.bootloader.extended_fuses=0x05 uno.bootloader.path=optiboot uno.bootloader.file=optiboot_atmega328.hex uno.bootloader.unlock_bits=0x3F uno.bootloader.lock_bits=0x0F uno.build.mcu=atmega328p uno.build.f_cpu=16000000L uno.build.core=arduino uno.build.variant=standard ############################################################## uno256.name=Arduino Uno (256 Serial Buffer) uno256.upload.protocol=arduino uno256.upload.maximum_size=32256 uno256.upload.speed=115200 uno256.bootloader.low_fuses=0xff uno256.bootloader.high_fuses=0xde uno256.bootloader.extended_fuses=0x05 uno256.bootloader.path=optiboot uno256.bootloader.file=optiboot_atmega328.hex uno256.bootloader.unlock_bits=0x3F uno256.bootloader.lock_bits=0x0F uno256.build.mcu=atmega328p uno256.build.f_cpu=16000000L uno256.build.core=arduino_256_serialbuf uno256.build.variant=standard ############################################################## |
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.