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

method Fb_init

sarunas
2023-08-16
2023-08-16
  • sarunas - 2023-08-16

    Hi community,
    "Could you please explain to me how the method FB_init works? The block inside it states, 'the retain variables are initialized (reset warm / reset cold).' Every time I perform a 'cold' reset on the PLC, do I need to call FB_init? Am I thinking correctly?"
    Then I do that

    VAR_GLOBAL PERSISTENT
    x: UINT;
    END_VAR
    

    Create function block

    {attribute 'call_after_init'}
    FUNCTION_BLOCK Test
    VAR_INPUT
        y:                          INT;
    END_VAR
    VAR 
        output                      :INT;
    END_VAR
    output:=y*y;
    

    Create FB_init of function block

    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
    gvl.x:=gvl.x+1;
    

    Create main program

    PROGRAM MAIN
    VAR
        test: test;
    END_VAR
    test(y:=15);
    test(y:=16);
    

    I'm currently performing a cold reset on the PLC, and the value of 'x' is supposed to increase. However, the value of 'x' remains unchanged at 1 all the time. The variable 'x' is of the PERSISTENT type so a cold reset of the PLC doesn't to have any impact on it.
    Why can explain for me why that happen?

     

    Last edit: sarunas 2023-08-16
  • hermsen

    hermsen - 2023-08-16
     
    • sarunas - 2023-08-16

      I don't find information. All information about fb_init method is there "You can declare additional function block inputs in an FBinit method. Then you have to set these inputs in the declaration of the function block instance." and show example.
      So when fbinit of method is call.

       

      Last edit: sarunas 2023-08-16
  • TimvH

    TimvH - 2023-08-16

    You are trying to make something very simple really complex, but my best guess is that the order of execution is:
    - initialize FB (x becomes 1)
    - Load persistent (x is 1)
    - start application (x remains 1)

    PS, It could also be that your "persistent" variables are not stored correctly and that, at startup, the value is always 0. Did you add this variable to the Persistent Variable list? And did you make sure that this works correctly on the device you are using?
    So maybe the execution order is:
    - load persistent (x is 0, because it was not stored correctly)
    - initialize FB (x becomes 1)
    - start application (x remains 1)

     

Log in to post a comment.