Supported platforms: CODESYS 2.3, CODESYS 3.5

PARAMETERS_ReadCSV (FUN)

Description

The function reads the given parameter CSV file to a struct array, which is defined by the application. The pointer to the PARAMETERS_CSVData structure array is given as an input to the function. The first line describes the column headers.

 

Inputs

CODESYS 2.3CODESYS 2.3

Input variable name

Data type

Description

i_Enable

BOOL

Trigger to read the CSV file

i_File

STRING

File path and name

i_pCSV

DWORD

Pointer to struct array

 

 

CODESYS 3.5CODESYS 3.5

Input variable name

Data type

Description

i_Enable

BOOL

Trigger to read the CSV file

i_File

STRING

File path and name

i_ParameterMaxCount

INT

Maximum number of parameters

i_pCSV

DWORD

Pointer to struct array

i_itfExtraData

IExtraColumnsData

Interface to application specific column data handler function.

 

 

Return value

Return value

Data type

Description

PARAMETERS_ReadCSV

INT

Amount of parameters read from file. Negative value indicates error:

-1 = File open failed

-2 = Null pointer i_pCSV = 0

 

Outputs

CODESYS 3.5CODESYS 3.5

Output variable name

Data type

Description

o_CSVErrorCode

PARAMETERS_CSVError

CSV error code

o_CSVErrorLine

INT

Number of CSV line containing error.

 

 

Example code

CODESYS 2.3CODESYS 2.3

Definitions:

 

VAR

  Count: INT; (* The amount of the parameter rows in the CSV file *)

  File:STRING:='Parameters.csv'; (* The file name, the file is located in the same directory with

                                   CODESYS application*)

  CSVData:ARRAY [0..150] OF PARAMETERS_CSVData; (* A data array for data read from the CSV file *)

  DoInit:BOOL:=FALSE; (*Read CSV once*)

END_VAR

 

 

Code:

 

 

IF NOT DoInit THEN

  Count:=PARAMETERS_ReadCSV(ReadFile, File, ADR(CSVData));

 

  IF Count > 0 THEN

     DoInit:=TRUE;

  END_IF

END_IF

 

 

 

 

CODESYS 3.5CODESYS 3.5

 

Definitions:

 

VAR_GLOBAL CONSTANT

    G_NUMBER_OF_PARAMETERS:UINT := 100; (* Number of application parameters *)

END_VAR

 

VAR

  Count: INT; (* The amount of the parameter rows in the CSV file *)

  File:STRING:='/opt/user/userapp/Parameters_Network1.csv'; (* The file path and name*)

  CSVData:ARRAY [1..G_NUMBER_OF_PARAMETERS] OF PARAMETERS_CSVData; (* A data array for data read from the CSV file *)

  DoInit:BOOL:=FALSE; (*Read CSV once*)

  ErrorInLine:INT;

  ErrorCode:PARAMETERS_CSVError;

END_VAR

 

 

Code:

 

 

IF NOT DoInit THEN

  Count:=PARAMETERS_ReadCSV(i_Enable:=TRUE,

          i_File:=File,

          i_ParameterMaxCount:=G_NUMBER_OF_PARAMETERS,

          i_pCSV:=ADR(CSVData),

          i_itfExtraData:=0,

          o_CSVErrorCode=>ErrorCode,

          o_CSVErrorLine=>ErrorInLine);

 

  IF Count > 0 THEN

     DoInit:=TRUE;

  ELSE

     (* Handle error here *)

     ;

  END_IF

END_IF

 

 

 

 

Example code of application specific column handler function

CODESYS 3.5CODESYS 3.5

 

Definitions:

 

VAR_GLOBAL CONSTANT

    G_NUMBER_OF_PARAMETERS:UINT := 100; (* Number of application parameters *)

END_VAR

 

VAR

    Count:INT; (* The amount of the parameter rows in the CSV file *)

    File:STRING:='/opt/user/userapp/Parameters_Network1.csv'; (* The file path and name*)

    PARData:ARRAY [0..G_NUMBER_OF_PARAMETERS-1] OF PARAMETERS_CSVData; (* Parameter data from the CSV file *)

    PAR_Ext:ARRAY [0..G_NUMBER_OF_PARAMETERS-1] OF PARAMETERS_Extras;  (* Application specific data *)

 

    DoInit:BOOL:=FALSE; (*Read CSV once*)

    ErrorInLine:INT;

    ErrorCode:PARAMETERS_CSVError;

 

    

END_VAR

 

 

Data types:

 

TYPE PARAMETERS_Extras :

STRUCT

    UserLevel_Write : BYTE; (* Required user level to set parameter value *)

    UserLevel_Read : BYTE;  (* Required user level to see parameter value *)

    Hidden : BYTE;

END_STRUCT

END_TYPE

   

 

 

Function block for interface IExtraColumnsData

Definitions:

 

 

FUNCTION_BLOCK ExtraColumnsData IMPLEMENTS  IExtraColumnsData

VAR_INPUT

END_VAR

VAR_OUTPUT

END_VAR

VAR

END_VAR

 

Code:

 

 

 

Implementing method of function block

Definitions:

 

 

METHOD CurrentColumnData : STRING

VAR_INPUT

    i_HeaderData:STRING;

    i_ParameterData:STRING;

    i_CSVRow:INT;

END_VAR

VAR

    AdjustedRow:INT; (* Example's parameter indexing starts from 0, thus it has to be adjusted. *)

END_VAR

 

Code:

 

 

IF i_CSVRow > 0 THEN

    AdjustedRow := i_CSVRow - 1; (* CSV reading counts parameters from 1.. *)

    IF i_HeaderData = '[UserLevelWrite]' THEN          

        PAR_Ext[AdjustedRow].UserLevel_Write:= STRING_TO_BYTE (i_ParameterData);

    ELSIF i_HeaderData = '[UserLevelRead]' THEN        

        PAR_Ext[AdjustedRow].UserLevel_Read:= STRING_TO_BYTE (i_ParameterData);        

    ELSIF i_HeaderData = '[Hidden]' THEN          

        PAR_Ext[AdjustedRow].Hidden:= STRING_TO_BYTE (i_ParameterData);                                              

    END_IF

END_IF

 

 

 

Code:

 

 

IF NOT DoInit THEN

  Count:=PARAMETERS_ReadCSV(i_Enable:=TRUE,

          i_File:=File,

          i_ParameterMaxCount:=G_NUMBER_OF_PARAMETERS,

          i_pCSV:=ADR(CSVData),

          i_itfExtraData:=ExtraColumnsData ,

          o_CSVErrorCode=>ErrorCode,

          o_CSVErrorLine=>ErrorInLine);

 

  IF Count > 0 THEN

     DoInit:=TRUE;

  ELSE

     (* Handle error here *)

     ;

  END_IF

END_IF

 

 

 

 

 

 

Source file Topic000316.htm

Last updated 24-Apr-2025