Jakub Szlaur
Jakub Szlaur

Reputation: 2132

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);

Upvotes: 0

Views: 1464

Answers (2)

Kent Hulick
Kent Hulick

Reputation: 26

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.

Upvotes: 1

Jakob
Jakob

Reputation: 1540

Even though you technically can have output parameters in the FB_Init-method, you can't get them out from the call of FB_Init. My guess is that it has to do with how the object is being constructed in time, and therefore it makes no sense.

The more important questions is: What are you trying to achieve? In the above example it seems you are trying to access the parameter output, which I assume is an output of the entire function block, from the FB_Init-call? You seem to be writing to an output of the function block, from an output of an method call, which doesn't make any sense.

If you only want to pass a parameter as an output from a function block, why not provide it as an input-parameter in that case? (either by copy or by reference)?

Upvotes: 0

Related Questions