How to pass init values to nested function blocks?

selli69
2018-03-08
2018-03-09
  • selli69 - 2018-03-08

    Hello @ all!

    I have problems to pass a Pointer during init to some FB's. Unfortunately, I cant post my whole application here, but my app structure is following:

    My Main app has an array of independent units. All of these units are called once per cycle:

    Program : PRG
    VAR
       // Array of unit function blocks
       aUnits: ARRAY [1..8] OF FB_Unit;
       i:   USINT;
    END_VAR
    FOR i:=1 to 8 DO
       aUnits[i]();
    END_FOR
    

    Each of these units has some FB's which implement the main unit functionalities. These are also called on every unit call. Note, that a pointer to the unit is passed to the FB_Init of the FB's:

    FUNCTION_BLOCK : FB_Unit
    VAR
       fbSomeFunction:   FB_SomeFunction(Unit:=THIS);
       fbAnotherFunction:   FB_AnotherFunction(Unit:=THIS);
    END_VAR
    fbSomeFunction();
    fbAnotherFunction();
    

    In these FB's init the pointer to the calling/defining unit is set. Although an array of subfunction is defined. Besides beeing called every cycle, they should also 'know' the pointer of 'their' corresponding unit:

    FUNCTION_BLOCK : FB_SomeFunction
    VAR
       pUnit:      POINTER TO FB_Unit;
       aSubFunctions:   ARRAY [0..10] OF FB_SubFunction(Unit:=pUnit);
       i:         USINT;
    END_VAR
    /////
    METHOD FB_Init : BOOL
    VAR_INPUT
       bInitRetains:   BOOL;
       bInCopyCode:    BOOL;
         Unit:         POINTER TO FB_Unit;
    END_VAR
    pUnit := Unit;
    /////
    FOR i:=0 to 10 DO
       aSubFunctions[i]();
    END_FOR
    

    The subfunction looks like this:

    FUNCTION_BLOCK : FB_SubFunction
    VAR
       pUnit:      POINTER TO FB_Unit;
    END_VAR
    /////
    METHOD FB_Init : BOOL
    VAR_INPUT
       bInitRetains:   BOOL;
       bInCopyCode:    BOOL;
         Unit:         POINTER TO FB_Unit;
    END_VAR
    pUnit := Unit;
    /////
    

    My problem is, that SomeFunction initalizes correctly containing the correct pointer to the calling/defining unit, but the Subfunctions (array) does not. The unit pointer is null after init. How do I accomplish that the pointer is set correctly in the subfunctions?

    I know, it is no problem to set this pointer in the first cycle programmatically via an input var, but my visualisation relies on calls based on the unit pointer and crashes when the unit pointer is null. Also some other init code relies on the correct reference of this unit pointer. Maybe my design isnt the best here.

    Hope someone can help.
    Thanks forward!

     
  • josepmariarams - 2018-03-09

    Fb_init is called afer your variables inicialization. When you create your subfunction your fb_init in not called yet and the unit is not initialized

     
  • selli69 - 2018-03-09

    Hi Josep,

    Thanks for your reply! I solved my Problem by adding a (pointer to unit) property to the subfunction and set this in the FB_Init of "SomeFunction". This seems to work.

     

Log in to post a comment.