Stefan Hamcke - 1 hour ago

Hello,

I have written a function block that uses a generic constant for the upper bound of an array. I wanted to have this array in the persistent memory area, thus I constructed my FB as follows:

FUNCTION_BLOCK FbWithGenericArrayLength
VAR_GENERIC CONSTANT
    uiArrayLength : UINT := 10;
END_VAR
VAR PERSISTENT
    arr : ARRAY[1..uiArrayLength] OF INT;
END_VAR

In my PLC_PRG I declare an instance of said FB

PROGRAM PLC_PRG
VAR
    myFb : FbWithGenericArrayLength<10>;
END_VAR

This compiles just fine. When I let CODESYS fill my persistent list, it adds duplicate entries

{attribute 'qualified_only'}
VAR_GLOBAL PERSISTENT RETAIN
    // Generated instance path of persistent variable
    PLC_PRG.myFb.arr: ARRAY [1..uiArrayLength] OF INT;
    // Generated instance path of persistent variable
    PLC_PRG.myFb.arr: ARRAY [1..uiArrayLength] OF INT;
END_VAR

so I manually delete the duplicate. When i now compile, I get errors

            ------ Build started: Application: Device.Sim.Device.Application -------
            Typify code...
[ERROR]     Sandbox_FW31: PersistentVars [Device: PLC Logic: Application](Line 4): C0161:  Border 'uiArrayLength' of array is no constant value
[ERROR]     Sandbox_FW31: PersistentVars [Device: PLC Logic: Application](Line 4): Type of mapped variable is not equal to original type
            Compile complete -- 2 errors, 0 warnings
            Build complete -- 2 errors, 0 warnings : No download possible

Is is simply not possible to use a generic constant for the persistent array?

Thanks in advance.


Edit: It seems I can manually replace the uiArrayLength in the persisten list by the value 10 I used as the generic constant for this particular instance.

 

Last edit: Stefan Hamcke 1 hour ago