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
Without a ring buffer
- Open the file
- Check that the pulse is ON
- Get the write data
- Write to the file
- If the end flag is OFF, return to 2.
- 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
- Check that the pulse is ON
- Get the write data
- Write to the buffer
- If the end flag is OFF, return to 1.
- If the end flag is OFF, return to 2.
- If the end flag is ON, close the file and finish
From the buffer to the file
- Is there data in the buffer?
- Open the file
- Read data from the buffer
- Write the data to the file
- If there is no data in the buffer, close the file
- If the end flag is OFF, return to 1.
- 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
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
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
