Object Interface
Symbol:
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 | : Extends the interface that is entered in the input field or selected via the input assistant . 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
- In an interface (e.g. I_Sample) you declare the methods and properties (including return type, inputs etc.), which are to be associated with this interface.
- In the function blocks, which are to correspond to this interface and should therefore make the corresponding methods and properties available, you implement the interface I_Sample.
- The function block contains the interface in the IMPLEMENTS list within its declaration part (e.g. FB_Sample IMPLEMENTS I_Sample).
- A function block can implement one or several interfaces (e.g. FB_Sample IMPLEMENTS I_Sample1, I_Sample2).
- A function block, which implements an interface, must contain all methods and properties that are defined in this interface (interface methods and properties). The declaration of the methods and properties must match the declaration in the interface exactly (name, return type, inputs, outputs).
- The function blocks add function block-specific code to the interface methods and interface properties. If an interface is implemented by several function blocks, you can use the same method with the parameters but different implementation code in different function blocks.
- For function blocks, which implement one or several interfaces, the compiler checks whether the function blocks meet the respective interface declarations. If the element declarations in the interface and in the function block differ, or if the interface contains further elements, which are not included in the function block, the compiler reports an error.
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.
- First, instantiate the interface (e.g.
iSample : I_Sample;
) and the function block(s), which implement(s) the interface correctly (see use case 1: Checking the interface declaration via the compiler). - You can then assign the interface variable to each instance of a function block, which implements the interface correctly. If an interface variable has not yet been assigned, the variable contains the value 0 in online mode.
- In the last step, you can call an interface method or property via the interface variable. The method or property is called for the function block, to which the interface refers.
- Such an implementation enables the use of different, but similar function blocks via the interface variable in a consistent manner. Depending on the project state you can assign a particular function block instance to the interface variable, for example, so that the call of the interface methods and properties is identical, although a different function block instance is used, depending on the project state.
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
- You cannot declare variables within an interface. An interface has no implementation part and no actions. Only a collection of methods and properties is defined, which contain declaration code, but no implementation code.
- An interface type variable is a reference to instances of function blocks. TwinCAT always treats variables, which are defined as an interface type, as references.
Interface references and Online Change
- Interface references are automatically redirected from TwinCAT 3.0 build 3100 , so that the correct interface is always referenced, even in the event of an online change. This requires additional code and time, which may cause issues, depending on the number of affected objects. Before the online change is performed, programmers are therefore shown the number of affected variables and interface references, so that they can decide whether to go ahead with the online change or abort it.
Checking interface variables
- The operator __ISVALIDREF can only be used for operands of type REFERENCE TO. This operator cannot be used for checking interface variables. To check whether an interface variable was already assigned a function block instance, you can check the interface variable for not equal to 0 (IF iSample <> 0 THEN …).
Extended monitoring of an interface variable
- Advanced options for monitoring/debugging an interface variable are available from TwinCAT 3.1 Build 4024. An interface variable in the monitoring area (declaration editor, watch list) can be expanded. The symbol path and online data of the currently assigned FB instance are then displayed below the interface variable.
Example 1
Interface declaration:
- You have added the interface I_Sample to your project. Add the method GetType with the return type STRING to the interface.
- I_Sample and GetType contain no implementation code. The method GetType contains only the required (variable) declarations (e.g. return type). You can program out the method GetType later on in the function block that implements the interface I_Sample.
INTERFACE I_Sample
Method I_Sample.GetType:
METHOD GetType : STRING
Interface implementation:
- If you subsequently add a function block to the project and enter the interface I_Sample in the field Implements in the dialog Add, TwinCAT also automatically adds the method GetType to this function block. Here you can implement function block-specific code in the methods.
- The function blocks FB_A and FB_B both implement the interface I_Sample:
FUNCTION_BLOCK FB_A IMPLEMENTS I_Sample
FUNCTION_BLOCK FB_B IMPLEMENTS I_Sample
- Both function blocks therefore have to include a method with the name GetType and the return type STRING. Otherwise the compiler reports an error (see use case 1 in section Interface applications).
Method FB_A.GetType:
METHOD GetType : STRING
GetType := 'FB_A';
Method FB_B.GetType:
METHOD GetType : STRING
GetType := 'FB_B';
Interface application:
- A function F_DeliverType contains the declaration of an input variable of the type of interface I_Sample. Within the function, the interface method GetType is called via the interface variable iSample. In this case, whether FB_A.GetType or FB_B.GetType is called depends on the transferred function block type (see application 2 in section Interface applications).
FUNCTION F_DeliverType : STRING
VAR_INPUT
iSample : I_Sample;
END_VAR
F_DeliverType := iSample.GetType();
- Instances of function blocks, which implement the interface I_Sample (e.g. FB_A and FB_B), can be assigned to the input variable of the function F_DeliverType.
- Examples of function calls:
- If the function block instance fbA is transferred to the function F_DeliverType, the method fbA.GetType will be called inside the function since the interface variable iSample points to the function block instance fbA. This method call delivers the return value 'FB_A', which in turn is returned by the function F_DeliverType and assigned in the main program to the variable sResultA.
- Accordingly, sResultB receives the value 'FB_B', since the method fbB.GetType is called inside the function F_DeliverType.
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
Interface declaration:
- You have added the interface I_Name to your project. Add the property Name with the return type STRING to the interface. The property has the accessor methods Get and Set. The Get accessor can be used to read the name of any object from a function block that implements the interface. The Set accessor is used to write the name into this function block.
- I_Name and Name contain no implementation code. The property Name only contains the required declaration (return type). Therefore you cannot process the Get and Set methods inside the interface definition, but you can do this later in the function block that implements the interface I_Name.
INTERFACE I_Name
Property I_Name.Name:
PROPERTY Name : STRING
Interface implementation:
- The function blocks FB_SampleA and FB_SampleB implement the interface I_Name.
- If the interface is specified, for example, when creating the function blocks in the dialog Add, TwinCAT automatically adds the property Name with the Get and Set methods under the function blocks FB_SampleA and FB_SampleB.
- You can edit the accessor methods underneath the function blocks, for example so that the variable sVar1 is read and you thus obtain the name of an object. In FB_SampleB, which implements the same interface I_Name, you can implement the Get method code, which then returns the name of another object. The Set method can be used to write the name, which the MAIN program supplies ('abc'), into the function block FB_SampleB.
- The function blocks FB_SampleA and FB_SampleB each implement the interface I_Name:
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
- Both function blocks must therefore contain a property with the name Name and the return type STRING. The property must have a Get and a Set method. Otherwise the compiler reports an error (see use case 1 in section Interface applications).
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:
- The properties of the function blocks can be accessed both via the corresponding function block instances and via an interface variable of the type I_Name. The prerequisite for access via an interface variable is that this variable has been assigned a specific function block instance beforehand that implements the interface I_Name.
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: