Object Oriented Programming (OOP)

IEC 61131-3 (Edition 3 and later) extends Structured Text with object-oriented programming. Methods, properties, interfaces and inheritance allow you to build libraries of reusable machine components, where similar components share a common interface. Namespaces allow you to organize large projects and avoid name conflicts.

Object-oriented features are optional. Many PLC programs only use programs, functions and function blocks, which are described on the program organization units page.

Contents

Methods

A method is a function that belongs to a function block or class, and has access to the variables of its instance. Methods are declared inside the function block, before its body, between METHOD and END_METHOD. Like a function, a method can have a result type, inputs, outputs and local variables. The local variables of a method are reinitialized with each call.

FUNCTION_BLOCK Motor
VAR_INPUT
    Run : BOOL;
END_VAR
VAR_OUTPUT
    Running : BOOL;
    Starts : DINT;
END_VAR

METHOD PUBLIC ResetStarts
    Starts := 0;
END_METHOD

METHOD PUBLIC IsIdle : BOOL
    IsIdle := NOT Running;
END_METHOD

// The body of the function block, which runs when the instance is called.
IF Run AND NOT Running THEN
    Starts := Starts + 1;
END_IF;
Running := Run;
END_FUNCTION_BLOCK

Methods are called through an instance with the . operator:

Pump(Run := StartButton);
IF ResetButton AND Pump.IsIdle() THEN
    Pump.ResetStarts();
END_IF;

Inside a method, THIS refers to the current instance, for example THIS.Starts. Another method of the same function block or class can be called with or without THIS, so IsIdle() is the same as THIS.IsIdle(). A method takes precedence over a function with the same name.

Access Specifiers

An access specifier controls where a method, property or variable section can be used:

  • PUBLIC: Accessible from anywhere.
  • PROTECTED: Accessible from the declaring POU and the POUs that extend it. This is the default for methods.
  • PRIVATE: Accessible only from the declaring POU.
  • INTERNAL: Accessible only from within the same namespace.

The inputs and outputs of a function block are always public. The VAR variables of a function block can only be accessed from inside the function block. A class can make its variables accessible with VAR PUBLIC.

Properties

A property looks like a variable from the outside, but is implemented with a getter and a setter. The PROPERTY_GET returns the value, and PROPERTY_SET receives the value that is assigned. A property with only a getter is read-only.

FUNCTION_BLOCK Tank
VAR
    LevelLiters : LREAL;
    CapacityLiters : LREAL := 1000.0;
END_VAR

PROPERTY_GET PUBLIC FillRatio : LREAL
    FillRatio := LevelLiters / CapacityLiters;
END_PROPERTY

PROPERTY_GET PUBLIC Capacity : LREAL
    Capacity := CapacityLiters;
END_PROPERTY

PROPERTY_SET PUBLIC Capacity : LREAL
    CapacityLiters := MAX(Capacity, 1.0);
END_PROPERTY
END_FUNCTION_BLOCK

MainTank.Capacity := 500.0;
Full := MainTank.FillRatio >= 0.95;

Classes

A class is declared between CLASS and END_CLASS. A class is similar to a function block, but it has no body, inputs or outputs. All of its behavior is provided by its methods and properties.

CLASS Statistics
VAR
    Count : DINT;
    Sum : LREAL;
END_VAR

METHOD PUBLIC Add
VAR_INPUT
    Value : LREAL;
END_VAR
    Count := Count + 1;
    Sum := Sum + Value;
END_METHOD

METHOD PUBLIC Mean : LREAL
    IF Count > 0 THEN
        Mean := Sum / Count;
    ELSE
        Mean := 0.0;
    END_IF;
END_METHOD
END_CLASS

PROGRAM Main
VAR
    CycleTimes : Statistics;
END_VAR

CycleTimes.Add(Value := 2.5);
WRITE(CycleTimes.Mean());
END_PROGRAM

Inheritance

A function block or class can extend another with EXTENDS. The derived type inherits the variables, methods and properties of its base type, and can add its own. A method of the base type is replaced with OVERRIDE, and the original method can be called through SUPER.

FUNCTION_BLOCK Conveyor
VAR_OUTPUT
    Speed : LREAL;
END_VAR

METHOD PUBLIC Start
    Speed := 0.5;
END_METHOD
END_FUNCTION_BLOCK

FUNCTION_BLOCK FastConveyor EXTENDS Conveyor
METHOD PUBLIC OVERRIDE Start
    SUPER.Start();
    Speed := Speed * 2.0;
END_METHOD
END_FUNCTION_BLOCK
  • ABSTRACT: An abstract function block or class cannot be instantiated, and is only used as a base type. An abstract method has no implementation, and must be overridden by every type that extends it.
  • FINAL: A final function block or class cannot be extended, and a final method cannot be overridden.
FUNCTION_BLOCK ABSTRACT Station
METHOD PUBLIC ABSTRACT Process : BOOL
END_METHOD
END_FUNCTION_BLOCK

FUNCTION_BLOCK DrillStation EXTENDS Station
VAR
    Holes : INT;
END_VAR

METHOD PUBLIC OVERRIDE Process : BOOL
    Holes := Holes + 1;
    Process := TRUE;
END_METHOD
END_FUNCTION_BLOCK

An instance of DrillStation can be declared, but declaring an instance of Station is a compile error, since Station is abstract.

Interfaces

An interface declares a set of methods and properties, without implementing them. A function block or class implements an interface with IMPLEMENTS, and must then implement all of its methods. A type can implement several interfaces, separated by commas, and an interface can extend other interfaces.

INTERFACE IStation
METHOD Process : BOOL
END_METHOD
END_INTERFACE

FUNCTION_BLOCK Drill IMPLEMENTS IStation
METHOD PUBLIC Process : BOOL
    Process := TRUE;
END_METHOD
END_FUNCTION_BLOCK

FUNCTION_BLOCK Press IMPLEMENTS IStation
METHOD PUBLIC Process : BOOL
    Process := FALSE;
END_METHOD
END_FUNCTION_BLOCK

A variable with an interface type refers to any instance that implements the interface. Calls through the variable are dispatched to the instance that it refers to, which allows one piece of code to handle many different types of station.

VAR
    DrillStation : Drill;
    PressStation : Press;
    Stations : ARRAY[1..2] OF IStation;
    I : INT;
END_VAR

Stations[1] := DrillStation;
Stations[2] := PressStation;
FOR I := 1 TO 2 DO
    Stations[I].Process();
END_FOR;

An interface variable is NULL until an instance is assigned to it. Calling a method through a NULL interface variable is a runtime error. The assignment attempt operator ?= converts an interface variable to a more specific interface or to a reference, and assigns NULL if the instance does not have that type.

VAR
    Station : IStation;
    DrillRef : REF_TO Drill;
END_VAR

DrillRef ?= Station;
IF DrillRef <> NULL THEN
    // The station is a drill.
END_IF;

Namespaces

A namespace groups related types and POUs under a common name. Names inside a namespace are accessed with the . operator, or imported with USING. Namespaces can be nested, either by declaring a namespace inside another, or with a dotted name.

NAMESPACE Plant.Conveyors
    FUNCTION_BLOCK Belt
    VAR_INPUT
        Run : BOOL;
    END_VAR
    END_FUNCTION_BLOCK
END_NAMESPACE

PROGRAM Main
VAR
    Infeed : Plant.Conveyors.Belt;
END_VAR

Infeed(Run := TRUE);
END_PROGRAM

A USING directive at the start of a file or POU allows the names of a namespace to be used without qualification:

USING Plant.Conveyors;

PROGRAM Main
VAR
    Infeed : Belt;
END_VAR
END_PROGRAM

Declarations marked INTERNAL can only be used from within their namespace.