PLC

ARRAY

TwinCAT supports one-, two- and three-dimensional arrays of elementary data types. You can define arrays in the declaration part of a function block and in the global variable lists.

Only VAR_IN_OUT variables of function blocks, functions and methods can be declared with the data type of an array of variable length.

For variable-length arrays, the operators LOWER_BOUND (<array name>,<dim>) and UPPER_BOUND (array name,<dim>) provide the lower and upper bounds.

Syntax for declaring an array with a defined length:

<array_name> : ARRAY [<ll1>..<ul1>,<ll2>..<ul2>] OF <elem. type>

ll1, ll2, ll3 indicate the lower bound of a field dimension, ul1, ul2 and ul3 the upper limit. These limits must be integral values.

Example:

aCardGame : ARRAY [1..13, 1..4] OF INT;

Syntax for declaring an array with variable length:

<array_name> : ARRAY [*|, *|, *] OF <data type>;

Example:

//An diese Additionsfunktion können Arrays mit verschiedener Längen übergeben werden
FUNCTION F_Sum : DINT;
VAR_IN_OUT
    aSample: ARRAY [*] OF INT;
END_VAR
VAR
    nI, nSum2 : DINT;
END_VAR

nSum2 := 0;

//Die Länge des jeweiligen Arrays wird ermittelt
FOR nI:= LOWER_BOUND(aSample,1) TO UPPER_BOUND(aSample,1) DO
    nSum2 := nSum2 + aSample[nI];
END_FOR;

F_Sum := nSum2;
 

Initialization of arrays

Example: Complete initialization of an array

aSample1 : ARRAY [1..5] OF INT := [1,2,3,4,5];
aSample2 : ARRAY [1..2,3..4] OF INT := [1,3(7)]; (*kurz für 1,7,7,7*)
aSample3 : ARRAY [1..2,2..3,3..4] OF INT := [2(0),4(4),2,3]; (*kurz für 0,0,4,4,4,4,2,3*)

Example: Initialization of an array of a structure

Structure definition:

TYPE ST_STRUCT1
STRUCT
    n1 : int;
    n2 : int;
    n3 : dword;
END_STRUCT
END_TYPE

Array initialization:

aSample4 : ARRAY[1..3] OF ST_STRUCT1 := [(n1:=1,n2:=10,n3:=4723),(n1:=2,n2:=0,n3:=299),(n1:=14,n2:=5,n3:=112)];

Example: Partial initialization of an array

aSample5 : ARRAY [1..10] OF INT := [1,2];

Elements, to which you do not explicitly assign a value as an initialization value, are initialized with the default value of the basic data type. In the example above, TwinCAT initializes the elements aSample5[3] to aSample5[10] with 0.

Example: Initialization of an array of function blocks with additional parameters in FB_init

Function block FB_Sample:

FUNCTION_BLOCK FB_Sample
VAR
    nId : INT;
    fIn : LREAL;
END_VAR

Method FB_Sample.FB_init with two parameters:

METHOD FB_init : BOOL
VAR_INPUT
    bInitRetains : BOOL;
    bInCopyCode  : BOOL;
    nId_Init     : INT;
    fIn_Init     : LREAL;
END_VAR
nId := nId_Init;
fIn := fIn_Init;

Declaration of a function block:

PROGRAM MAIN
VAR
    fbSample   : FB_Sample(nId_Init := 11, fIn_Init := 33.44);
END_VAR

Declaration of an FB array with different parameters for all FB instances:

PROGRAM MAIN
VAR
    afbSample1 : ARRAY [0..1, 0..1] OF FB_Sample[(nId_Init := 12, fIn_Init := 11.22),
                                                 (nId_Init := 13, fIn_Init := 22.33),
                                                 (nId_Init := 14, fIn_Init := 33.44),
                                                 (nId_Init := 15, fIn_Init := 44.55)];
END_VAR

Declaration of an FB array with the same parameters for all FB instances:

PROGRAM MAIN
VAR
    afbSample2 : ARRAY [0..1, 0..1] OF FB_Sample[(nId_Init := 100, fIn_Init := 123.456)];
END_VAR
 
 

Access to array components:

In a two-dimensional array, you access the elements as follows:

<Array name>[Index1, Index2]

Example:

aCardGame[9,2]
 
Note
Note the option to monitor violation of field bounds at runtime using the implicit monitoring function CheckBounds.

See also: