Declaration of an interface incl. method prototypes

Syntax
INTERFACE name_1 ( * optional_begin *) EXTENDS interface_2, interface_3, ... interface_n (* optional_end *)
 
(* optional: declarations of method prototypes *)
 
END_INTERFACE

Meaning

declaration of an →interface (a feature of the →object-oriented programming) , name_1 must be an →IEC-identifier.
The declaration is possible within an ST-object – the declaration is done within the global →namespace or within a declared namespace. INTERFACE and END_INTERFACE are →keywords for the declaration of the interface.

images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/information.svg In the object oriented programming, the interface separates the interface specification from its implementation as a →class. This allows different implementations of a common interface specification.

The keyword EXTENDS is optional. Specify EXTENDS, if you want to derive the interface from other interfaces (also known as base interfaces). Specify the name of the base interfaces after EXTENDS (separated by a comma).
The name of the method prototypes within the interface and the base interfaces must be unique. However, multiply existing m
ethod prototypes from the same interface are ignored (see method prototype UP in the following example 2).

Good to know

images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/lightbulb.svg You are able to declare only →method prototypes within an interface. See the following section "Declaration of a method prototype".

images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/lightbulb.svg An interface may be used as the type of a variable (see "Declaration of variables based on an interface"). Moreover, a function block can implement the interfaces (see " Declaration of a function block in ST ").

images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/lightbulb.svg An interface behaves as a →function block does.

Declaration of a method prototype

A method prototype is a restricted declaration of a →method (with the implicit modifier PUBLIC ) for the use with an interface.

Overview for the syntax
METHOD name_1 (* optional_begin *) : type (* optional_end *)
 
(* optional: declaration of variables but only input variables, output variables and in-out variables are allowed in method prototypes *)
 
END_METHOD

In contrast to a method (see "Declaration of a method" for details), a method prototype may contain these elements only:

Observe that no keywords (e.g. modifiers) and no body (hence, a code to execute) are defined in a method prototype as this is possible for a method.

Example 1: Interface with 2 method prototypes
INTERFACE COUNT
METHOD UP : UINT
VAR_INPUT
INC: UINT;
END_VAR
VAR_OUTPUT
QU: BOOL;
END_VAR
END_METHOD
 
METHOD UP5 : UINT
VAR_OUTPUT
QU: BOOL;
END_VAR
END_METHOD
END_INTERFACE
Example 2: As example 1 with derived interfaces
INTERFACE COUNT_Original
(* "COUNT_Original" contains the method prototypes "UP" und "UP5". *)
 
METHOD UP : UINT
VAR_INPUT
INC: UINT;
END_VAR
VAR_OUTPUT
QU: BOOL;
END_VAR
END_METHOD
METHOD UP5 : UINT
VAR_OUTPUT
QU: BOOL;
END_VAR
END_METHOD
END_INTERFACE
 
INTERFACE COUNT_BASE
(* "COUNT_BASE" contains the method prototype "UP". *)
 
METHOD UP : UINT
VAR_INPUT
INC: UINT;
END_VAR
VAR_OUTPUT
QU: BOOL;
END_VAR
END_METHOD
END_INTERFACE
 
INTERFACE COUNT EXTENDS COUNT_BASE
(* "COUNT" contains the method prototype "UP5". Due to "EXTENDS COUNT_BASE", there is also the method prototype "UP". *)
 
METHOD UP5 : UINT
VAR_OUTPUT
QU: BOOL;
END_VAR
END_METHOD
END_INTERFACE
 
INTERFACE COUNT3 EXTENDS COUNT_BASE, COUNT_Original
(* "COUNT3" contains the method prototype "UP10". Due to "EXTENDS COUNT_BASE, COUNT_Original", there are also the method prototypes "UP5" und "UP". *)
(* Here an error is reported. Reason: "UP" exists multiple times in the different interfaces "COUNT_BASE" und "COUNT_Original". *)
METHOD UP10 : UINT
END_METHOD
END_INTERFACE
 
INTERFACE Sample EXTENDS COUNT_BASE, COUNT_BASE
(* Due to "EXTENDS COUNT_BASE, COUNT_Original", there is also the method prototype "UP". *)
(* This is OK although "UP" actually exists multiple times. The 2nd occurrence of "UP" in "COUNT_BASE" is ignored because "UP" is derived from the same interface "COUNT_BASE". *)
END_INTERFACE
 
INTERFACE COUNT2 EXTENDS COUNT_BASE, COUNT
(* "COUNT2" contains the method prototype "UP10". Due to "EXTENDS COUNT_BASE, COUN", there are also the method prototypes "UP5" und "UP". *)
(* This is OK although "UP" actually exists multiple times. The 2nd occurrence of "UP" in "COUNT" is ignored because "UP" is derived from the same interface "COUNT_BASE". Background: The interface "COUNT" is derived from the interface "COUNT_BASE" as well. *)
METHOD UP10 : UINT
END_METHOD
END_INTERFACE
Example 3
INTERFACE I_Motor
/*
* Returns the current speed of the motor in RPM.
*/
METHOD GetCurrentSpeed : UINT
END_METHOD
/*
* Sets the desired speed of the motor.
* The desired ramp-up time can be specified by an input parameter.
* Returns the current motor speed as an output parameter.
*/
METHOD SetSpeed
VAR_INPUT
i_RampUpTime : TIME := T#1s;
i_DesiredSpeed : UINT;
END_VAR
VAR_OUTPUT
o_CurrentSpeed : UINT;
END_VAR
END_METHOD
END_INTERFACE

More examples for interfaces are specified under " Examples on using interfaces and variables based on these interfaces (incl. assignments) ".