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

Instantiation id for a Function Block.

learnetk
2016-11-14
2016-11-15
  • learnetk - 2016-11-14

    Hi Everyone
    I made a Function Block that would be part of a Library. The user of this library could use as many instances of the said function block which is not known at the time of implementation of the Function Block / Library.

    Is it possible to dynamically create an Id for each instance of the function block that the User creates. Does anyone have an idea how this could be done.

    Thanks and regards

     
  • Anonymous - 2016-11-14

    Originally created by: scott_cunningham

    What is wrong with letting the user define the name when the user uses your FB?

    PROGRAM Example
    VAR
       Inst1 : YourLibrary.YourFB;
       Inst2 : YourLibrary.YourFB;
       Inst3 : YourLibrary.YourFB;
    END_VAR
    
     
  • learnetk - 2016-11-15

    Hi Scott

    Probably i couldnt explain my issue.
    So i try it again, inside the function block i have a variable which is the Id of the function block, just as shown below

    Function_Block Motor
    var
    Motor_id : uint;
    end_var

    so if the User of the library has an two Instances of the function block like

    fbMotor1 : Motor;
    fbMotor2 : Motor;

    then i would like the Motor_Id in fbMotor1 to have 1 and Motor_Id in fbMotor2 is 2, or any other number.

    Thanks and warm regards.

     
  • Anonymous - 2016-11-15

    Originally created by: scott_cunningham

    Use VAR_STAT to define an internal FB var that is static across FB instances. Then add an Fb_INIT method (see the help) that will increment the counter and assign to a var output.

    FUNCTION_BLOCK Motor
    VAR_OUTPUT
       MotorId : UINT;
    END_VAR
    VAR_STAT
       InstanceCount : UINT;
    END_VAR
    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
    THIS^.InstanceCount := THIS^.InstanceCount + 1;
    THIS^.MotorId := THIS^.InstanceCount ;
    
     
  • learnetk - 2016-11-15

    Oh..Thankyou so much Scott..
    this works like a charm..!!

    Thanks again and warm regards

     

Log in to post a comment.