Search code examples
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);
  • I get syntax error: Identifier 'output' not defined. But I did defined this parameter in the constructor method.
  • When I try to compile the project I get another error: No matching FB_init method found for instantiation of A
  • Does anyone know where is the problem? And how I can pass variable as an output parameter to function block?
  • I want basically to link the localoutput to the variable output (of the FB A) in the FB constructor.

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.