Pages: [1]   Go Down
Author Topic: Does serial buffer discard old data when buffer is full and new data comes?  (Read 1291 times)
0 Members and 1 Guest are viewing this topic.
Central MN, USA
Offline Offline
Tesla Member
***
Karma: 75
Posts: 7269
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17303
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 75
Posts: 7269
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Offline Offline
Edison Member
*
Karma: 3
Posts: 1001
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
  // 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.
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 75
Posts: 7269
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged


Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 75
Posts: 7269
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Erdin,

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


Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 75
Posts: 7269
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Erdin. That explains a lot what I saw.
Logged


Pages: [1]   Go Up
Jump to: