Attribute 'call_after_init'
The pragma causes a method to be called implicitly after the initialization of a function block instance. Strictly speaking, the method is called after the initial assignments have been processed and before the tasks of a PLC project have been started. It can therefore respond appropriately to the user's specifications. The name of the method is freely selectable (exceptions: FB_init, FB_reinit and FB_exit).
For performance reasons, you must add the attribute to both the function block and the method in a dedicated first line above the declaration part.
Syntax: {attribute 'call_after_init'}
Insertion location: First line above the declaration part of the method and function block
TwinCAT calls the method after the method FB_init, and after the variable values of an initialization expression have become valid in the instance declaration.
call_after_init for derived function blocks
A function block that extends another function block, which uses the attribute 'call_after_init', must also be assigned the attribute.
For reasons of clarity, it is recommended that you overwrite the corresponding method with the same name, signature, and attribute. This requires a call of SUPER^.MyInit
.
Methods that contain the attribute 'call_after_init' must not have any inputs (VAR_INPUT). TwinCAT issues a corresponding compile error. |
Debugging Finding errors in methods that are declared with {attribute' call_after_init'} is cumbersome because setting breakpoints cannot have the desired effect, for example. |
Example:
Definition:
{attribute 'call_after_init'}
FUNCTION_BLOCK FB_Sample
... <functionblock definition>
{attribute 'call_after_init'}
METHOD CallAfterInit
... <method definition>
The definition implements the following declaration in the following code processing, for example:
fbSample : FB_Sample := (nValue1 := 99);
Code processing:
fbSample.FB_Init();
fbSample.nValue1 := 99;
fbSample.CallAfterInit();
In this way, CallAfterInit() is able to respond to the user-defined initialization.
See also: