Access to Variables in Arrays, Structures, and Blocks

Access to array elements

Syntax

<Name der Arrayvariablen> [ <kommaseparierte Liste der Dimensionsindizes> ]

<Name der Arrayvariablen>

Sample: aSample

More information: Assigning identifiers

<kommaseparierte Liste der Dimensionsindizes>

One index per dimension, so that one element of the array is identified

Sample: 2

The index is valid from the index minimum to the index maximum.

Sample: 0..9

Samples

One-dimensional array with ten components

// Declaration
VAR
    aSample : ARRAY[0..9] OF INT;
    nVar    : INT;
END_VAR
// Implementation
nVar := aSample[2];

Two-dimensional array with two times two components

//Declaration
VAR
    aCardGame : ARRAY[1..2, 3..4] OF INT;
    nVar1     : INT;
END_VAR
//Implementation
nVar1 := aCardGame[1, 3];

More information: ARRAY

Access to structure components

Syntax

<Name der Strukturvariablen>.<Name der Komponente>

<Name der Strukturvariablen>

Sample: sPolygon

More information: Assigning identifiers

<Name der Komponente>

Sample: aStart

Samples

// Declaration type
TYPE ST_POLYGONLINE :
STRUCT
    aStart  : ARRAY[1..2] OF INT := [-99, -99];
    aPoint1 : ARRAY[1..2] OF INT;
    aPoint2 : ARRAY[1..2] OF INT;
    aPoint3 : ARRAY[1..2] OF INT;
    aPoint4 : ARRAY[1..2] OF INT;
    aEnd    : ARRAY[1..2] OF INT := [99, 99];
END_STRUCT
END_TYPE
//Declaration structure variable
VAR
    sPolygon : ST_POLYGONLINE;
    nPoint   : INT;
END_VAR
//Implementation
nPoint := sPolygon.aPoint1[1];

More information: Structure

Access to variables in programming blocks

Syntax

<POU Name>.<Variablenname>

<POU Name>

Name of a function block instance (FUNCTION_BLOCK) or a program (PROGRAM)

Sample: fbController

More information: Assigning identifiers

<Variablenname>

POU variable

Sample: bStart

Samples

FUNCTION_BLOCK FB_SAMPLE
VAR_INPUT
    bStart : BOOL;
END_VAR
VAR_OUTPUT
END_VAR
VAR
    nState  : INT;
END_VAR
IF bStart = TRUE THEN
    nState := 1;
END_IF
PROGRAM MAIN
VAR
    fbSample : FB_Sample;
END_VAR
fbSample();
fbSample.bStart := TRUE;

More information: Object Function block, Object Program

See also: