Usage
Setup
To use some functions in CSV Core the API header file will need to be included. It is important that this is included before AsDefaults. Programs written in structured text will not be able to use these functions. Header can be included in reference to library or a copy may be insterted in task and included that way.
#include <bur/plctypes.h>
// Include api before AsDefault
#include "CSVCoreAPI.h"
#ifdef _DEFAULT_INCLUDES
#include <AsDefault.h>
#endif
Basic Usage
Initialize csv structure
// Configure csv object
csvInitData(&csv, &data, sizeof(data));
csv.cfg.headerRow = 1;
Configure predef
// Configure predef
csvInitPredef(&csv.predef, 3, 0, 0, 0); // Let this auto allocated
csvFilterByHeader(&csv, &csv.predef, "id", "pressure", "temperature");
csvCastAs(&csv.predef, CSV_CORE_TYPE_UDINT, CSV_CORE_TYPE_REAL, CSV_CORE_TYPE_REAL);
ReadNextRow
To prevent program from taking up to much time in one cycle we will add a 50 lines per a cycle max.
while(count < 50
&& !(status = csvReadNextRow(&csv, &dataIndex, &pressure, &temperature))) {
// dataIndex, pressure, temperature will be populated with csv data
count++;
}
Result
VAR
csv : CSVCore_typ;
data : STRING[4000];
dataReady : BOOL;
partialData : BOOL;
status : DINT;
dataIndex : UDINT;
pressure : REAL;
temperature : REAL;
END_VAR
#include <bur/plctypes.h>
// Include api before AsDefault
#include "CSVCoreAPI.h"
#ifdef _DEFAULT_INCLUDES
#include <AsDefault.h>
#endif
void _INIT ProgramInit(void) {
// Populate data
strcpy(data,
"id,pressure,volume,temperature\n"
"1,1,0.3742,98.9\n"
"2,0.98,0.3849,103.4\n"
"3,0.72,0.5295,109.4\n");
// Configure csv object
csvInitData(&csv, &data, sizeof(data));
csv.cfg.headerRow = 1;
// Configure predef
csvInitPredef(&csv.predef, 3, 0, 0, 0); // Let this auto allocated
csvFilterByHeader(&csv, &csv.predef, "id", "pressure", "temperature");
csvCastAs(&csv.predef, CSV_CORE_TYPE_UDINT, CSV_CORE_TYPE_REAL, CSV_CORE_TYPE_REAL);
}
void _CYCLIC ProgramCyclic(void) {
if(dataReady) {
count = 0;
while(count < 50
&& !(status = csvReadNextRow(&csv, &dataIndex, &pressure, &temperature))) {
// dataIndex, pressure, temperature will be populated with csv data
count++;
}
if(status == CSV_CORE_ERR_END_OF_DATA) {
dataReady = 0;
}
else if(status == CSV_CORE_ERR_NO_NEWLINE) {
// Handle partial data
}
else {
// Throw error
}
}
// Load data
// ...
// Populates data, dataReady, partialData
if(loadData) {
loadData = 0;
dataReady = 1;
partialData = 0;
strcpy(data,
"id,pressure,volume,temperature\n"
"1,1,0.3742,98.9\n"
"2,0.98,0.3849,103.4\n"
"3,0.72,0.5295,109.4\n");
}
}