3

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]
darnbar
  • 33
  • 1
  • 1
  • 5

2 Answers2

4

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;
mrsargent
  • 1,824
  • 3
  • 15
  • 34
  • Thanks for your reply Mrsargent that works a charm. I was also looking at using real numbers such as '(0.1,0.2,0.3)' in an array is there a method of implementing that also? – darnbar Jan 06 '16 at 14:55
  • For real numbers you can just setup another array but make of type real. Then just do `RealArray[i]:=i*0.1;` in the for loop. – mrsargent Jan 06 '16 at 15:03
  • If you found that answer helpful would you make this as the accepted solution to help others find this solution if they have a similar problem? – mrsargent Jan 06 '16 at 15:07
  • Many thanks for your help MrSargent, that works a treat,for some of the generated real numbers I see some of the values displayed as 1.30007 I presume that has to do with the PLC scan time? – darnbar Jan 06 '16 at 15:20
  • 1
    @darnbar it is not a plc scan time issue. It is a code issue and how the REAL value is converted and calculated. Try this code. Don't think you will have this problem. `RealArray[i]:= INT_TO_REAL(i)/10.0;` – mrsargent Jan 06 '16 at 16:22
3

You can use the ()-shorthand if you want to initialize an array or a part of the array with the same values.

ARRAY [1..100] OF INT :=[4(1),3(2),2(3),4]; //Equivalent [1,1,1,1,2,2,2,3,3,4]

In Codesys V3 or TwinCat 3 you can use the FB_init Method to write initialization code. The FB_init Methods of all declared FB instances will be called at startup before the normal cyclic task execution starts.

You can use it like this:

METHOD FB_Init : BOOL
VAR_INPUT
  bInitRetains : BOOL; // if TRUE, the retain variables are initialized (warm start / cold start)
  bInCopyCode : BOOL;  // if TRUE, the instance afterwards gets moved into the copy code (online change)
END_VAR
VAR
  unCount : UINT;
END_VAR

FOR unCount := 1 TO 100 DO
   arrnInitExample[unCount]:= unCount;
END_FOR

Note: The first two boolean inputs of the FB_init Method have to be always the same. But you could also add additional Input variables afterwards. Then you can pass custom parameters at declaration of the FB instance to the method.

Felix Keil
  • 2,102
  • 1
  • 20
  • 23