Search code examples
arrayscodesysstructured-text

Defining Arrays in Codesys


I'm using Codesys to create an array.

The example below is what I'm using to create an array for 5 values

ARRAY [1..5] OF INT := [1,2,3,4,5];

That's fine for an array of 5 but if I needed an array for 100 values?

Is there a quicker way of doing an array for 100 values instead of typing each value out separately as shown in the example below.

ARRAY [1..100] OF INT :=[1,2,3,4,5,6,7 to 100]

Solution

  • Well if you just want to do it up to 100 elements and you want the elements of the array to proceed in order you can use this code to initialize your array on the first plc scan instead of initializing in the array declaration.

    VAR
       SomeArray: ARRAY[1..100] OF INT;
       i:INT:=0;
       isInitialized:BOOL:=false;
    END_VAR
    
    IF NOT isInitialized THEN
        FOR i:=1 TO 100 BY 1 DO
             SomeArray[i]:=i;
        END_FOR;
        isInitialized:=TRUE;
    END_IF;