1

I am trying to persist a function block (POU) in CODESYS 3.5.16, but I am getting C0138: No matching 'FB_Init' method found for instantiation of POU error.

PersistentVars:

VAR_GLOBAL PERSISTENT RETAIN
    PLC_PRG.p: POU;
    // PLC_PRG.p: POU(val := 10); // also tried this
    PLC_PRG.p1: POU1;
END_VAR

POU:

FUNCTION_BLOCK POU
VAR
    _val: INT;
END_VAR

METHOD FB_Init: BOOL
VAR_INPUT
    bInitRetains: BOOL; // TRUE: the retain variables are initialized (reset warm / reset cold)
    bInCopyCode: BOOL;  // TRUE: the instance will be copied to the copy code afterward (online change)
    val: INT;
END_VAR
THIS^._val := val;

POU1:

FUNCTION_BLOCK POU1
VAR_INPUT
    val: INT;
END_VAR
VAR
    _val: INT;
END_VAR
_val := val;

PLC_PRG:

PROGRAM PLC_PRG
VAR PERSISTENT
    p: POU(val := 10);
    p1: POU1;
END_VAR
(*VAR
    p: POU(val := 10);
END_VAR*) // also tried this
p1(val := 20);

What am I doing wrong? (POU1 works as intended)

Guiorgy
  • 1,405
  • 9
  • 26

1 Answers1

1

In Codesys you can't apparently have a Persistent FB and at the same time an FB_Init method with custom parameters.

The solution with codesys, is to delete the custom parameter(s) of the fb_init method or try another initialization solution for you program. A different initialization solution can be found in the answer to one of your previous questions.

As a side note: This problem does not arise if you use the Twincat platform. In fact you don't even have a thing as a separate Persistent Vars list.

Note though, this is wrong if you use Twincat:

VAR_GLOBAL PERSISTENT RETAIN
    PLC_PRG.p: POU;
    // PLC_PRG.p: POU(val := 10); // also tried this
    PLC_PRG.p1: POU1;
END_VAR

You either declare your FBs in PLC_PRG or in VAR_GLOBAL.

(Technically you can declare 2 different instances with the same name, one in PLC_PRG one in your global list, but it's not really good style naming them the same)

If you declare them in the global list then like this:

VAR_GLOBAL PERSISTENT RETAIN
    p : POU;
END_VAR
Filippo Boido
  • 1,136
  • 7
  • 11
  • What I did was following the [CODESYS documentation](https://help.codesys.com/api-content/2/codesys/3.5.13.0/en/_cds_vartypes_retain_persistent/#db08df6492224edc0a8640e01f170c4-id-e792657b492224ecc0a8640e004ed201): *" VAR PERSISTENT declarations that are present in other POUs are added to the list using the menu command Declarations ‣ Add all instance paths."* Besides, I followed you suggestion (commented the declaration in PLC_PRG) and got the error *C0138*. (I tried both *p: POU;* and *p: POU(val := 10);*). Don't forget, the *POU* FB has the *FB_Init* method – Guiorgy Jul 25 '20 at 15:18
  • You don't have Add all instance paths in Twincat..another reason not to rely on this approach if you want to stay cross-platform..besides it doesn't seem to work with the new codesys compiler anyway ;) . Of course you need to declare p with parameters if you have any in your fb_init..just trying to keep it simple as you should do... – Filippo Boido Jul 25 '20 at 15:47
  • So as I said, putting the declaration the the *PersistantVarList* with or without the constructor causes a build error. Doing it in the *PLC_PRG* does build, however it gives me the warning: *"C0244: No matching instance path in VAR_PERSISTENT list found for variable PLC_PRG.p. Use the command "Add all instance paths" to add all instance paths to the VAR_PERSISTENT list."* Although the simulation runs, the value p._val does NOT persist through a warm reset! – Guiorgy Jul 25 '20 at 16:02
  • If you use Twincat, you won't have such issues, otherwise delete the fb_init method.. – Filippo Boido Jul 25 '20 at 16:12
  • I tried it in Machine Expert too (CODESYS 3.5.12 compiler) and same result, so either we are doing different things, or Twincat somehow is an exception? – Guiorgy Jul 25 '20 at 16:24
  • My values stay persistent after warm restart, even with the c0244 warning in codesys. – Filippo Boido Jul 25 '20 at 16:29
  • You are aware that fb_init is not called after warm restart right? – Filippo Boido Jul 25 '20 at 16:30
  • I definitely recommend you to check out Twincat – Filippo Boido Jul 25 '20 at 16:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/218585/discussion-between-guiorgy-and-filippo-boido). – Guiorgy Jul 25 '20 at 16:33
  • @Guiorgy I modified the answer. – Filippo Boido Jul 29 '20 at 13:42
  • When you say that it's impossible to do it in CODESYS, do you have a source for me to read by chance? – Guiorgy Jul 29 '20 at 15:26
  • 1
    No, I don't have any source and I must say, documentation is not the strong side of codesys. I came to this conclusion through empiric testing. Unfortunately for you, I don't think there is a secret switch to make it work the way you wish on Codesys.Some adjustments are necessary. – Filippo Boido Jul 29 '20 at 16:03