plctwincatststructured-textiec61131-3

Why can't I pass output variable as a parameter to function block constructor in IEC61131-3 structured text (TwinCAT3)?


I have a function block A that has one variable output (defined in the FUNCTION_BLOCK A method) and this FB_init method:

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
    
    VAR_OUTPUT
        output : REAL := 0;
    END_VAR

THIS^.output := output;

When I call this constructor as follows:

a : A(output => outputLocal);

Solution

  • By declaring output in the VAR_OUTPUT section of the method, you are making it a local output variable to the method, not an input variable.

    You need to declare a VAR_OUTPUT in the FunctionBlock and a VAR_INPUT in the constructor and assign the input variable from the constructor to the output variable of the FunctionBlock—probably by REF=, or you'll just get the value.