Program Organization Units

Structured Text code is organized into program organization units (POUs). There are three kinds of POU:

  • Program: The top-level unit, which is executed by the SoftPLC in each scan.
  • Function: A unit that computes a result from its inputs, without remembering anything between calls.
  • Function Block: A unit with state, which is instantiated like a variable. Each instance keeps its own values between calls.

POUs can be declared in any Structured Text file, in any order, and used throughout the project. The object orientation page describes classes, which are a fourth kind of POU.

Contents

Programs

A program is declared between PROGRAM and END_PROGRAM. Its variables keep their values between scans.

PROGRAM Main
VAR
    Start AT %IX0.0 : BOOL;
    Stop AT %IX0.1 : BOOL;
    Motor AT %QX0.0 : BOOL;
END_VAR

// A start/stop latch, where Stop takes priority.
Motor := (Start OR Motor) AND NOT Stop;
END_PROGRAM

A project can contain several programs. Without a configuration, each program runs once per scan. A configuration can run programs in tasks at different rates, and can create several instances of the same program. Programs cannot be called from other POUs.

Programs can also declare VAR_INPUT and VAR_OUTPUT variables, which are connected to global variables by the configuration.

Functions

A function is declared between FUNCTION and END_FUNCTION. The type after the name is the type of the result. The result is assigned to the name of the function, and must be assigned in the function.

FUNCTION Average : LREAL
VAR_INPUT
    A : LREAL;
    B : LREAL;
END_VAR

Average := (A + B) / 2.0;
END_FUNCTION

A function can be called with named arguments, or with positional arguments in the order the inputs are declared:

Middle := Average(A := Left, B := Right);
Middle := Average(Left, Right);

Inputs can have default values, which are used when an argument is omitted. A function can also have outputs, which are assigned to variables of the caller with the => arrow operator:

FUNCTION Split : BOOL
VAR_INPUT
    Value : LREAL;
    Scale : LREAL := 1.0;
END_VAR
VAR_OUTPUT
    Whole : DINT;
    Fraction : LREAL;
END_VAR

Whole := TRUNC(Value * Scale);
Fraction := Value * Scale - Whole;
Split := Fraction > 0.0;
END_FUNCTION

HasFraction := Split(Value := 2.75, Whole => Count, Fraction => Remainder);

A function without a result type is called as a statement:

FUNCTION Report
VAR_INPUT
    Message : STRING;
END_VAR

WRITE(Message);
END_FUNCTION

Report('Cycle complete');

The local variables of a function are reinitialized with each call, so a function always returns the same result for the same inputs. Functions cannot declare located variables, and cannot have the same name as a standard function.

Function Blocks

A function block is declared between FUNCTION_BLOCK and END_FUNCTION_BLOCK. A function block is a type, so you declare instances of it as variables. Each instance has its own copy of the function block’s variables, which keep their values between calls. Function blocks are ideal for reusable machine components, such as a cylinder, a motor starter or a station.

FUNCTION_BLOCK Cylinder
VAR_INPUT
    Extend : BOOL;
    Sensor : BOOL;
    Timeout : TIME := T#2s;
END_VAR
VAR_OUTPUT
    Valve : BOOL;
    Extended : BOOL;
    Fault : BOOL;
END_VAR
VAR
    Monitor : TON;
END_VAR

// Reports a fault if the cylinder does not extend in time.
Monitor(IN := Extend AND NOT Sensor, PT := Timeout);
Fault := Monitor.Q;
Valve := Extend AND NOT Fault;
Extended := Extend AND Sensor;
END_FUNCTION_BLOCK

The function block is used by declaring an instance and calling it once per scan. The inputs are assigned in the call, and the outputs are read from the instance with the . operator. The program connects each instance to its own located variables, which are bound to the valve and sensor of that cylinder in the model.

PROGRAM Main
VAR
    Clamp : Cylinder;
    ClampButton AT %IX0.0 : BOOL;
    ClampSensor AT %IX0.1 : BOOL;
    ClampValve AT %QX0.0 : BOOL;
    FaultLamp AT %QX0.1 : BOOL;
END_VAR

Clamp(Extend := ClampButton, Sensor := ClampSensor, Timeout := T#1s);
ClampValve := Clamp.Valve;
FaultLamp := Clamp.Fault;
END_PROGRAM

Inputs that are not assigned in a call keep the value from the previous call. Inputs can also be assigned outside of a call, for example Clamp.Extend := TRUE, and are then used by the next call. Outputs and local variables can only be assigned inside the function block.

A located variable declared inside a function block occupies the same address in every instance, so instances would overwrite each other’s outputs. Connect the inputs and outputs of each instance in the program instead, as shown above, or use partly specified addresses that a configuration assigns for each instance.

In-Out Variables

A VAR_IN_OUT variable refers to a variable of the caller, which the function block reads and writes directly. This avoids copying large structures or arrays, and allows a function block to modify a variable that it does not own.

FUNCTION_BLOCK Accumulate
VAR_INPUT
    Amount : DINT;
END_VAR
VAR_IN_OUT
    Total : DINT;
END_VAR

Total := Total + Amount;
END_FUNCTION_BLOCK

PROGRAM Main
VAR
    Produced : DINT; // The total, which belongs to the program.
    Tally : Accumulate;
END_VAR

Tally(Amount := 5, Total := Produced); // Adds 5 to Produced in each scan.
END_PROGRAM

Instances

Function block instances can be declared inside other function blocks, in arrays, and as members of structures. A function block can contain instances of the standard function blocks, such as timers and counters, and instances of other user-defined function blocks.

VAR
    Stations : ARRAY[1..4] OF Cylinder;
    I : INT;
END_VAR

FOR I := 1 TO 4 DO
    Stations[I](Extend := Requests[I], Sensor := Sensors[I]);
    Valves[I] := Stations[I].Valve;
END_FOR;

A function block instance should usually be called exactly once per scan. Timers, edge detection and the motion control function blocks rely on being called every scan to observe their inputs.

Choosing a POU

  • Use a function for a calculation that depends only on its inputs, such as a unit conversion or a scaling formula.
  • Use a function block for anything that must remember its state between scans, such as a sequence, a timer, or a model of a machine component.
  • Use a program for the top-level logic of a machine or a station, which calls the function blocks.