Go Down

Topic: Does serial buffer discard old data when buffer is full and new data comes? (Read 1 time) previous topic - next topic

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.

retrolefty


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

liudr

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.

BenF

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

Code: [Select]

  // 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.

liudr

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

Code: [Select]
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.

Erdin

This is my test, http://arduino.cc/forum/index.php/topic,153089.msg1149879.html#msg1149879

liudr


Erdin

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.

liudr


Go Up