Increasing Arduino Serial Buffer Size

Hi Gang

I've just downloaded the latest iteration of the Arduino IDE. I need to increase the size of serial buffer from the standard 64 bytes to 128 bytes. In the past I've done the following.

Increasing Arduino Serial Buffer Size

Unfortunately the file path no longer exists. The following file path does bare 'some' fruit.

Program Files (x86)>Arduino>hardware>arduino>avr>cores>arduino>HardwareSerial.cpp

I'm unable to find however the key line.

#define SERIAL_BUFFER_SIZE 64

Has the way the serial buffer is handled changed?

Any help would be greatly appreciated.

Cheers

Jase :)

Is it, perhaps, in a header file?

Hi AWOL

Thanks for getting back to me so quickly. The following file path…

Program Files (x86)>Arduino>hardware>arduino>avr>cores>arduino>HardwareSerial.h

I found the following…

/*
  HardwareSerial.h - Hardware serial library for Wiring
  Copyright (c) 2006 Nicholas Zambetti.  All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

  Modified 28 September 2010 by Mark Sproul
  Modified 14 August 2012 by Alarus
  Modified 3 December 2013 by Matthijs Kooijman
*/

#ifndef HardwareSerial_h
#define HardwareSerial_h

#include <inttypes.h>

#include "Stream.h"

// Define constants and variables for buffering incoming serial data.  We're
// using a ring buffer (I think), in which head is the index of the location
// to which to write the next incoming character and tail is the index of the
// location from which to read.
// NOTE: a "power of 2" buffer size is reccomended to dramatically
//       optimize all the modulo operations for ring buffers.
#if !defined(SERIAL_TX_BUFFER_SIZE)
#if (RAMEND < 1000)
#define SERIAL_TX_BUFFER_SIZE 16
#else
#define SERIAL_TX_BUFFER_SIZE 64
#endif
#endif
#if !defined(SERIAL_RX_BUFFER_SIZE)
#if (RAMEND < 1000)
#define SERIAL_RX_BUFFER_SIZE 16
#else
#define SERIAL_RX_BUFFER_SIZE 64
#endif
#endif

Line 41…

#define SERIAL_TX_BUFFER_SIZE 64

Line 48…

#define SERIAL_RX_BUFFER_SIZE 64

Not sure if this is correct?

Thought?

Cheers

Jase :slight_smile:

should work

Hi robtillaart

So should I change both the rx and tx buffer size in the header (.h) file?

Cheers

Jase :)

This is a recent change.

You can now specify the RX and TX sizes independently using SERIAL_TX_BUFFER_SIZE and SERIAL_RX_BUFFER_SIZE.

https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/HardwareSerial.h#L42

Implement your own #defines above the line highlighted in the link above. The line numbers will be different as there has been a comment added since the version you are using was released.

#define SERIAL_TX_BUFFER_SIZE 1024 #define SERIAL_RX_BUFFER_SIZE 1024

Hi pYro_65

Do you mean below the highlighted line?

Cheers

Jase :)

ilovetoflyfpv: Hi pYro_65

Do you mean below the highlighted line?

Cheers

Jase :)

Nope,

Those macros are set below if the macros SERIAL_TX_BUFFER_SIZE/SERIAL_RX_BUFFER_SIZE are not already defined.

Hi pYro_65

I don't quite follow? Are you saying I have two options?

  1. Simply change...
#define SERIAL_TX_BUFFER_SIZE 64

&

#define SERIAL_RX_BUFFER_SIZE 64

to

#define SERIAL_TX_BUFFER_SIZE 128

&

#define SERIAL_RX_BUFFER_SIZE 128

OR

  1. Create my own '#define' above the highlighted line...
#define SERIAL_TX_BUFFER_SIZE 128

&

#define SERIAL_RX_BUFFER_SIZE 128

then delete any further reference to 'serial buffer size' below the highlighted line?

Cheers

Jase :)

Do not modify/remove anything below the line, just add the define for which ever buffer you want to re-size above the line.

The defines below the line are set only if they have not been previously defined.

The alternative is to leave the serial buffer sizes as they are, and just read them more frequently.

Hi Guys

Thanks for the responses. I'm actually thoroughly confused as to what to do? Could someone post an example of how I'm meant to change the header file buffer size from 64 bytes to 128 bytes?

Cheers

Jase :(

you just need to change the #define lines (as stated in post #8)

but be aware they are changed for all your sketches

Hi robtillaart

Is the following correct?

/*
  HardwareSerial.h - Hardware serial library for Wiring
  Copyright (c) 2006 Nicholas Zambetti.  All right reserved.
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.
  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  Modified 28 September 2010 by Mark Sproul
  Modified 14 August 2012 by Alarus
  Modified 3 December 2013 by Matthijs Kooijman
*/

#ifndef HardwareSerial_h
#define HardwareSerial_h

#include <inttypes.h>

#include "Stream.h"

// Define constants and variables for buffering incoming serial data.  We're
// using a ring buffer (I think), in which head is the index of the location
// to which to write the next incoming character and tail is the index of the
// location from which to read.
// NOTE: a "power of 2" buffer size is reccomended to dramatically
//       optimize all the modulo operations for ring buffers.
// WARNING: When buffer sizes are increased to > 256, the buffer index
// variables are automatically increased in size, but the extra
// atomicity guards needed for that are not implemented. This will
// often work, but occasionally a race condition can occur that makes
// Serial behave erratically. See https://github.com/arduino/Arduino/issues/2405
#if !defined(SERIAL_TX_BUFFER_SIZE)
#if (RAMEND < 1000)
#define SERIAL_TX_BUFFER_SIZE 16
#else
#define SERIAL_TX_BUFFER_SIZE 128 //CHANGE HERE!!!
#endif
#endif
#if !defined(SERIAL_RX_BUFFER_SIZE)
#if (RAMEND < 1000)
#define SERIAL_RX_BUFFER_SIZE 16
#else
#define SERIAL_RX_BUFFER_SIZE 128 //CHANGE HERE!!!
#endif
#endif
#if (SERIAL_TX_BUFFER_SIZE>256)
typedef uint16_t tx_buffer_index_t;
#else
typedef uint8_t tx_buffer_index_t;
#endif
#if  (SERIAL_RX_BUFFER_SIZE>256)
typedef uint16_t rx_buffer_index_t;
#else
typedef uint8_t rx_buffer_index_t;
#endif

Cheers

Jase :slight_smile:

Its quite simple:

Do not modify/remove anything below the line, just add the define for which ever buffer you want to re-size above the line.

So exactly as I've already mentioned, add the define for which ever buffer you want to re-size above the line.

[u]Do not change anything[/u] at or below the line I linked, you keep trying to do this for some reason...

Once more, put this above the line I linked.

#define SERIAL_TX_BUFFER_SIZE 128
#define SERIAL_RX_BUFFER_SIZE 128

Hi pYro_65

Thanks for your reply. So the following should be fine? I just don’t want to break anything. All the hash tags are throwing me. :wink:

Cheers

Jase :slight_smile:

/*
  HardwareSerial.h - Hardware serial library for Wiring
  Copyright (c) 2006 Nicholas Zambetti.  All right reserved.
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.
  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  Modified 28 September 2010 by Mark Sproul
  Modified 14 August 2012 by Alarus
  Modified 3 December 2013 by Matthijs Kooijman
*/

#ifndef HardwareSerial_h
#define HardwareSerial_h

#include <inttypes.h>

#include "Stream.h"

// Define constants and variables for buffering incoming serial data.  We're
// using a ring buffer (I think), in which head is the index of the location
// to which to write the next incoming character and tail is the index of the
// location from which to read.
// NOTE: a "power of 2" buffer size is reccomended to dramatically
//       optimize all the modulo operations for ring buffers.
// WARNING: When buffer sizes are increased to > 256, the buffer index
// variables are automatically increased in size, but the extra
// atomicity guards needed for that are not implemented. This will
// often work, but occasionally a race condition can occur that makes
// Serial behave erratically. See https://github.com/arduino/Arduino/issues/2405

#define SERIAL_TX_BUFFER_SIZE 128 //Here!!!
#define SERIAL_RX_BUFFER_SIZE 128 //and here!!!


#if !defined(SERIAL_TX_BUFFER_SIZE)
#if (RAMEND < 1000)
#define SERIAL_TX_BUFFER_SIZE 16
#else
#define SERIAL_TX_BUFFER_SIZE 64
#endif
#endif
#if !defined(SERIAL_RX_BUFFER_SIZE)
#if (RAMEND < 1000)
#define SERIAL_RX_BUFFER_SIZE 16
#else
#define SERIAL_RX_BUFFER_SIZE 64
#endif
#endif
#if (SERIAL_TX_BUFFER_SIZE>256)
typedef uint16_t tx_buffer_index_t;
#else
typedef uint8_t tx_buffer_index_t;
#endif
#if  (SERIAL_RX_BUFFER_SIZE>256)
typedef uint16_t rx_buffer_index_t;
#else
typedef uint8_t rx_buffer_index_t;
#endif

Yes, that is what I meant.

In the future, the IDE may allow sketches to provide defaults for these defines. Then each project will be able to have a custom setting for things like this.

Hi pYro_65

Thanks so much. I really appreciate the help.

Cheers

Jase :)

pYro_65: Yes, that is what I meant.

In the future, the IDE may allow sketches to provide defaults for these defines. Then each project will be able to have a custom setting for things like this.

This is the future asking if it's implemented yet. I'm getting heartburn while editing HardwareSerial.h