Does serial buffer discard old data when buffer is full and new data comes?

I have some serial sensors that are free running. They power up and start sending data to my MEGA. I have thought that all serial buffers will discard one oldest byte and take in a new byte when the ring buffer is full. My test today kind of indicates differently. Am I correct in assuming the buffer discard old and takes in new when it is full? Thanks.

liudr:
I have some serial sensors that are free running. They power up and start sending data to my MEGA. I have thought that all serial buffers will discard one oldest byte and take in a new byte when the ring buffer is full. My test today kind of indicates differently. Am I correct in assuming the buffer discard old and takes in new when it is full? Thanks.

It's a software circular buffer (using a head and tail pointer with a fixed sized array) in the serial library so if it gets full and another characters arrives it should just overwrite the oldest character in the buffer with the new character I would think.

Lefty

Lefty,

So if I send 12345 and the buffer is only 4 bytes long then I get 5234? Thanks. I will have to try it tomorrow. Too late tonight.

In version 0022 (copy of source below) a character received when the buffer is full will be lost/ignored.

  // 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;
  }

So in the example of a 4 byte buffer and receiving 12345, we would read this as 1234 an remain "happily" unaware of the 5th character received.

Thanks BenF. I dug into the Arduino 1.0 code and found the LightWeightRingBuffer.h

		static inline void RingBuffer_Insert(RingBuff_t* const Buffer,
		                                     const RingBuff_Data_t Data)
		{
			*Buffer->In = Data;
			
			if (++Buffer->In == &Buffer->Buffer[BUFFER_SIZE])
			  Buffer->In = Buffer->Buffer;

			ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
			{
				Buffer->Count++;
			}
		}

So I guess the new does replace the old in a rotating ring buffer.

This is my test, Reading Continuous Serial Packets - #25 by system - Project Guidance - Arduino Forum

Erdin,

Thanks. What Arduino IDE version did you test it on?

The newest version. I think 1.0.4 or 1.5.2.
Let me test that again....
Arduino 1.5.2 and Uno board.
The first 63 characters are in the buffer and the last character is not overwritten with the newest. I disabled CR + LF to test that. So if the buffer is full, everything incoming is discarded.

Thanks Erdin. That explains a lot what I saw.