FB_Init to define length of array in FB

gabe-ctrl
2021-02-01
2021-02-02
  • gabe-ctrl

    gabe-ctrl - 2021-02-01

    Hi,is there a method to pass a number through FB_Init and use it to define the length of the array which is internal to the FB?

    Example

    METHOD PUBLIC FB_Init : BOOL
    VAR_INPUT
        bInitRetains : BOOL; // initializing of retain variable
        bInCopyCode : BOOL; // instance is copied to copy code
        ArrLength : INT; // additional input: number of the COM interface, that is to be observed
    END_VAR
    
    This^.ArrLen := ArrLength;
    

    Where ArrLen is used in the FB as shown, to define array length?

    FUNCTION_BLOCK FB_Example
    VAR CONSTANT
        ArrLen : INT;
    END_VAR
    
    VAR
        Array1 : ARRAY [0..**ArrLen**] OF INT;
    END_VAR
    

    I understand Codesys forces you to use a constant to define the Array length and I cannot write to a constant. So I am hoping for a workaround. The idea is to make the FB use the correct amount of memory for the array on instantiation.

    It would make sense that since the function block is instantiated with FB_Init I should be able to define the length of the array with the FB_Init.

    Thanks.

     
  • gabe-ctrl

    gabe-ctrl - 2021-02-02

    Thanks for clarifying. I did not realize they ran at different times.
    I will look into the FB_Factory and see what I can do. Thank you.

     

Log in to post a comment.