Object Interface

Symbol: Object Interface 1:

Keyword: INTERFACE

An interface is a tool for object-oriented programming. The object Interface describes a set of method and property prototypes. Prototype in this context means that the methods and properties only contain declarations, but no implementation.

In this way you can use various function blocks with common properties in the same way.

You can add the objects Interface property and Interface method to the Interface object.

Creating an object Interface

1. Select a folder in the Solution Explorer in the PLC project tree.
2. In the context menu select the command Add > Interface...
The Add Interface dialog opens.
3. Enter a name, and optionally select an interface to be extended.
4. Click on Open.
The interface is added to the PLC project tree and opens in the editor.

Dialog Add Interface

Name

Interface name

Inheritance

Advanced

Object Interface 2:: Extends the interface that is entered in the input field or selected via the input assistant Object Interface 3:. This means that all interface methods, which the new interface extends, are also available in the new interface.

Interface applications

1. Checking the interface declaration via the compiler

 

2. Calling methods and properties of a function block instance via an interface variable

In addition to automatic verification of the interface declaration via the compiler, you can use interfaces to call an interface method or interface property of a function block instance via an interface variable.

Object Interface 4:

An interface type variable has to be assigned the instance of a function block, before a method or property can be called via the interface variable.

Notes

Interface references and Online Change

Checking interface variables

Extended monitoring of an interface variable

Example 1

Object Interface 5:

Interface declaration:

INTERFACE I_Sample

Method I_Sample.GetType:

METHOD GetType : STRING

 

Interface implementation:

FUNCTION_BLOCK FB_A IMPLEMENTS I_Sample
FUNCTION_BLOCK FB_B IMPLEMENTS I_Sample

Method FB_A.GetType:

METHOD GetType : STRING
GetType := 'FB_A';

Method FB_B.GetType:

METHOD GetType : STRING
GetType := 'FB_B';

 

Interface application:

FUNCTION F_DeliverType : STRING
VAR_INPUT
    iSample : I_Sample;
END_VAR

F_DeliverType := iSample.GetType();
PROGRAM MAIN
VAR
    fbA       : FB_A;
    fbB       : FB_B;
    sResultA  : STRING;
    sResultB  : STRING;
END_VAR

sResultA := F_DeliverType(iSample := fbA); // call with instance of type FB_A
sResultB := F_DeliverType(iSample := fbB); // call with instance of type FB_B

Example 2

Object Interface 6:

Interface declaration:

INTERFACE I_Name

Property I_Name.Name:

PROPERTY Name : STRING

 

Interface implementation:

FUNCTION_BLOCK FB_SampleA IMPLEMENTS I_Name
VAR
    sVar1 : STRING := 'My name is A.';
END_VAR

FUNCTION_BLOCK FB_SampleB IMPLEMENTS I_Name
VAR
    sVar2 : STRING := 'My name is B.';
END_VAR

FB_SampleA.Name.Get:

Name := sVar1;

FB_SampleA.Name.Set:

sVar1 := Name;

FB_SampleB.Name.Get:

Name := sVar2;

FB_SampleB.Name.Set:

sVar2 := Name;

 

Interface application:

PROGRAM MAIN
VAR
    iName     : I_Name;

    fbSampleA : FB_SampleA;
    sNameA    : STRING;       // will be 'My name is A.'

    fbSampleB : FB_SampleB;
    sNameB    : STRING;       // will be 'My name is B.' after first cycle
                              // and will be 'New name' afterwards
END_VAR

// assign FB instance fbSample1 to interface variable
iName := fbSampleA;
 
// access to name property of fbSample1 via interface variable (Get)
sNameA := iName.Name;
 
// access to name property of fbSample2 via FB instance (Get and Set)
sNameB := fbSampleB.Name;
fbSampleB.Name := 'New name';

See also: