[solved] Serial buffer size

Hello,
2 years ago i had finished a sketch (IDE 1.6.7) in mega2560, using 1serial in and 2 serial out. For the purpose of the project I increased the serial buffer size in hardwareserial.h to 128.

AFAIK, buffer size cannot be "adjusted" in newer IDE. Of course, the solutions of making the sketch "smarter" (ie not using delays) are respected.

The way (for example) the Serial was handled, is (in pseudo)

void loop{
check if buffer has received >75 bytes //data arrive in packets of 75 every 100ms @ 19200
if yes (complete packet) read one packet and go to process data
else process previous data

output one packet prosessed data (37bytes) every 40mec

}

In this sketch I have to make a minor change and then recompile, upload... but for this old project it is no worth to make also the changes for better handling (lower buffer size).

Is there any way to increase the serial buffer size? (PMs are welcome)

Not behind a PC but hardwareserial.h should have the setting for the buffer size.

You can edit it using a normal text editor.

Hello demkat1

demkat1:
AFAIK, buffer size cannot be "adjusted" in newer IDE. Of course, the solutions of making the sketch "smarter" (ie not using delays) are respected.

I wonder why.
I can still do it (Windows IDE 1.8.8 ).

Regards,
bidouilleelec

sterretje:
Not behind a PC but hardwareserial.h should have the setting for the buffer size.

You can edit it using a normal text editor.

I knew about hardwareserial.h and had used it for many projects, although did not check extensively. But for some days now there is a problem.
for example

change hardwareserial to

..
..
..
#define SERIAL_TX_BUFFER_SIZE 64
#define SERIAL_RX_BUFFER_SIZE 512



#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 256
#endif
#endif
..
..

save /exit

open ide 1.8.1/new sketch/compile for mega2560 -> "usage 9 bytes of dynamic memory"

in setup add Serial1.begin(9600); compile-> "usage 184 bytes of dynamic memory"
in setup add Serial2.begin(9600); compile-> "usage 341 bytes of dynamic memory"
in setup add Serial3.begin(9600); compile-> "usage 498 bytes of dynamic memory"

that for sure is not corresponding to the definition of 64/512 on each port (if the difinition is correct)

I think there are separate buffer sizes for each of the Mega's hardware serial ports.

...R

Robin2:
I think there are separate buffer sizes for each of the Mega's hardware serial ports.

...R

Can I ask, where?

Maybe I was wrong. My 1.8.6 has files like HardwareSerial1.cpp but they don't seem to define the buffer size.

...R

I went back to editing hardwareserial.h and restored all initial values (16/64)
Nothing changes. used dynamic memory bytes go :

9/184/341/498/655 for no serial/ only serial / +serial1/ +serial2/ +serial3

it seems that hardwareserial has no effect

The size of buffers is fixed and same for all ports. The difference is only for RX and TX. The only way how to change it is to modify the code. However, it will require some change in approach, since service functions are common for all ports.

Something that may be confusing is that you may have multiple copies of Arduino AVR Boards (which contains the serial buffer code you're trying to modify), but only one of those copies is in use by the Arduino IDE. The Arduino IDE comes with a bundled copy of Arduino AVR Boards. However, if you install an update of Arduino AVR Boards via the Arduino IDE's Boards Manager, that is installed to a different location on your computer. So if you are editing the files of the inactive copy of Arduino AVR Boards it would give just the results you're seeing.

The easiest way to find the active hardware package location is as follows:

  • Select a board from the hardware package you want to find from the Tools > Board menu
  • File > Examples > SPI > BarometricPressureSensor (or any other SPI example sketch)
  • Sketch > Show Sketch Folder
  • Move up folder levels until you reach the one that contains boards.txt

You will now find the hardware serial buffer definitions at cores/arduino /HardwareSerial.h.

2 Likes

yes! that was it.

but think about the situation : because I know that there are many "versions" of some files, i had searched the entire hdd, found a total of 8 hardwareserial.h files but the system didnot show the one in AppData!!!

thank you, thank you, THANK YOU

You're welcome. I'm glad if I was able to be of assistance. Enjoy!
Per

and that was your 16666 post !!!