1

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.

Guiorgy
  • 1,405
  • 9
  • 26
  • I tried your code on Twincat 3 and Codesys 3.5 and I'm not experiencing any issues. On both platforms the code runs without any problems. – Filippo Boido Jul 21 '20 at 17:23
  • @FilippoBoido, I'll remind you, just building the project didn't result in any errors, but running a simulation resulted in the error above, and a log: "No bootproject file $PlcLogic$/Sim.LMC_PacDrive.Application/Sim.LMC_PacDrive.Application.app available!". If your simulation is running without problems, then I'll try contacting Schneider directly. Thank you for your help! – Guiorgy Jul 22 '20 at 09:20
  • @FilippoBoido, I just downloaded CODESYS 3.5.16 and can confirm that everything seems to work as intended. If you make an Answer, I'll accept it. – Guiorgy Jul 22 '20 at 10:54

1 Answers1

1

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.

Filippo Boido
  • 1,136
  • 7
  • 11
  • This is a little off topic, but do you know of a way to prevent the retention of a local variable inside a function block? e.i. suppose POU1 has the keyword RETAIN, and POU1 has val1: INT and val2: INT. In such case, I only want to retain val1, and I want to reset val2. – Guiorgy Jul 22 '20 at 17:41
  • Sorry, I'm not quite sure what you mean.Formulate a new question with more details please. – Filippo Boido Jul 22 '20 at 18:59
  • I did so, would you please have a look? https://stackoverflow.com/questions/63059799/prevent-local-variable-retention – Guiorgy Jul 23 '20 at 17:22