FB_init

FB_init is always implicitly available and is generally called in order to perform the standard initialization (implicit call). You can also explicitly declare the method and add additional code to the standard initialization code to exert specific influence.

If the explicitly defined initialization code is reached during the execution, the function block instance is already fully initialized via the implicit initialization code in the case of both automatic zero initialization and individual value initialization. Therefore, no SUPER^.FB_init call is allowed.

FB_init 1:

Debugging

Finding errors in the FB_init methods is laborious because, among other things, setting breakpoints cannot have the desired effect.

FB_init 2:

Automatic core dump on exception in FB_init/FB_reinit/FB_exit

If an exception error occurs within the FB_init/FB_reinit/FB_exit code, e.g. due to a programming error, the runtime system automatically stores a core dump on the target system (from TC3.1 Build 4024.25). This core dump is stored as a *.core file in the boot folder of the target system (by default under C:\TwinCAT\3.1\Boot\Plc) and can be used for the search of causes.

For more information on loading a core dump, see: Error analysis with core dump

FB_init 3:

Observe the order of initialization

Please note that the values assigned to the input variables of the function block instance are not yet known when FB_init is executed. If you wish to parameterize the execution of FB_init, you can declare additional method inputs in the FB_init method.
For more information see Operating cases.

Explicit call of FB_init

FB_init 4:

Explicit calling is not recommended

The methods FB_init, FB_reinit and FB_exit are system functions that are called implicitly at different times (further information on this can be found under Operating cases). Explicitly calling these methods can have inadvertent consequences and is therefore not recommended.

In principle, the method FB_init can also be called explicitly. However, this is not recommended.

In the case of an explicit call, not only is the explicit initialization code of the FB_init method executed again, but also the implicit initialization is repeated. For example, all variables are re-initialized, both in the case of automatic zero initialization and in the case of individual value initialization.

The effect of the latter is that both initialization steps are also performed for the FB instances declared in this instance. This procedure continues over the further instance levels contained.

If FB instances are re-initialized without being de-initialized first, this can lead to problems in the further program sequence, for example if the FB_init method contains dynamic memory allocations or an instance counter.

As the FBs are not used here for their intended purpose and it may not be possible to estimate the problematic consequences, we expressly advise you not to call FB_init methods explicitly.

Interface of the method FB_init

METHOD FB_init : BOOL
VAR_INPUT
    bInitRetains : BOOL; // if TRUE, the retain variables are initialized (warm start / cold start)
    bInCopyCode  : BOOL; // if TRUE, the instance afterwards gets moved into the copy code (online change)
END_VAR

Through the evaluation of the FB_init method parameters you can distinguish between the operating cases and adapt the implementation if necessary. (See Operating cases)

Method parameters

(first/new) download

Online Change

TwinCAT restart (without new download)

bInitRetains

TRUE

FALSE

FALSE

bInCopyCode

FALSE

TRUE

FALSE

You can declare additional method inputs in an FB_init method. In this case, you have to set these inputs in the declaration of the function block instance.

Return value

Implicit calls

If the methods are called implicitly the return value will not be evaluated by the system. Even if you adjust the return value, it will not be evaluated with an implicit call.

Explicit calls

If the methods are called explicitly you can evaluate the return value. You can thus return a meaningful return value.

FB_init with derived function blocks

If a function block is derived from another function block, then the FB_init method of the basic function block is automatically executed for this function block. If the FB_init method of the derived function block is explicitly added, it is executed following the FB_init method of the basic function block (see Behavior with derived function blocks).

If the FB_init method of the derived function block is to be present in explicit form, it must define the same parameters as the FB_init method of the basic function block. Further parameters can be added, in order to set up a special initialization for the derived instance.

FB_init 5:

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.

Samples for the assignment of additional FB_init parameters

Sample 1:

Method FB_SerialDevice.FB_init

METHOD PUBLIC FB_init : BOOL
VAR_INPUT
    bInitRetains : BOOL; // if TRUE, the retain variables are initialized (warm start / cold start)
    bInCopyCode  : BOOL; // if TRUE, the instance afterwards gets moved into the copy code (online change)
    nComNum      : INT;  // additional input: number of the COM-interface that is to be observed
END_VAR

Declaration of the function block FB_SerialDevice:

fbCom1  : FB_SerialDevice(nComNum := 1);
fbCom0  : FB_SerialDevice(nComNum := 0);

Sample 2:

Function block FB_Sample:

FUNCTION_BLOCK FB_Sample
VAR
    nStartValue : INT;
END_VAR

Method FB_Sample.FB_init:

METHOD FB_init : BOOL
VAR_INPUT
    bInitRetains : BOOL; // if TRUE, the retain variables are initialized (warm start / cold start)
    bInCopyCode  : BOOL; // if TRUE, the instance afterwards gets moved into the copy code (online change)
    nValue       : INT;  // parameter to initialize the start value
END_VAR
nStartValue := nValue;

Declaration of the function block FB_Sample:

PROGRAM MAIN
VAR
    fbSample1 : FB_Sample(123); // => fbSample1.nStartValue is set to 123
    fbSample2 : FB_Sample(456); // => fbSample2.nStartValue is set to 456
END_VAR

Sample 3:

In the following sample, an input variable and a property are initialized by a function block that has an FB_init method with an additional parameter.

Function block FB_Sample:

FUNCTION_BLOCK FB_Sample
VAR_INPUT
    nInput           : INT;
END_VAR
VAR
    nLocalInitParam  : INT;
    nLocalProp       : INT;
END_VAR

Method FB_Sample.FB_init:

METHOD FB_init : BOOL
VAR_INPUT
    bInitRetains : BOOL;  // if TRUE, the retain variables are initialized (warm start / cold start)
    bInCopyCode  : BOOL;  // if TRUE, the instance afterwards gets moved into the copy code (online change)
    nInitParam   : INT;
END_VAR
nLocalInitParam := nInitParam;

Property FB_Sample.nMyProperty and the associated Set function:

PROPERTY nMyProperty : INT
nLocalProp := nMyProperty;

Program MAIN:

PROGRAM MAIN
VAR
    fbSample  : FB_Sample(nInitParam := 1) := (nInput := 2, nMyProperty := 3);
    aSample   : ARRAY[1..2] OF FB_Sample[(nInitParam := 4), (nInitParam := 7)]
                            := [(nInput := 5, nMyProperty := 6), (nInput := 8, nMyProperty := 9)];
END_VAR

Initialization result:

  • fbSample
    • nInput = 2
    • nLocalInitParam = 1
    • nLocalProp = 3
  • aSample[1]
    • nInput = 5
    • nLocalInitParam = 4
    • nLocalProp = 6
  • aSample[2]
    • nInput = 8
    • nLocalInitParam = 7
    • nLocalProp = 9

See also: