Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

HardwareSerial overflow of input detection [imported] #637

cmaglie opened this Issue Nov 15, 2012 · 3 comments


5 participants

cmaglie commented Nov 15, 2012

This is Issue 637 moved from a Google Code project.
Added by 2011-09-10T16:11:30.000Z by rob.till...@gmail.com.
Please review that bug for more context and additional comments, but update this bug.

Original labels: Type-Defect, Priority-Medium

Original description

What steps will reproduce the problem?

If more data is received over Serial than the buffer can hold (or is not processed in time due to whatever blocking reason)

It is not difficult to add a counter in the Serial ringbuffer to count the lost characters. A function overflow() can be called to retrieve this counter.

see - http://arduino.cc/forum/index.php/topic,71833.msg538108.html#msg538108

Missing in the discussion in the thread is a reset function for the lost counter.
This makes it functional complete and stays backwards compatible.



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

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

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

void resetOverflow(void)
_rx_buffer->lost = 0;

@ffissore ffissore added the New label Feb 27, 2014

@cmaglie cmaglie removed the New label Feb 27, 2014

caschoff commented Sep 2, 2015

Please implement this. Errors rising up because of lost chars in HardwareSerial are really really annoying and hard to track down.


techpaul commented Jan 18, 2016

My first thoughts are

1/ Why do you need a long to hold it unsigned int should be enough (65535 errors)

2/ Overflow (or other discraded character reasons like parity error) should be
if count not max
increment count

2/ Be better to have ATOMIC access to read the overflow counter to temporary varaiable
then reset count to 0 whenever you read the count. End ATOMIC before returning temp variable
Then you get counts between reads.

Considering doing something like this for Due/ARM variants with some other tweaks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment