日本語 English

How to Use the Ring Buffer (2/2)

Output to a CSV file

Here we explain the steps for using a ring buffer when writing the following data to a CSV file
Data is written while the execution flag is ON, one line every 10 ms

Variable nameData typeDescription
indexUDINTSequence number
DataSTRING[256]Write string

Step 1: Create the Ring Buffer

1-1. Create the ring buffer structure

Sysmac Studio data types
Create the ring buffer

The data to output to the file goes into the sData structure, so create it with matching data types
That Data structure goes into the ring buffer structure, with the array set to the size needed for buffering
The number of array elements is the buffer size

NameData typeDescription
sRingBuffer
TopUINTAdd position
BottomUINTRead position
CountUINTNumber of stored records
DatasData[1000]Buffer
IndexDINT
DataSTRING[256]

1-2. Copy functions from the sample

Sysmac Studio copy functions

Copy the RingBufPut and RingbufGet functions into Functions from the ring buffer sample

Step 2: Store in the Ring Buffer

2-1. Create the program

Ladder that stores data in the ring buffer
Sysmac Studio create program

Here the timing for putting data into the ring buffer is created by counting the scan time and turning the pls flag ON
For the data to write to the file, set a sequence number in Index of PutData (sData structure) and a dummy string in Data
PutData is added to the ring buffer with the RingBufPut function

2-2. If an error occurs

Function error

If an error like 'Cannot convert from SRINGBUFFER to ST_STRING_LITERAL.' appears, open the function's variable table, delete the data type of RingBuf on the In-Out tab once, then set the sRingBuffer type again

Delete the data type of Ringbuf
Function error
Set sRingBuffer again
Function error

Step 3: From the Ring Buffer to a File

3-1. If there is data in the ring buffer, write it to the file

Variable table
Variable table
Ladder that reads data from the ring buffer and writes it to the file
Ladder logic
  • Line 2: check the ring buffer Count, and if there is data turn DataExists ON
  • Lines 3-4: turn the writing (Write) flag ON and open the file
  • Line 5: read one item from the ring buffer into GetData (sData structure), convert GetData.Index to a string, join it with the GetData.Data string and set it in the write string (WriteString)
  • Line 6: write the write string (WriteString) to SD with the FilePut instruction
  • Line 7: when writing completes, check the ring buffer Count; if there is data, turn the NextLine flag ON to run line 5 again for the next data. If there is no data, turn the Close flag ON to close for now
  • Lines 8-9: close the file. Do not forget to close it also when an error occurs in FilePut
  • Line 10: turn ON the normal completion OK flag or the error completion NG flag

Step 4. Check operation

The program is complete, so check its operation
While the Run flag is ON, data is buffered every 10 ms and appended to the 'testRing.csv' file on SD

Normal operation

Monitor the value of Ringbuffer.Count; if it does not keep increasing and stays stable at a low number, buffer processing is keeping up and operation is normal

Overflow

If the Count value rises to the number of array elements, the buffer overflows and gaps appear
In that case, change the amount of data saved, or combine multiple lines into one WriteString in the line 5 processing to shorten processing time

This completes the ring buffer procedure

This introduced simple buffering
The data you want to write to a file will probably be a combination of various data such as numeric, REAL and date/time types
Change the contents of the sData structure to match the data you want to capture

Download

You can download the test project used here

Comments