Open cmaglie opened 11 years ago
Missing in the discussion in the thread is a reset function for the lost counter. This makes it functional complete and stays backwards compatible.
Rob
HardwareSerial.cpp
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; }
Please implement this. Errors rising up because of lost chars in HardwareSerial are really really annoying and hard to track down.
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.
UARTs usually have an overflow bit, not a counter. This should be sufficient to determine that something is wrong with the code. Related issue: characters with parity error are simply dropped without any notice!
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