Example: Using the CSV Utility SL Library
Product: CODESYS IIOT Libraries SL
The
CSVReader
folder contains the components to read CSV files.The initialization and the passing of the file path is done by the
CSVReaderInit
function block.The values of the CSV file can be read individually, line by line, or all at one time. The
NextLine
,NextElement
, orReadAll
function blocks are used to read the values.The the
CSVReaderExample
application in theCSV Utility Examples.project
sample project shows how to use these components.The maximum element length has to be greater than or equal to the maximum column length of the CSV file.
The
CSVWriter
folder contains the components to write CSV files.Data is saved in three steps:
The first step is to initialize the library. At this time, the path and the file name are passed.
The second step is to store the data in a buffer.
The third step is to write the buffer to a CSV file.
Description
The example describes how you can use the library function blocks in an application.
Classical procedural programming
The function blocks for classical programming based on function blocks are located in the FunctionBlocks
folder.
The function blocks in this folder are designed for asynchronous processing. They were derived from the CBM.ETrigA
function block.
For each function block, a visualization object has also been created which can be used in other projects.
An example of this can be found in the CSV Utility Examples.project
project in the CSVWriterCFCExample
application.
Initialization: This is done by means of the Init
function block. Here the storage path and the file name can be passed.
To add values: Values can be written to the buffer by means of the AddXXX
function blocks. A new line can be added by means of the NewLine
function block. A new file is created with NewFile
.
Save: The buffer is saved by means of the WriteFile
function block. The values are written in the standard format of the corresponding data type.
Visualizations: A visualization of the inputs and outputs exits for each function block.
Using the function blocks
All function blocks in the FunctionBlocks
folder react to a rising edge at the xExecute
input.
Pay attention that these operations are executed asynchronously and the xDone
, xError
, and eError
outputs have to be sampled accordingly. The processing does not have to be done within a cycle.
Object-oriented programming
The function blocks and interfaces for object-oriented programming are located in the Objects
folder.
An example for this can be found in the CSV Utility Examples.project
project in the CSVWriterSTExample
application.
The CSVWriter
function block provides basic methods for saving CSV files.
Initialization: At the beginning, the InitSave
method has to be called to create a new file or open an existing file.
To add values: When one of the Add
methods is called, the value is written to the buffer. A new line is appended as soon as the corresponding method is called. If the buffer is full, then an error is returned. As soon as data has been saved, space is available again for new values.
AddXXX
: Inserts data at the end of the bufferNewLine
: Appends a new line
Save:
Save
: Saves the value of the buffer as a CSV file
Additional methods:
GetFileSize
: Returns the size of the current fileNewFile
: Creates a new file, either with a specified name or an automatically generated name
Error: All methods have an error field to store a possible error of the type CSV_ERROR
.
Additional information
The CSV Utility SL Examples.project
sample project contains a sample application for CSVReader
and two sample applications for CSVWriter
.
Application: CSVReaderExample
The example shows the three ways of reading values from a CSV file. The Prog
program shows how to use the CSVReaderInit
, NextElement
, ReadAll
, and NextLine
function blocks. The contents of the CSVReader.csv
file is displayed in a visualization.
Tip
To test the examples on a CODESYS Control, the CSV file CSVReader
can be copied from the target directory of the installation to the c:\temp
directory.
Application: CSVWriterCFCExample
This example uses a CFC example to show how to use function blocks from the CSVWriter
library. It also shows how to use the integrated visualization.
Functionality:
The
WriteValues
program creates a CSV file in the"c:/temp/CSVWriterCFCExampleData.csv"
folder.In the visualization, clicking the Click here to add button triggers the writing to the file.
The initialization is done by means of the
CSV.Init
function block. The path and the file name are passed here directly.The
AddWord
,AddString
, andNewLine
function blocks show how to add values to the internal buffer.The
ctu
counter increments the line number during each event. After a new line is added, the values are written to a CSV file by means of theWriteFile
function block.
Application: CSVWriterSTExample
This example shows how to write variable values to a CSV file by means of object-oriented programming or sequential programming. Each process is executed in two tasks. One task writes the values to the buffer and the other saves the data from the buffer to the file.
Functionality:
ObjectOriented_Save
(object-oriented saving)This program calls the
Save
method fromDataObject
, where the save operation is implemented.ObjectOriented_Write
(object-oriented writing)This program calls the
Write
method fromDataObject
where the write operation is implemented.DataObject
This function block implements the
ICSVObject
interface. TheWrite
method includes the local variables which will be saved later. The correspondingAddXXX
method is called for each data type. After a certain number of lines, a new file is created with other values. As soon as an error occurs, theError
status is reached. Then nothing else will be written. TheSave
method implements the save operation. At the beginning, theInitSave
method of theCSVWriter
function block instance is called. After that, only theSave
method still has to be called.To activate the following programs, the POUs in the tasks have to be changed.
Sequential_Save
(sequential saving)The save operation also has to be started with the
InitSave
method. After the CSV file reaches a specific size, a new file is created. If no file name is specified, the existing name with appended numbering is used.Sequential_Write
(sequential writing)Different variables are written to the buffer. If this is full, then the
EndOfBuffer
error is returned. In contrast to the writing operation described above, the error is ignored this time and the buffer continues to be written. As a result, some data is lost, but the program continues to run. As soon as the next save operation is started, the buffer is free again.
System requirements and restrictions
Programming system | CODESYS Development System (version 3.5.18.0 or higher) |
Runtime system | All Note: Use the free |
Add-on components | - |
Note
DOWNLOAD projects