Skip to content

Arrays, Structs, and UDTs

Arrays And Loops

Arrays are useful when the same logic is applied to multiple similar objects.

VAR_INPUT
    arrxboGeneratorReady : Array[1..4] of Bool;
END_VAR

VAR_OUTPUT
    xboAnyGeneratorReady : Bool;
    xboAllGeneratorsReady : Bool;
END_VAR

VAR_TEMP
    _inIndex : Int;
END_VAR

BEGIN
    xboAnyGeneratorReady := FALSE;
    xboAllGeneratorsReady := TRUE;

    FOR _inIndex := 1 TO 4 DO
        xboAnyGeneratorReady := xboAnyGeneratorReady OR arrxboGeneratorReady[_inIndex];
        xboAllGeneratorsReady := xboAllGeneratorsReady AND arrxboGeneratorReady[_inIndex];
    END_FOR;
END_FUNCTION_BLOCK

Initialize the result before the loop. Otherwise the result may accidentally keep an old value from the previous cycle.

Structs And UDTs

A STRUCT groups related values. A UDT gives that structure a reusable type name.

TYPE "UDT_DriveStatus"
VERSION : 0.1

   STRUCT
      xboReady : Bool;
      xboRunning : Bool;
      xboFaultActive : Bool;
      reActualSpeedRpm : Real;
   END_STRUCT;

END_TYPE

The UDT can then be used in an FB or DB:

VAR_INPUT
    stDriveStatus : "UDT_DriveStatus";
END_VAR

VAR_OUTPUT
    xboDriveAvailable : Bool;
END_VAR

BEGIN
    xboDriveAvailable :=
        stDriveStatus.xboReady
        AND NOT stDriveStatus.xboFaultActive;
END_FUNCTION_BLOCK

Use structs when the values belong together in the engineering domain: drive status, command data, alarm entries, generator data, HMI status, and similar interfaces.