Skip to content

Methods, classes and interfaces

logiccloud supports the object-oriented extensions of IEC 61131-3: methods, classes, interfaces, inheritance and access control. These build on function blocks.

A METHOD is a callable, parameterized routine that belongs to a function block or class. It has its own variable sections and, optionally, a return type.

FUNCTION_BLOCK Counter
VAR
value : DINT;
END_VAR
METHOD Increment
VAR_INPUT
by : DINT;
END_VAR
value := value + by;
END_METHOD
METHOD Get : DINT
Get := value;
END_METHOD
END_FUNCTION_BLOCK

Call a method on an instance with dot notation:

VAR
c : Counter;
n : DINT;
END_VAR
c.Increment(by := 5);
n := c.Get();

A method may declare VAR_INPUT, VAR_OUTPUT, VAR_IN_OUT, VAR, VAR_TEMP and VAR_EXTERNAL sections, just like a function.

A CLASS is a type that bundles data and methods but — unlike a function block — is not directly assignable to a task. Declare it with CLASS … END_CLASS.

CLASS Vector2D
VAR
x : REAL;
y : REAL;
END_VAR
METHOD Length : REAL
Length := SQRT(x * x + y * y);
END_METHOD
END_CLASS

Classes hold VAR and VAR_EXTERNAL sections plus methods.

An INTERFACE declares method prototypes (signatures only, no bodies) that implementing types must provide.

INTERFACE IDrivable
METHOD Start
END_METHOD
METHOD Stop
END_METHOD
METHOD SetSpeed
VAR_INPUT
rpm : REAL;
END_VAR
END_METHOD
END_INTERFACE

An interface may itself extend one or more other interfaces with EXTENDS.

  • A function block can EXTENDS another function block (or a class) to inherit its variables and methods.
  • A class can EXTENDS another class.
  • A function block or class can IMPLEMENTS one or more interfaces, and must then provide every method those interfaces declare.
FUNCTION_BLOCK Motor IMPLEMENTS IDrivable
VAR
speed : REAL;
END_VAR
METHOD Start ... END_METHOD
METHOD Stop ... END_METHOD
METHOD SetSpeed
VAR_INPUT
rpm : REAL;
END_VAR
speed := rpm;
END_METHOD
END_FUNCTION_BLOCK
FUNCTION_BLOCK ServoMotor EXTENDS Motor
// inherits speed, Start, Stop, SetSpeed
END_FUNCTION_BLOCK
ModifierWhereMeaning
ABSTRACTCLASS, FUNCTION_BLOCK, METHODCannot be instantiated / has no body; must be overridden by a subtype.
FINALCLASS, FUNCTION_BLOCK, METHODCannot be extended / overridden.
OVERRIDEMETHODReplaces an inherited method of the same signature.
FUNCTION_BLOCK ABSTRACT Shape
METHOD ABSTRACT Area : REAL
END_METHOD
END_FUNCTION_BLOCK
FUNCTION_BLOCK Circle EXTENDS Shape
VAR
radius : REAL;
END_VAR
METHOD OVERRIDE Area : REAL
Area := 3.14159 * radius * radius;
END_METHOD
END_FUNCTION_BLOCK

Methods, VAR sections of a class/function block, and class/function-block/ function declarations themselves may carry an access specifier controlling visibility:

SpecifierVisibility
PUBLICAccessible from anywhere.
PROTECTEDAccessible from the declaring type and its subtypes.
PRIVATEAccessible only from the declaring type.
INTERNALAccessible within the same namespace.
FUNCTION_BLOCK Account
VAR PRIVATE
balance : REAL;
END_VAR
METHOD PUBLIC Deposit
VAR_INPUT
amount : REAL;
END_VAR
balance := balance + amount;
END_METHOD
END_FUNCTION_BLOCK
  • THIS refers to the current instance — use it to disambiguate a member from a local variable of the same name, or to pass the instance itself.
  • SUPER refers to the parent type, allowing a subtype to call the overridden implementation it inherited.
FUNCTION_BLOCK ServoMotor EXTENDS Motor
METHOD OVERRIDE SetSpeed
VAR_INPUT
rpm : REAL;
END_VAR
SUPER.SetSpeed(rpm := rpm); // run the base behaviour first
THIS.Calibrate(); // then this instance's extra step
END_METHOD
METHOD Calibrate ... END_METHOD
END_FUNCTION_BLOCK

Use SUPER^ and THIS^ to access them as references where a reference is required.