日本語 English
KVKV-X

Ring buffer

What Is a Ring Buffer?

A ring buffer is a typical FIFO (first in, first out) buffer used to prevent data loss in production data collection, serial communication, EtherNet/IP communication, logging and similar.

The KEYENCE KV series has no general-purpose ring buffer function, so it is convenient to implement one as a user-defined FB.

Relationship to FIFO (First In, First Out)

A ring buffer is a typical data structure for implementing FIFO (First In First Out) efficiently.

Overview

A buffer is a memory area that temporarily holds data to absorb and smooth out timing differences when exchanging data with a storage device (such as an SD card) or an external device
The ring buffer is one of the most typical buffer algorithms

The figure below shows a ring buffer with 12 slots, with data being stored and taken out at the same time
Top is the write pointer and Bottom is the read pointer. Orange slots contain data
Each data slot is defined with a structure, and the ring buffer is built as an array of that structure

How the ring buffer works

The ring buffer structure looks like this:

RingBuffer
|
+- Top
+- Bottom
+- Count
+- Data[] (array of any data type)

Data can also be a structure.

Behavior When the Buffer Is Full

If new data is written while the ring buffer is full, the oldest data is overwritten. This suits uses where you always want to keep the latest data.

When There Is No Data

If the number of data in the buffer (Count) is 0, RingBufGet ends without getting anything.

Symbols

RingBufPut

FB that stores data in the ring buffer
ringbufput function
In-out
RingBuf
Ring buffer structure
Input
Data
Data to buffer

RingBufGet

FB that stores data in the ring buffer
ringbufget function
In-out
RingBuf
Ring buffer structure
Output
Data
Retrieved data

RingBufPut

FB that stores data in the ring buffer
ringbufput function
In-out
RingBuf
Ring buffer structure
Input
Data
Data to buffer

RingBufGet

FB that stores data in the ring buffer
ringbufget function
In-out
RingBuf
Ring buffer structure
Output
Data
Retrieved data

Example

Structures

sData

Structures

sRingBuffer

Structures

Ladder logic

Sample ladder


The data being buffered is the contents of the structure sData. One TIME (duration) value, 10 bits (B) and 10 DINT values (DI) are buffered together as one record
The ring buffer size is 1000 records, as specified by sRingBuffer.Data

When the PutTrigger contact turns ON, the contents of PutData are stored in RingBuffer
When the GetTrigger contact turns ON, data is taken out of RingBuffer and placed in GetData

Structures

sData

Structures

sRingBuffer

Structures

Ladder logic

Sample ladder

The data being buffered is the contents of the structure sData. One TIME (duration) value, 10 bits (B) and 10 DINT values (DI) are buffered together as one record
The ring buffer size is 1000 records, as specified by sRingBuffer.Data

When the PutTrigger contact turns ON, the contents of PutData are stored in RingBuffer
When the GetTrigger contact turns ON, data is taken out of RingBuffer and placed in GetData

Source

RingBufPut
RingBufGet

Features of this function

  • Structures can be stored as-is
  • Capacity can be changed just by changing the array size
  • Usable as a FIFO
  • Supports both KV and KV-X
  • The library can also be used on its own

Download

RingBufferKVXLib.kufb (library only)

* Operation on the KV-X was verified with the simulator

Comments