Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

FB_init and var init

2018-02-09
2018-02-12
  • wpmccormick - 2018-02-09

    I'm having trouble with initializing function blocks inside of function blocks:

    FUNCTION BLOCK FB_Child
    VAR
    cVar: DINT
    END
    METHOD FB_Init
    VAR_INPUT
    bInitRetains: BOOL;
    bInCopyCode: BOOL;
    SomeVar: DINT;
    END_VAR

    THIS^.cVar := SomeVar;

    FUNCTION BLOCK FB_Parent
    VAR
    pVar: DINT;
    Child: FB_Child(myVar);
    END_VAR

    METHOD FB_Init
    VAR_INPUT
    bInitRetains: BOOL;
    bInCopyCode: BOOL;
    SomeVar: DINT;
    END_VAR

    THIS^.myVar := SomeVar;

    PROGRAM TestProg
    VAR
    Test: DINT;
    Parent: FB_Parent(1234);
    END_VAR

    Obviously, this program does nothing but initialize a single variable, or should. The problem I'm having is that the Child.cVar (fairy syntax) doesn't have the value 1234, per my example.

     
  • josepmariarams - 2018-02-10

    Fb_init is called after the variables initialization.

    See in online help how is the initialization sequence

    Enviat des del meu Aquaris M5.5 usant Tapatalk

     
  • wpmccormick - 2018-02-12

    OK, thanks. I saw something that said FB_Init happened before VAR init.

    What is the function/method decorator to have it called after init? I think that will work for me.

    Thanks.

     
  • josepmariarams - 2018-02-12

    Look at pragmas help. There are one called call_after_init or something similar. It doesn t work if it is called from a library.

    There exist another method which is instantiate an fb from var. If you instantiate it as the first var in your fb, the fb_init of the new fb will be called before your variables initialization.

    Be healty!

     

Log in to post a comment.