Supported platforms: CODESYS 2.3, CODESYS 3.5
The function block reads and adjusts the given parameter value and writes it to control unit. The function block can be used with the parameter CSV file (see also PARAMETERS_ReadCSV).
When the function block is called for the first time, the parameter value is read from the control unit. This is also done when i_NodeID, i_Index, i_SubIndex or i_StartBit changes. When the rising edge of the input i_Update is noticed, the parameter value is updated from the control unit.
When a parameter value is changed, it will be written to the control unit. If the changed parameter value exceeds i_Min or i_Max, the value is limited to minimum or maximum value.
This function block can adjust also unit's own parameters. Only the correct values need to be set to inputs i_OwnNodeID (unit own Node ID) and i_pOD (pointer to unit's own OD). The input i_pOD can be set to value 0 if there is no need to adjust own parameters.
Input variable name |
Data type |
Description |
i_Channel |
BYTE |
Number of CAN interface. CAN1 = 0, CAN2 = 1 |
i_pValue |
DWORD |
Pointer to parameter value |
i_NodeID |
BYTE |
Module bus identification |
i_Index |
WORD |
SDO object |
i_SubIndex |
INT |
SDO object sub index |
i_StartBit |
BYTE |
Bit number |
i_DataType |
PARAMETERS_CSVType |
Parameter data type enumeration |
i_Min |
DINT |
Minimum limit for parameter value |
i_Max |
DINT |
Maximum limit for parameter value |
i_Update |
BOOL |
Rising edge of i_Update forces reading of current value |
Input variable name |
Data type |
Description |
i_itfChannel |
CANVXD_API.ICANVXD |
CAN interface. (From Code_Template_globals, for example, G_CAN1_Channel: EPEC_CANL2.Channel;) |
i_pValue |
DWORD |
Pointer to parameter value |
i_NodeID |
BYTE |
Module bus identification |
i_Index |
WORD |
SDO object |
i_SubIndex |
INT |
SDO object sub index |
i_StartBit |
BYTE |
Bit number |
i_DataType |
PARAMETERS_CSVType |
Parameter data type enumeration |
i_Min |
DINT |
Minimum limit for parameter value |
i_Max |
DINT |
Maximum limit for parameter value |
i_Update |
BOOL |
Rising edge of i_Update forces reading of current value |
i_WriteEnable |
BOOL |
Enable status for writing. FUNCTIONALITY NOT IMPLEMENTED YET. |
i_Timeout |
TIME |
Time out for the SDO request-response pair. Transfer is aborted if the response is not received during the time out time. |
i_OwnNodeID |
BYTE |
Unit's own node ID. Needed when unit own parameters are adjusted. |
i_pOD |
POINTER TO CANopenOD |
Pointer to unit's own OD. Needed when unit own parameters are adjusted. Set input to 0 if there is no need to adjust own parameters. |
Output variable name |
Data type |
Description |
o_State |
PARAMETERS_EditState |
State |
o_Error |
PARAMETERS_EditError |
Error code |
o_AbortCode |
DWORD |
Abort code |
Output variable name |
Data type |
Description |
o_ParameterValue |
DINT |
Actual parameter value at target unit. Last SDO read or written value. |
o_State |
PARAMETERS_EditState |
State |
o_Error |
PARAMETERS_EditError |
Error code |
o_AbortCode |
DWORD |
Abort code |
Actions name |
Description |
CheckLimits |
Limits written variable between i_Min and i_Max. |
SetNextState |
Handles parameter edit state. |
Global variables
VAR_GLOBAL (*One CSDO channel instance is needed to define for every CANopen device on the same network*) G_CAN1_CSDOinstances:ARRAY [1..1] OF CANopenCSDO:= (i_NodeID:=2,i_Enable:=FALSE,i_Channel:=0); |
END_VAR
|
Definitions |
|
VAR Init: BOOL; Enable: BOOL; File: STRING := 'parameters.csv'; (* File name, file is located in the same directory with the CODESYS application *) CSVParams: ARRAY[0..20] OF PARAMETERS_CSVData; (* A data array for data read from the CSV file *) Count: INT; (* The amount of the parameter rows in the CSV file *)
(*Button variables*) PrevButton: BOOL; NextButton: BOOL; PrevTrigger: R_TRIG; NextTrigger: R_TRIG;
MaxIndex: INT; CurrentIndex: INT; EditParameters: PARAMETERS_Edit; Value: DINT; END_VAR
|
Init:
|
|
(* Initialize CANopen302_SDOManager and if CSV file is used, read the file first with PARAMETERS_ReadCSV *)
IF NOT Init THEN Enable := TRUE; Count := PARAMETERS_ReadCSV(Enable, File, ADR(CSVParams)); IF Count > 0 THEN MaxIndex := Count - 1; END_IF
(* Init CSDO instance array for bus *) CANopen302_SDOManager.Init( i_CanBusNbr:=0, i_NodeId:=1, i_CsdoArrayStart:=ADR(G_CAN1_CSDOinstances), i_CsdoArraySize:=1);
Init := TRUE; END_IF
|
Code: |
|
(*When parameter is not written or read, next/previous buttons can be used*) IF EditParameters.State = EditStateIdle THEN
(* Handle buttons *) (* PREVIOUS *) PrevTrigger(CLK:=PrevButton); IF PrevTrigger.Q = TRUE THEN IF CurrentIndex > 0 THEN CurrentIndex := CurrentIndex - 1; END_IF END_IF NextTrigger(CLK:=NextButton); IF NextTrigger.Q = TRUE THEN IF CurrentIndex < Count - 1 THEN CurrentIndex := CurrentIndex + 1; END_IF END_IF END_IF
IF Count > 0 THEN EditParameters( i_Channel:= 0, (*0 = CAN1, 1 = CAN2*) i_pValue:= ADR(Value), i_NodeID:= CSVParams[CurrentIndex].NodeID, i_Index:= CSVParams[CurrentIndex].Index, i_SubIndex:= CSVParams[CurrentIndex].SubIndex, i_StartBit:= CSVParams[CurrentIndex].StartBit, i_DataType:= CSVParams[CurrentIndex].ParameterType, i_Min:= CSVParams[CurrentIndex].MinValue, i_Max:= CSVParams[CurrentIndex].MaxValue, i_Update:= FALSE, (*When this is true, the value is read again from control unit*) o_State=> , o_Error=> , o_AbortCode=> ); END_IF
(*Update CAN data*) CANVXDCheckMessages(); (*This needs to be called once in every program cycle, but it is automatically added when MultiTool Creator code template is used*)
|
Definitions |
|
VAR Init: BOOL; Enable: BOOL; File: STRING := 'parameters.csv'; (* File name, file is located in the same directory with the CODESYS application *) CSVParams: ARRAY[0..20] OF PARAMETERS_CSVData; (* A data array for data read from the CSV file *) Count: INT; (* The amount of the parameter rows in the CSV file *)
(*Button variables*) PrevButton: BOOL; NextButton: BOOL; PrevTrigger: R_TRIG; NextTrigger: R_TRIG;
MaxIndex: INT; CurrentIndex: INT; EditParameters: PARAMETERS_Edit; Value: DINT; END_VAR
|
Init:
|
|
(* Initialize CANopen302_SDOManager and if CSV file is used, read the file first with PARAMETERS_ReadCSV *)
|
Code: |
|
(*When parameter is not written or read, next/previous buttons can be used*) IF EditParameters.State = EditStateIdle THEN
(* Handle buttons *) (* PREVIOUS *) PrevTrigger(CLK:=PrevButton); IF PrevTrigger.Q = TRUE THEN IF CurrentIndex > 0 THEN CurrentIndex := CurrentIndex - 1; END_IF END_IF NextTrigger(CLK:=NextButton); IF NextTrigger.Q = TRUE THEN IF CurrentIndex < Count - 1 THEN CurrentIndex := CurrentIndex + 1; END_IF END_IF END_IF
IF Count > 0 THEN EditParameters( i_itfChannel:= G_CAN1_CANopenDevice.itfCanChannel, i_pValue:= ADR(Value), i_NodeID:= CSVParams[CurrentIndex].NodeID, i_Index:= CSVParams[CurrentIndex].Index, i_SubIndex:= CSVParams[CurrentIndex].SubIndex, i_StartBit:= CSVParams[CurrentIndex].StartBit, i_DataType:= CSVParams[CurrentIndex].ParameterType, i_Min:= CSVParams[CurrentIndex].MinValue, i_Max:= CSVParams[CurrentIndex].MaxValue, i_Update:= FALSE, (*When this is true, the value is read again from control unit*), i_Timeout:= T#1s, (* Timeout for SDO transfer*) i_OwnNodeID:= G_CAN1_CANopenDevice.NodeID, i_pOD:= ADR(G_CAN1_CANopenDevice_OD), o_State=> , o_Error=> , o_AbortCode=> ); END_IF
|
|
|
|
|
Textdisplay variable
Main.CSVParams[Main.CurrentIndex].ParameterName
Textdisplay variable
Main.CSVParams[Main.CurrentIndex].MinValue
Main.CSVParams[Main.CurrentIndex].MaxValue
Textdisplay variable
Main.CSVParams[Main.CurrentIndex].NodeID
Textdisplay variable
Main.CSVParams[Main.CurrentIndex].Index
Main.CSVParams[Main.CurrentIndex].SubIndex
Textdisplay variable
Main.CSVParams[Main.CurrentIndex].ParameterType
Textdisplay variable
Main.PrevButton
Main.NextButton
Source file Topic000315.htm
Last updated 24-Apr-2025