Data Type ‘ARRAY’¶
CODESYS supports one, two, and three-dimensional arrays of elementary data types. You can define arrays in the declaration part of a block and in the global variable list.
Arrays of variable length can be used only VAR_IN_OUT variables of function blocks, methods, and functions can be declared with the data type of an array of variable length.
For arrays of variable length, the LOWER_BOUND(<array name>,<dim>)
and UPPER_BOUND(array name,<dim>)
operators yield the lower and upper limits.
Syntax for the declaration of an array with a defined length:
<array name> : ARRAY [<ll1>..<ul1>,<ll2>..<ul2>] OF <elem. type>
ll1
, ll2
, ll3
define the lower limit of a field dimension, and ul1
, ul2
and ul3
define the upper limit. These limiting values must be integer values.
Example
Card_game: ARRAY [1..13, 1..4] OF INT;
Syntax for the declaration of an array with a variable length:
<array name> : ARRAY [\*|, \*|, \*] OF <data type>;
Example
FUNCTION SUM: INT; // One-dimensional arrays of variable lengths can be passed to this addition function.
VAR_IN_OUT
A: ARRAY [*] OF INT;
END_VAR
VAR
i, sum2 : DINT;
END_VAR
sum2:= 0;
FOR i:= LOWER_BOUND(A,1) TO UPPER_BOUND(A,1) // The length of the respective array is determined.
sum2:= sum2 + A[i];
END_FOR;
SUM:= sum2;
Initializing arrays
Example of the full initialization of an array:
arr1 : ARRAY [1..5] OF INT := [1,2,3,4,5];
arr2 : ARRAY [1..2,3..4] OF INT := [1,3(7)]; (* short for 1,7,7,7 *)
arr3 : ARRAY [1..2,2..3,3..4] OF INT := [2(0),4(4),2,3];
\ (* short for 0,0,4,4,4,4,2,3 *)
Example of the initialization of an array of a structure:
Structure definition:
TYPE STRUCT1
STRUCT
p1:int;
p2:int;
p3:dword;
END_STRUCT
END_TYPE
Array initialization:
ARRAY[1..3] OF STRUCT1:= [(p1:=1,p2:=10,p3:=4723),(p1:=2,p2:=0,p3:=299),(p1:=14,p2:=5,p3:=112)];
Example of the partial initialization of an array:
arr1 : ARRAY [1..10] OF INT := [1,2];
Elements that you explicitly did not assign initialization values are initialized with the default value of the basic data type. In the example above, CODESYS initializes the elements arr1[3]
to arr1[10]
with 0.
Example of the initialization of an array of function blocks with additional parameters in FB_init:
Function block FB and method FB_Init with two parameters:
FUNCTION_BLOCK FB
VAR
\ _nId : INT;
\ _lrIn : LREAL;
END_VAR
METHOD FB_Init : BOOL
VAR_INPUT
\ bInitRetains : BOOL;
\ bInCopyCode : BOOL;
\ nId : INT;
\ lrIn : LREAL;
END_VAR
_nId := nId;
_lrIn := lrIN;
Array declaration with initialization:
PROGRAM PLC_PRG
VAR
inst : POU(nId := 11, lrIn := 33.44);
\ ainst : ARRAY [0..1, 0..1] OF POU[(nId := 12, lrIn := 11.22), (nId := 13, lrIn := 22.33), (nId := 14, lrIn := 33.55),(nId := 15, lrIn := 11.22)];
END_VAR
Access to array elements
In a two-dimensional array, you access the elements as follows:
<array name>[Index1,Index2]
Example
Card_game [9,2]
Note
Please note the capability of using the implicit monitoring function CheckBounds
to monitor the violation of field limits in runtime mode.
See also