Search code examples
arrayscodesys

Creat an array in CoDeSys with changeable size


I am working on a moving average algorithm to analyze a sensor values and the values are stored in an Array. BUT, the length of Array is variabla (depends on speed of one motor).

how can I Creat an array in CoDeSys with changeable size. It's wrong to define Array so :

Name: ARRAY[1...SpeedValue] OF INT ;


Solution

  • I am sorry to tell you that there is no changeable size for arrays in Codesys V2/V3. The general explanation is that there is no Dynamic Memory Allocation available in a PLC because Dynamic Memory Allocation is considered to be too unreliable.

    Your only choice is to define an array with a constant ARRAY[1..N_MAX_SPEED_VALUE] and just use the array until SpeedValue

    VAR
        arrnValues          : ARRAY[1..N_MAX_SPEED_VALUE] OF INT;
    END_VAR
    VAR CONSTANT
        N_MAX_SPEED_VALUE   : INT := 100; (*Max Array Size*)
    END_VAR
    

    For myself I am really bugged by this limitation. I already requested a feature many times, to define arrays like ARRAY[*], have keywords for start and end and define the actual start and end size when instantiating. This has nothing todo with dynamic memory allocation, because size is defined at compile time.