日本語 English

How to Use the Ring Buffer (1/2)

Overview

There are many kinds of buffers; here we cover the typical ring buffer

  • Situations where a ring buffer is needed
  • How to use the sample

These two topics are explained
For an explanation of the ring buffer, see the sample

Situations Where a Buffer Is Needed

Buffers used in PLC control include output buffers, which anticipate being kept waiting by the other side when continuously writing data one way to a DB or SD card as in logging, and input buffers, which anticipate a lot of data arriving at once when sequentially processing data received from connected devices

Why use a buffer

Let's look at the effect of a buffer in processing that writes a CSV text file to an SD card at a fixed period

Ring buffer processing flow comparison
FFT graph NA HMI

Without a ring buffer

  1. Open the file
  2. Check that the pulse is ON
  3. Get the write data
  4. Write to the file
  5. If the end flag is OFF, return to 2.
  6. If the end flag is ON, close the file and finish

With a ring buffer

Writing data to the buffer and reading from the buffer to write to the file proceed in parallel

Write to the buffer

  1. Check that the pulse is ON
  2. Get the write data
  3. Write to the buffer
  4. If the end flag is OFF, return to 1.
  5. If the end flag is OFF, return to 2.
  6. If the end flag is ON, close the file and finish

From the buffer to the file

  1. Is there data in the buffer?
  2. Open the file
  3. Read data from the buffer
  4. Write the data to the file
  5. If there is no data in the buffer, close the file
  6. If the end flag is OFF, return to 1.
  7. Finish when the end flag is ON

Collection results at a 10 ms period

We collect about 200 bytes of indexed data each time and look at the results

Checking the trace of data reads and file write completions with a fixed-period pulse

Without ring buffer
With ring buffer

Without ring buffer

At the points marked × (missing), it was time to acquire data, but it could not be acquired because file writing was in progress, leaving gaps in the data
The first write is missing two data points, and there are also gaps around 220 ms

Looking at the intervals of write completion, even though writing starts at a fixed period, variation in write time can be seen
SD writes and communication processing have this kind of variation, so it must be taken into account when writing continuously

With ring buffer

With a buffer, the fixed-period pulse and the write completion timing are asynchronous, so the timing shifts
What matters here is that the number of buffered data items does not exceed the buffer capacity (number of array elements)

Here the number of buffered data items stays at about two, so the data is written to the file without gaps
This can be confirmed by the indexes in the created file being written without gaps

Collection results at a 6 ms period

Without a buffer, gaps appeared at 10 ms, so it was not tested

With ring buffer

Data is added faster than it can be written, so the number of buffered items keeps increasing
It reached 400 items at about 6500 ms

It cannot be used when buffering forever at a fixed period, but when buffered data arrives irregularly, I think it can absorb the load if you create an appropriate capacity while monitoring the number of items

Result

Without a buffer, variation in SD writes caused gaps in the data
This variation is affected by differences between models and SD cards, so placing a buffer absorbs it and prevents data loss
When data loss occurs, consider placing a buffer

Not only file operations but also communication and DB inserts have even larger variation, so it is also effective when adding data frequently

From the next page,

Explains the steps for using the ring buffer functions RingbufPut and RingbufGet

Download

You can download the test project used here

Comments