Search code examples
codesys

Persistent Vars list with function blocks with FB_Init


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)


Solution

  • 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