An array is a collection of data elements of the same data type. One- and multi-dimensional arrays of fixed or variable length are supported. You can define arrays both in the declaration part of a POU and in the global variable lists. You can also use implicit boundary checks.
The data type ARRAY with variable length can only be declared for VAR_IN_OUT variables of function blocks, methods, and functions. Use the operators LOWER_BOUND(<array name>,<dim>)
and UPPER_BOUND(<array name>,<dim>)
to get the lower and upper limits of this array.
__NEW
are not allowed.
<Array_Name> : ARRAY [<ll1>..<ul1>,<ll2>..<ul2>,..,<lln>..<uln>] OF <elem. type>
ll1, ll2, lln
identify the lower limit of the field range.
ul1, ul2
, and uln
identify the upper limit of the field range.
The range values have to be of type integer.
<Array name> :ARRAY[* ( , * )+ ] OF <data type>;
// (...)+
: One or more further dimensions
FUNCTION SUM: INT; // Onedimensional
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) DO // The length of the respective array is determined.
sum2:= sum2 + A[i];
END_FOR;
SUM:= sum2;
Example for complete 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 where no value is pre-assigned are initialized with the default initial value of the basic type. In the previous example, the elements arr1[3]...arr1[10]
are therefore initialized with 0.
Example of a 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;
Example of array declaration with initialization:
PROGRAM PLC_PRG
VAR
inst : FB(nId := 11, lrIn := 33.44);
ainst : ARRAY [0..1, 0..1] OF FB[(nId :=
12, lrIn := 11.22), (nId := 13, lrIn :=
22.33), (nId := 14, lrIn := 33.55),(nId := 15,
lrIn := 11.22)];
END_VAR
In a two-dimensional array, access the elements as follows:
<Array name>[Index1,Index2]
Example:
Card_game [9,2]
In order to access an array element properly during runtime, the function CheckBounds
has to be available to the application. For information on inserting the function, refer to the description of the function.
This check function has to treat boundary violations by an appropriate method (for example, by setting a detected error flag or adjusting the index). The function is called implicitly as soon as a variable of type ARRAY is assigned.
The default implementation of the check function is the following:
Declaration part:
// Implicitly generated code : DO NOT EDIT
FUNCTION CheckBounds : DINT
VAR_INPUT
index, lower, upper: DINT;
END_VAR
Implementation part:
// Implicitly generated code : Only an Implementation suggestion
IF index < lower THEN
CheckBounds := lower;
ELSIF index > upper THEN
CheckBounds := upper;
ELSE
CheckBounds := index;
END_IF
When called, the function gets the following input parameters:
index: field element index
lower: the lower limit of the field range
upper: the upper limit of the field range
As long as the index is within the range, the return value is the index itself. Otherwise, in correspondence to the range violation either the upper or the lower limit of the field range will be returned.
The upper limit of the array a is exceeded in the following example:
PROGRAM PLC_PRG
VAR
a: ARRAY[0..7] OF BOOL;
b: INT:=10;
END_VAR
a[b]:=TRUE;
In this example, the implicit call to the CheckBounds
function preceding the assignment affects that the value of the index is changed from 10
into the upper limit 7
. Therefore, the value TRUE is assigned to the element a[7] of the array. This is how you can correct attempted access outside the array range via the function CheckBounds
.