Array declaration with FB_Init

2014-03-12
2020-08-21
  • DavidWSteadman - 2014-03-12

    I created an empty function block called MyObject, then declared an array of instances of the function block, as such:

    MyArray : ARRAY [1..2] OF MyObject;
    

    So far, so good. Now I add an FB_Init method to MyObject:

    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)
      ID : UINT;
    END_VAR
    

    The question is, how do I now declare an array of MyObject? My original declaration doesn't work any more because the objects can't be created without the 'ID' parameter. But I can't figure out any way to pass it in. I was hoping something along the lines of this would work:

    MyArray : ARRAY [1..2] OF MyObject := [ (ID:=24), (ID:=17) ];
    

    but that doesn't compile.

    Any suggestions on how to do this?

     
  • Anonymous - 2014-05-15

    Originally created by: scott_cunningham

    I have found adding VAR_INPUTS to Fb_init() to be a little troubling - intellisense doesn't show the VAR_INPUTS, so there are no hints to the user. Also, an unfriendly error "No matching FB_init method found for instantiation...". So, I normally just create my own "init" method that I call during first boot and initialize necessary items. A bit of a workaround, but also very clear what is happening to other programmers:

    FUNCTION_BLOCK MyObject
    METHOD MyInit
    VAR_INPUT
       ID: UINT;
    END_VAR
    ...
    END_METHOD
    PROGRAM PLC_PRG
    VAR
       MyArray: ARRAY[1..2] OF MyObject;
       FirstBoot: BOOL:= TRUE;
    END_VAR
    IF FirstBoot THEN
       MyArray[1].MyInit(ID:= 24);
       MyArray[2].MyInit(ID:= 17);
       ...
       FirstBoot:= FALSE;
    END_IF
    ...
    END_PROGRAM
    
     

    Related

    Talk.ru: 1
    Talk.ru: 2

  • aikapan - 2020-08-21

    get rid of this:

    :=
    

    FB_init is a method/funciton and needs to be called! not assigned...

    Do it like the following:

    MyArray : ARRAY [1..2] OF MyObject[ (ID:=24), (ID:=17) ];
    

    Then intellisense works at least at the first line though

     

    Last edit: aikapan 2020-08-21

Log in to post a comment.