IEC 61131-3 Array Type

Help Contents

Introduction

You can use array types to declare a collection of elements that can be indexed by an integer value. For example:

Type
    FlowRates : Array [0..3] of LREAL;
End_Type

You access array elements using the array operator consisting of square bracket ([) and (]) characters. For example:

Var 
    Values : Array [0..3] of LREAL;
    TotalFlow : LREAL;
End_Var

    TotalFlow := Values[0] + Values[1] + Values[2] + Values[3];

The previous example could also be implemented with a Structured Text FOR Statement:

Var 
    Values : Array [0..3] of LREAL;
    TotalFlow : LREAL;
    Index : INT;
End_Var

    TotalFlow := 0;
    For Index := 0 To 3 DO
        TotalFlow := TotalFlow + Values[ Index ];
    End_For;

Arrays with more than 1 dimension

Arrays can be declared with more than 1 dimension. For example:

Type
    FlowRates : Array [0..3, 0..1] of LREAL;
End_Type

To access elements of arrays with more than 1 dimension you need multiple indexes:

Var 
    Values : Array [0..3, 0..1] of LREAL;
    TotalFlow : LREAL;
    Index : INT;
End_Var

    TotalFlow := 0;
    For Index := 0 To 3 DO
        TotalFlow := TotalFlow + Values[ Index, 0 ] + Values[ Index, 1 ];
    End_For;

Initialized Arrays

You can declare an initialized array using the assignment operator ':=' followed by initial values. For example:

Var Constant
    TemperatureTargets : Array [0..5] Of Real := 
                [ 27.0, 25.0, 23.5, 22.0, 21.0, 20.0 ];
End_Var

To initialize multiple array elements with the same value, use a repeat operator:

Var Constant
    TimeIntervals : Array [0..5] Of Time := 
                [ 3(T#30m), 3(T#1h) ];
End_Var

This example initializes array elements 0 to 2 with T#30m and elements 3 to 5 with T#1h.

Arrays of Derived Types

You can declare arrays of any Derived Data Type. For example:

Type
    TemperatureAndDuration : Struct
        Temperature : REAL;
        Duration : TIME;
    End_Struct; 
End_Type

Var
    Targets : Array[0..5] Of TemperatureAndDuration;
End_Var

You could initialize the above array declaration like this:

Var Constant
    Targets : Array[0..5] Of TemperatureAndDuration :=
    [
        ( Temperature:=27.0, Duration:=T#30m ),
        ( Temperature:=25.0, Duration:=T#30m ),
        ( Temperature:=23.5, Duration:=T#30m ),
        ( Temperature:=22.0, Duration:=T#1h ),
        ( Temperature:=21.0, Duration:=T#1h ),
        ( Temperature:=20.5, Duration:=T#1h )
    ];
End_Var

Standards Compliance

IEC 61131-3 Second Edition: Table 12.4.

IEC 61131-3 Third Edition: Table 11.4.

Further Information

Derived Data Types

To learn about other derived data types, for example STRUCT.

Elementary Types

To learn about the built-in elementary types for example LREAL, or INT.

Common Elements

To learn about other common language elements.

Character Codes

For a list of all Unicode character codes used in IEC 61131-3 code.

Glossary

For the meaning of terms used in Fernhill SCADA.