Function Blocks and arrays of function blocks

jackbrady
2024-02-14
2024-02-15
  • jackbrady - 2024-02-14

    Hello,

    I am new to Codesys and PLC programming in general (please go easy ha!) I'm not looking for code to be written for me just some help and pointing in the right direction.

    I am writing some code to send commands to a relay based on input values (to put it simply). Quite basic stuff.

    I have wrote a function block that takes a global variable (Open_command:BOOL) and outputs to another global variable (Opened : BOOL).

    The function block is simulating a device so I'll eventually get the globals from that.

    I now need to create multiple versions of this function block/ device (lets say 100) but I need each iteration of that function block to reference it's own relevant global variable. I think that the best way of doing this would be to use arrays, although I could be wrong. I am aware that for up to 100 instances I could very well manually assign everything but that seems rather time consuming and I want a fancier way of doing it.

    Here is a very basic example of what I am looking to do, please note I have not written this in proper code it's just to show what I mean.

    Global Variables
    V[0-100] int
    Open_command [0-100] Bool
    Opened [0-100] Bool

    Function Block
    var input
    x : BOOL
    Var output
    y : BOOL

    if x then
    y = TRUE ELSE
    y = FALSE

    The input to my function block will be Open_command, output will be Opened

    Example code.
    If V[x] > 10 then
    Open_command [x] = TRUE
    ELSE
    Open_command [x] = FALSE

    (So when V1 goes above 10 I need Open_command1 = TRUE therefore initiating FB1 output.
    V2 > 10, open_command2 = True > FB2 output
    V3 > 10, open_command3 = True > FB3 output
    ...
    ...
    )

    What I can't seem to figure out is how to tie all this together, I have read through the codesys documentation and if anything it has confused me more! ha.

    Apologies for the poorly written post but hopefully you understand what I am trying to get at.

    Thanks,
    Jack

     
  • alexgooi

    alexgooi - 2024-02-15

    Hi Jack,

    I think you have to look at a FB in a different way.
    A Function block (Class) can contain its own data.
    In other words don't define loose data in your GVL, but define a instance of a FB (Object) in your GVL:

    Example:

    Function_Block Basic_Class
    VAR_INPUT
        Open_Command: BOOL;
    END_VAR
    VAR_OUTPUT
        Opened:       BOOL;
    END_VAR
    
    if Open_Command then
        Opened := TRUE;
    ELSE
        Opened := FALSE;
    END_IF
    
    Global Variables
       Objects: ARRAY[1..100] OF  Basic_Class; //Here you ar defining you objects
    END_VAR
    

    In your code you can directly acces the data and couple it to the IO:

    GVL.Objects[1].Open_Command := %IX0.0;
    %QX0.0 := GVL.Objects[1].Opened;
    
    //To call the code itself use:
    GVL.Objects[1]();
    

    If you want to take this a step further you are also able to add methods and properties to the FB/Class end thereby creating a OOIP program

     
    πŸ‘
    1

    Related

    Talk.ru: 1


    Last edit: alexgooi 2024-02-15
    • jackbrady - 2024-02-15

      Thanks for the reply. Much appreciated.

       

Log in to post a comment.