A method is a language element similar to a function that can be used in a context of a function block. It can be regarded as a function which contains an instance of the respective function block. Such as a function, a method has a return value, and its own declaration part for temporary variables and parameters.
Also as a means of object-oriented programming, you can use interfaces to organize the methods available in a project.
To assign a method to a function block or interface, select the corresponding function block or interface node in the
, click the green plus button and execute the command . Alternatively, you can right-click the function block or interface node and execute the command from the contextual menu.In the
dialog box, enter a , the desired , the , and the (see below). If the method does not have an implementation and the implementation is provided by the derived function block, select the option . For choosing the return data type, click the button to open the dialog box.: For compatibility reasons, access specifiers are optional. The specifier is available as an equivalent for having set no specifier.
Alternatively, choose one of the options from the selection list:
: The access on the method is restricted to the function block.
: The access on the method is restricted to the function block and its derivation.
: The access on the method is restricted to the present namespace (the library).
Click
to confirm. The method editor view opens.EcoStruxure Machine Expert facilitates object-oriented programming using inheritance within function blocks: When you execute on a function block that inherits from another function block, the , , , and elements used in the base function block are listed for selection:
, , , and elements with = , , and defined in the base function block are available for selection. You can adapt the definition for the inherited object. In the inherited object, the same is assigned as to the source elements.
, , , and elements with = are not available for selection because access is restricted to the base function block.
Syntax:
METHOD <access specifier> <method name> : <return data type>VAR_INPUT ... END_VAR
For a description on how to declare interface handling methods, refer to the Interface chapter.
Method calls are also named virtual function calls. For further information, refer to the chapter Method Invocation.
Note the following for calling a method:
The data of a method is temporary and only valid during the execution of the method (stack variables). Therefore, the variables and function blocks declared in a method are reinitialized at each call of the method.
Methods defined in an interface are only allowed to have input, output, and input/output variables, but no body (implementation part).
Methods such as functions can have additional outputs. They must be assigned during method invocation.
The declared access specifier defines how the method can be called:
: The method can be called within its own namespace.
: The method can be called within its own POU and its derivatives.
: The method can be called within its own POU.
: No restrictions apply for calling the method.
Note the following for implementing a method:
In the body of a method, access to the function block instance variables is allowed.
If necessary, use the THIS pointer which always points on the present instance.
VAR_TEMP
variables of the function block cannot be accessed in a method.
A method can call itself recursively.
Use the following syntax for calling a method:
<return value variable> := <POU name> . <method name> ( <method input name> := <variable name> (, <further method input name> := <variable name> )* );
Declaration example:
METHOD PUBLIC DoIt : BOOL
VAR_INPUT
iInput_1 : DWORD;
iInput_2 : DWORD;
sInput_3 : STRING(12);
END_VAR
Call example:
bFinishedMethod := fbInstance.DoIt(sInput_3 :='Hello World ', iInput_2 := 16#FFFF,iInput_1 := 16);
Declaration example:
METHOD PUBLIC DoIt : BOOL
VAR_INPUT
iInput_1 : DWORD;
iInput_2 : DWORD;
sInput_3 : STRING(12);
END_VAR
Call example:
bFinishedMethod := fbInstance.DoIt( 16, 16#FFFF,'Hello World ');
Within the implementation, a method can call itself, either directly by using the THIS
pointer, or by using a local variable for the assigned function block.
THIS^. <method name> ( <parameter transfer of all input and output variables>)
Direct call of the relevant function block instance with the THIS
pointer.
VAR fb_Temp : <function block name>; END_VAR
Call by using a local variable of the method that temporarily instantiates the relevant function block.
A recursive call results in a compiler message. The compiler message will not be issued if the method is provided with the pragma {attribute 'estimated-stackusage : '<estimated_stack_size_in_bytes>'}
. Refer to the Attribute estimated-stack-usage
chapter for an implementation example.
Thus, specifying the method name is insufficient for recursive method calls. In this case, the following compiler message is issued:
Program name, function or function block instance expected instead of
NOTICE | |
---|---|
Method |
Description |
---|---|
|
A method named |
|
If a method named |
|
If an exit method named |
Properties and interface properties each consist of a Set
and/or a Get
accessor method.
In the device description file, it can be defined that a certain method should always be called task-cyclically by a certain function block instance (of a library module). If this method has the following input parameters, it is processed also when the active application is not running.
Example
VAR_INPUT
pTaskInfo : POINTER TO DWORD;
pApplicationInfo: POINTER TO _IMPLICIT_APPLICATION_INFO;
END_VAR
The programmer can check the application status via pApplicationInfo
, and can define what should happen.
IF pApplicationInfo^.state = RUNNING THEN <instructions> END_IF