FB_Init, FB_Reinit, and FB_ExitMethods
General Purpose of the Methods
You can explicitly use the methods FB_Init and FB_Reinit to influence the initialization of function block variables as well as the behavior when exiting function blocks.
This chapter describes the methods, and the applications and effects of the methods in different conditions that require variable initialization.
By default, the FB_Init method is available implicitly. It is used by EcoStruxure Machine Expert to initialize a function block or a structure.
In order to influence the initialization, you can explicitly declare the FB_Init method by extending the given default initialization code. This allows you to evaluate the return value.
The FB_Reinit method must be declared explicitly.
If the FB_Reinit method is available, it is called after the instance of the corresponding function block has been copied (during an online change after modifications in the function block declaration). It reinitializes the new instance module. The return value is not evaluated. In order to achieve that the base function block is reinitialized, call FB_Reinit explicitly for that function block. This allows you to evaluate the return value.
The FB_Exit method must be declared explicitly.
If there is an implementation, then the method is called before the controller removes the code of the function block instance (implicit call). The return value is not evaluated.
The following paragraphs provide use cases of these methods for different operating conditions.
When you download an application to a controller that is in default state, the memory locations of the variables are set to the desired initial state. The data areas of function blocks are set to the desired values. You can influence this process by explicitly implementing FB_Init for function blocks in the program code of the application.
The method parameters bInCopyCode set to FALSE and bInitRetains set to TRUE indicate that a first download is being executed.
When an Online Change command is executed, the methods FB_Exit, FB_Init, and FB_Reinit can be used to influence the initialization of function blocks.
During online change, the modifications made to the application in offline mode are downloaded to the controller. The instances of function blocks are updated by the new instances as follows:
If you have only made changes in the implementation part of the function block and not in the declaration part, the data areas are not replaced. The methods FB_Init, FB_Reinit, and FB_Exit are not called.
If you have made changes in the declaration part of a function block, the copy process described in the FB_Reinit paragraph is performed when the Online Change command is executed. A list of the objects that have been changed since the last download is provided in the Application Information dialog box. Open this dialog box by clicking the Details... button in the dialog box where you select the option Login with online change.
The FB_Init and FB_Reinit method parameters bInCopyCode set to TRUE and bInitRetains set to FALSE indicate that an online change is being executed.
Calls Executed During an Online Change
Executing the Online Change command can change the contents of addresses.
|
INVALID POINTER |
Verify the validity of the pointers when using pointers on addresses and executing the Online Change command. |
Failure to follow these instructions can result in injury or equipment damage. |
During an online change, the following calls are executed consecutively:
Step |
Action |
Comment |
---|---|---|
1 |
FB_Exit |
old_inst.FB_Exit(bInCopyCode := TRUE); FB_Exit is called to initiate a cleanup process before the copy process is started. It prepares the data for the next copy process and influences the state of the new instance. Other parts of the application are informed about the position changes that are performed in the memory. Keep in mind that variables of type POINTER or REFERENCE keep their values during an online change and may no longer refer to the desired memory locations after the process has been completed. Variables of type INTERFACE are adapted during online change. External resources, such as socket, file, or other handles possibly can be adopted by the new instance, and often need no separate treatment during online change when the system resource is not affected by the online change copy process. If needed, this must be treated in Init or Reinit implementations. |
2 |
FB_Init |
new_inst.FB_Init(bInitRetains := FALSE, bInCopyCode := TRUE); FB_Init can be used to perform specific operations during the online change process. These are, for example, appropriately initializing variables at the new memory locations, or providing information on the new position of certain variables to other parts of the application. |
3 |
Copy operation copy |
copy(&old_inst, &new_inst); Existing values remain unchanged. For this purpose, they are copied from the old instance into the new instance. |
4 |
FB_Reinit |
new_inst.FB_Reinit(); The FB_Reinit method is called after the copy operation. It sets the variables of the function block instance to defined values. For example, you can appropriately initialize variables at the new memory locations, or provide information on the new position of certain variables to other parts of the application. Implement the FB_Reinit method independent of online change because the method can be called by the application anytime, whenever a function block is to be reset to the original state. |
NOTE: If you add the pragma {attribute no_copy} to a variable of a function block, this variable will not be copied during online change; it will only be initialized.
Downloading an Updated Application
When you download an application to a controller that is already running an application, the existing application will be replaced. You can use the FB_Exit method, for example, to assign a defined state to external resources (such as socket or file handles).
The method parameters bInCopyCode set to FALSE and bInitRetains set to FALSE indicate that a download of an updated application is being executed.
Before the first cycle of the tasks of an application is executed, the initial assignments are processed.
Example:
T1 : TON := (PT:=t#500ms);
The assignments are executed after FB_Init has been called. To be able to verify the impacts of these assignments, attach the {attribute call_after_init} pragma to a function block and a method of a function block (for example, called MyInit). Insert this attribute above the declaration part of the function block and above the declaration part of the corresponding method. Attach this pragma also to function blocks that extend other function blocks which are using the {attribute call_after_init} pragma. It is a good practice to assign the same name, the same signature, and the same attribute to the corresponding method. To achieve this, call SUPER^.MyInit. Select a method name of your choice (except FB_Init, FB_Reinit, and FB_Exit). The method is called after the initial assignments have been processed and before the tasks of an application are started.
NOTE: When the explicitly defined initialization code is executed, then the function block has already been initialized completely via the implicit initialization code. Due to this, calling SUPER^.FB_Init is not allowed.
Interface of the FB_Init Method
METHOD FB_Init : BOOL
VAR_INPUT
bInitRetains : BOOL; // TRUE: the retain variables are initialized (reset warm /reset cold)
bInCopyCode : BOOL; // TRUE the instance will be copied to the copy-code afterward (online change)
END_VAR
The return value is not used.
In an FB_Init method, you can declare additional function block inputs. Assign the inputs in the declaration of a function block instance.
Example: Method FB_Init for a function block serialdevice:
METHOD PUBLIC FB_Init : BOOL
VAR_INPUT
bInitRetains : BOOL; // Initialization of the retain variables
bInCopyCode : BOOL; // Instance is copied to copy-code
iCOMnum : INT; // additional input: number of the COM interface that is to be observed
END_VAR
Instantiation of function block serialdevice:
com1: serialdevice (iCOMnum:=1);
com0: serialdevice (iCOMnum:=0);
Interface of the FB_Reinit Method
METHOD FB_Reinit : BOOL
Interface of the FB_Exit Method
The parameter bInCopyCode. is mandatory.
METHOD FB_Exit : BOOL
VAR_INPUT
bInCopyCode : BOOL; // TRUE: the exit method is called in order to leave the instance which will be copied afterwards (online change).
END_VAR
If a function block is derived from another function block, then the FB_Init method of the derived function block must define the same parameters as the FB_Init method of the base function block. However, you can add further parameters in order to implement a special initialization for the instance.
Example for the Call Order of Derived Function Blocks for FB_Exit and FB_Init
The following is assumed for the POUs named in this list: SubFB EXTENDS MainFB and SubSubFB EXTENDS SubFB:
Step |
Action |
---|---|
1 |
fbSubSubFb.FB_Exit(...); |
2 |
fbSubFb.FB_Exit(...); |
3 |
fbMainFb.FB_Exit(...); |
4 |
fbMainFb.FB_Init(...); |
5 |
fbSubFb.FB_Init(...); |
6 |
fbSubSubFb.FB_Init(...); |