Go Down

Topic: Serial port "rx_buffer" gobbles up unnessary RAM (Read 3 times) previous topic - next topic



I am sold that HardwareSerial need to be fixed.  The problem is to sell this to the Arduino developers.

Arduino 1.0 beta 3 is really bad.  Serial gets loaded even if you don't use it.  You can't easily control buffer sizes even by editing HardwareSerial.h/HardwareSerial.cpp.  There are both input and output buffers and they must be the same size.

One value controls the size of all eight buffers on a Mega and they are all allocated, even if you don't use any Serial ports.

Here are links to what I found:




There have been suggestions that if you add a specially named header file to your sketch (e.g. options.h) it should be automatically included by libraries and the core during compilation: http://code.google.com/p/arduino/issues/detail?id=27.  This is mostly motivated by precisely this issue (RAM usage for serial buffers).  So far I've been reluctant to add it because there are lots of other possible complications (and, so far, I've seen few other use cases).  But I'm not necessarily opposed to ever including it, if there are good reasons.

If there's a reasonable way to have the compiler only include the buffers for the specific serial ports that are actually used by the sketch, that would be great.  Any thoughts on how that might happen?

Adding parameters to Serial.begin() to specify the buffer size (and dynamically allocating the buffers) seems like overkill.


Beta 4 fixes the problem that Serial is loaded even if it is not used.

I would like to see a way to independently specify buffer sizes for the eight buffers on the Mega if all eight must be loaded when one port is used.

There are apps that require one large receive buffer on a Mega and this is not currently possible.  For example, to log data from a 115200 baud Serial port to an SD card reliably requires about a 2500 byte receive buffer to allow for the maximum possible write latency of an SD card.

I would accept any improvement, even if it required edits of defines or const parameters in HardwareSerial.

I am writing a replacement for HardwareSerial for my own use.  I am using an idea from several commercial embedded systems.

All Serial ports start in an unbuffered mode and do not use interrupts.  This seems to work fine for most sketches since few sketched benefit from output buffering and most sketches do little input. 

Even sketches that do multiple character input work o.k. since most do input in a wait for character loop.

I added an attachInterrupts() call to allocate  buffers and switch to interrupt driven mode.  This allows run time control of serial buffering.  You can just attach a buffer to the input side of a single port when required.


Are you planning to share this library when it's finished? I'd be interested. I've already modified my own version of HardwareSerial to support buffered output but different buffer sizes for each port sounds really useful.


I am not planning on sharing this library.  It is intended as a true replacement for HardwareSerial, not just an additional serial library.

I tried the idea as an additional serial library and found that other libraries use Serial.  There can only be one ISR for a serial interrupt vector so replacement of HardwareSerial is the only practical solution.

The policy of the Arduino developers is that they control features of the core for comparability reasons. Therefore I have not released core components that I have redesigned for my use. 

Go Up