Search code examples
codesys

Retain a Function Block with FB_Init method


If I have a Function Block with an explicitly defined FB_Init method, I can't seem to use the Retain keyword, as I get a C0138: No matching FB_init method found for instantiation of POU error, for example:

FUNCTION_BLOCK POU1
VAR
    _val: INT;
END_VAR
METHOD FB_Init : BOOL
VAR_INPUT
    bInitRetains : BOOL;
    bInCopyCode : BOOL;
    val: INT;
END_VAR
THIS^._val := val;
PROGRAM SR_Main
VAR RETAIN
    p1: POU1(val := 10); // C0138:  No matching FB_init method found for instantiation of POU1
END_VAR

The project seems to build without error, until I try to run a simulation that fails with the above error. I am using Machine Expert 1.2.3 (CODESYS 3.5.12 I believe)

How do I retain Function Blocks with FB_Init methods?

PS. The only way I found so far, is to manually write the RETAIN keyword on every VAR type inside the Function Block (VAR, VAR_OUTPUT), but then I'll have to have 2 separate version of this object (unretained, and retained, like POU1 and POU1_RETAINED for example), not to mention that, that will not work with PERSISTENT.


Solution

  • The code from Guiorgy runs as intended on both Twincat 3 and Codesys 3.5.

    The error, which occurs when running the example using Machine Expert, must be a specific Schneider issue.