For those interested, I updated the issue on github - HardwareSerial overflow of input detection [imported] · Issue #223 · arduino/ArduinoCore-avr · GitHub
Missing in the discussion in the thread is a reset function for the lost counter,
This makes the proposal functional complete while staying 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;
}
New addition as it makes sense to be able to reset the overflow/ lost counter
void resetOverflow(void)
{
_rx_buffer->lost = 0;
}