EcoStruxure Machine Expert version 1.1 does not support the M258, LMC058 and LMC078 controllers.

Arrays

Overview

One, two and three-dimensional fields (arrays) are supported as elementary data types. 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. You can declare arrays with defined length and with variable length.

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.

Syntax for the Declaration of an Array with Defined Length

<Array_Name> : ARRAY [<ll1>..<ul1>,<ll2>..<ul2>,<ll3>..<ul3>] OF <elem. type>

ll1, ll2, ll3 identify the lower limit of the field range.

ul1, ul2, and ul3 identify the upper limit of the field range.

The range values have to be of type integer.

Example for the Declaration of an Array with Defined Length

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

Syntax for the Declaration of an Array with Variable Length

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

Example for the Declaration of an Array with Variable Length

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) // The length of the respective array is
determined.
  sum2:= sum2 + A[i];
END_FOR;
SUM:= sum2;

Initializing Arrays

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 the Initialization of an Array of Function Blocks with Additional Parameters in FB_Init

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

Accessing Array Elements

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

<Array name>[Index1,Index2]

Example:

Card_game [9,2]

Check Functions on Array Bounds

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 POUs for implicit checks 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.

Example for the Use of Function CheckBounds

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:

oindex: field element index

olower: the lower limit of the field range

oupper: 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.

Exceeding the Upper Limit of the Array a

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.