Using Arrays in IEC PLC

Products: ACR9600,9630,9640

Note:

  • Uses 16bit integers to represent array subscripts for performance reasons. Arrays should not be declared in a way to use subscripts exceeding 16bit address limits, as this would lead to undefined behavior.

  • The upper and lower bounds must always be specified as integers in the declaration. It is not possible to assign/change array length using a variable at run-time. 

  • Expression are not allowed in the [index] assignment of any array. For example, x2[MyIndex+1]:=10;  would result in an error. Instead, evaluate the expression prior to using in the index field:    MyIndex:=MyIndex+1; x2[MyIndex]:=10;

     

     

     

Example1

The following declares an array of five integers and assigns initial values:
VAR
x1: ARRAY[0..4] of INT := [1,2,3,4,5];
END_VAR

 

Example2

TYPE
UserData : STRUCT
 ID : UDINT;
 Score : REAL;
END_STRUCT;
END_TYPE

VAR

TopValues : Array[1..10] of real;  (* array of 10 real values *)
TopID : Array[1..10] of UDINT;  (* array of 10 integers*)
tempData1 : UserData;  (* structure of one int and 1 real *)
tempData2 : UserData; (* structure of one int and 1 real *)
TopTen : ARRAY[1..10] of UserData; (* array of structures of one int and 1 real *)

END_VAR

for index:=1 to 10 by 1 do
  (*resets the top ten back*)
  TopTen[index].id:=index;
  TopTen[index].score:=100000.0;
end_for;

for index:=1 to 100 by 1 do
 readparm.ParameterNumber:=39300+index;
 ReadParm( Enable:=true | val:=value);
 uID :=index;

 
 if val >0.1 then
  KeepChecking:=true;
  IsTopTen:=False;
  index2:=1;
  while KeepChecking and Index2<11 do
   if val<TopTen[index2].score then
    keepchecking:=false;
    IsTopTen:=true;
    tempData1.score:=val;
    tempData1.id:=uid;
   end_if;
   index2:=index2+1;
  end_while;
  if IsTopTen then
   index2:=index2-1;
   tempdata2:=TopTen[index2];
   TopTen[index2]:=tempdata1;
   index2:=index2+1;
   for index3:=index2 to 10 by 1 do
    tempdata1:=TopTen[index3];
    TopTen[index3]:=tempdata2;
    tempdata2:=tempdata1;
   end_for;
  end_if;
 end_if;
  
 
  
END_for; 

Example3

VAR

X1 : ARRAY[0..4] of real:= [0.0,1.0,1.0,0.0,0.0];
Y1: ARRAY[0..4] of real:= [0.0,0.0,1.0,1.0,0.0];

X4 : ARRAY[0..5] of real:= [0.0,0.0,1.0,0.0,-1.0,0.0];
Y4: ARRAY[0..5] of real:= [0.0,-1.0,0.0,1.0,0.0,-1.0];

X2 : ARRAY[0..8] of real:= [0.0,6.0,9.0,9.0,6.0,3.0,0.0,0.0,3.0];
Y2 : ARRAY[0..8] of real:= [0.0,0.0,3.0,6.0,9.0,9.0,6.0,3.0,0.0];

Xs : ARRAY[0..16] of real;
Ys : ARRAY[0..16] of real;

State : INT;
index : INT:=0;
MAXINDEX : INT:=16;

PathType : DINT ;
HMIRadius : REAL;

END_VAR

 

  CASE PathType OF
   1:(*Square;*)
   maxindex:=4;
   
   FOR Index:=0 to MaxIndex Do
    Xs[Index]:=X1[index]*HMIRadius;
    Ys[Index]:=Y1[index]*HMIRadius;
   END_FOR;
   2:(*Hex;*)
   maxindex:=8;
   FOR Index:=0 to MaxIndex Do
    Xs[Index]:=X2[index];
    Ys[Index]:=Y2[index];
   END_FOR;

   maxindex:=5;
   FOR Index:=0 to MaxIndex Do
    Xs[Index]:=X4[index]*HMIRadius;
    Ys[Index]:=Y4[index]*HMIRadius;
   END_FOR;
  END_CASE;

Example4

VAR

INDEX : INT;
 readP : ACR_ReadRealparameter;

parm :array[0..519] of real;

END_VAR

 

 

for index:=0 to 519 by 1 do
readP.ParameterNumber:=int_TO_udint(index);
readP( Enable:=true);
 parm[index]:=READP.VALUE;
end_for;