Serial port "rx_buffer" gobbles up unnessary RAM

WRT overflow() I added the lost char counter into the ringbuffer

HardwareSerial.cpp

struct ring_buffer
{
  unsigned char buffer[RX_BUFFER_SIZE];
  int head;
  int tail;
  long lost;  // <<<< ADDED
};

and

inline void store_char(unsigned char c, ring_buffer *rx_buffer)
{
  int i = (unsigned int)(rx_buffer->head + 1) % RX_BUFFER_SIZE;

  // if we should be storing the received character into the location
  // just before the tail (meaning that the head would advance to the
  // current location of the tail), we're about to overflow the buffer
  // and so we don't write the character or advance the head.
  if (i != rx_buffer->tail) {
    rx_buffer->buffer[rx_buffer->head] = c;
    rx_buffer->head = i;
  } else rx_buffer->lost++;  // <<<<<<<<<<< ADDED
}

Added this function

// note: lost counter is not reset ...
uint32_t HardwareSerial::overflow(void)
{
 return  _rx_buffer->lost;
}

Added in hardwareSerial.h the prototype of course.

virtual uint32_t overflow(void);

Works like a charm!

test sketch, just keep sending chars :slight_smile:

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  Serial.println(Serial.overflow());
  Serial.println(Serial.available());
  Serial.println("----------");
  delay(1000);
}

reported as issue 637, - http://code.google.com/p/arduino/issues/detail?id=637 -