PLC

Methods FB_init, FB_reinit and FB_exit

You can use the methods explicitly to influence the initialization of function block variables and the behavior when function blocks are terminated.

FB_init
FB_reinit
FB_exit

See also the information on the behavior of these methods for derived function blocks.

 

Return value

By default, the return value of the methods FB_init, FB_reinit, and FB_exit is not used, and FALSE is returned. If these methods are called implicitly, the return value is not evaluated.

If you want to call the methods explicitly, you can return a meaningful return value and evaluate it when you explicitly call the methods. Keep in mind that, even if you adjust the return value, it is not evaluated in an implicit call.

 
 

Operating cases

Below you will find information on various operating cases for the methods FB_init, FB_reinit and FB_exit.

 

Operating case "First download"

When downloading a PLC project to a controller that is in the delivery state, the memory locations of all variables must be set to the desired initial state. This assigns the required values to the data sections of function block instances. The explicit implementation of FB_init for function blocks in the code of the PLC project enables you to exert specific influence on the initialization in this situation.

By evaluating the FB_init method parameters “bInCopyCode” (FALSE) and “bInitRetains” (TRUE) you can detect this operating case unambiguously. (See also Operating case “Online Change”)

 
 
The FB_init method cannot be compared with the construct of a constructor, as it is known from C#, C++ or Java, since a function block in the PLC does not require a constructor to initialize its declared variables. This takes place implicitly or explicitly in the declaration lines.
The resulting consequences for function blocks that extend other function blocks are described in section Behavior with derived function blocks.
 
 

Alternative to FB_init

The initial assignments are processed after the download and before the first task cycle of a PLC project.

fbTimer : TON := (PT := T#500MS);

Such assignments are only executed after FB_init has been called. If TON had an FB_init method, the assigned time value of T#500MS would not be known in this method.

To control the effects of these assignments, you can assign the attribute {attribute 'call_after_init'} to a method of a function block. You must insert the attribute both above the declaration part of the corresponding method and above the declaration part of the function block body.

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.

 

During the download, the following calls are made one after the other:

1. FB_init
Initialize new instance (including previous implicit variable initialization, both with automatic zero initialization and individual value initialization)
new_inst.FB_init(bInitRetains := TRUE, bInCopyCode := FALSE);
By evaluating the FB_init method parameters, you can detect this operating case unambiguously. For example, you can have variables at the “new” position in the memory initialized accordingly or inform other parts of the PLC project about the new position of certain variables in the memory.
2. Explicit variable initialization via instance declaration of the function block
Copying the values of the assigned input variables of the function block
Beispiel:
<FB-Instanzbezeichner> : <FB-Name> := (<Variable>:=17);
The values of unassigned variables are retained.
3. Method declared with attribute call_after_init
Alternative initialization
new_inst.<Methodenname der gekennzeichneten Methode>();
To detect this operating case unambiguously, you can copy the value of bInCopyCode in FB_init into an auxiliary variable. For example, you can have variables at the “new” position in the memory initialized accordingly or inform other parts of the PLC project about the new position of certain variables in the memory. Make the implementation as independent as possible. The method can also be called from the PLC project at any time to restore a function block instance to its original state.
 
 

Operating case "New download"

Downloading a PLC project may replace an existing project on the controller. For this reason, the memory space for the existing function blocks must be released in a controlled manner. You can use the method FB_exit to implement the necessary steps. For example, you can set external resources (with socket or file handles) to a defined state or release dynamically allocated memory (__NEW or __DELETE operator).

By evaluating the FB_exit method parameter "bInCopyCode" (FALSE), you can detect this operating case unambiguously.

 
 

Operating case "Online Change"

During an online change, you can influence the initialization of function block instances via the methods FB_exit, FB_init and FB_reinit. As part of the Online Change, the changes made to the PLC project during offline operation are tracked in the current control system. Therefore, the “old” instances of the function blocks are replaced by their "new" siblings as smoothly as possible. If no changes were made in the declaration part of a function block before logging into the PLC project (i.e. changes were only made in the implementation), the data areas are not replaced. Only code blocks are replaced. The methods FB_exit, FB_init and FB_reinit are not called.

 
If you have made changes to the declaration of a function block that will lead to the copy process described above, you receive a message about “possible unintentional effects” during an Online Change. The message box Details contains a list of all instances to be copied.

In the code of the methods FB_init and FB_exit, the parameters “bInitRetains” (FALSE) and “bInCopyCode” (TRUE) can be evaluated to determine whether an Online Change is in progress.

During the Online Change, the following calls take place one after the other:

1. FB_exit
Exit old instance
old_inst.FB_exit(bInCopyCode := TRUE);
By evaluating the FB_exit method parameter, you can detect this operating case unambiguously. You can use the call of FB_exit when leaving the "old" instance to trigger certain cleanup tasks before copying. In this way, you can prepare the data for the next copy operation and influence the state of the "new" instance. Other parts of the PLC project can inform you about the imminent change of position in the memory. Pay particular attention to variables of type POINTER or REFERENCE. After the Online Change, these may no longer point to the desired storage locations. Interface variables (INTERFACE) are handled separately by the compiler and are adapted accordingly during the Online Change. External resources such as sockets, files or other handles can possibly be accepted unchanged by the new instance. In many cases they do not have to be treated separately during the Online Change. (See operating case "New download")
2. FB_init
Initialize new instance (including previous implicit variable initialization, both with automatic zero initialization and individual value initialization)
new_inst.FB_init(bInitRetains := FALSE, bInCopyCode := TRUE);
By evaluating the FB_init method parameters, you can detect this operating case unambiguously. The call of FB_init takes place after the copy operation and can be used to perform specific operations for the Online Change. For example, you can inform other parts of the PLC project about the new position of certain variables in the memory.
3. Method declared with attribute call_after_init
Alternative initialization
new_inst.<Methodenname der gekennzeichneten Methode>();
To detect this operating case unambiguously, you can copy the value of bInCopyCode in FB_init into an auxiliary variable. For example, you can inform other parts of the PLC project about the new position of certain variables in the memory. Make the implementation as independent as possible. The method can also be called from the PLC project at any time to restore a function block instance to its original state.
4. Copy process
Copying the function block values (copy code)
copy(&old_inst, &new_inst);
Existing values are retained. To this end, they are copied from the old instance to the new instance.
5. FB_reinit
Reinitialize new instance
new_inst.FB_reinit();
This method is called after the copy operation. It sets the variables of the instance to defined values. For example, you can have variables at the “new” position in the memory initialized accordingly or inform other parts of the PLC project about the new position of certain variables in the memory. The implementation should always be handled independent of the Online Change. The method can also be called from the PLC project at any time to restore a function block instance to its original state.
 
The attribute {attribute 'no_copy'} can be used to prevent a specific variable of the function block from being copied during the Online Change. In this case the variable always retains the initial value.
 
 

See also:

Object Method
Attribute 'call_after_init'
Attribute 'no_copy'
Attribute 'no-exit'
Additional information
FB_init
FB_reinit
FB_exit
Behavior with derived function blocks