24.01.2015 Views

PLC Programming

  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Appendix C: Data types in CoDeSys<br />

DATE_AND_TIME#1996-05-06-15:36:30<br />

dt#1972-03-29-00:00:00<br />

10.15 Defined data types<br />

ARRAY<br />

One-, two-, and three-dimensional fields (arrays) are supported as elementary data types. Arrays can<br />

be defined both in the declaration part of a POU and in the global variable lists.<br />

Syntax:<br />

:ARRAY [..,..] OF .<br />

ll1, ll2, ll3identify the lower limit of the field range; ul1, ul2 and ul3 identify the upper limit. The range<br />

values must be integers.<br />

Example:<br />

Card_game: ARRAY [1..13, 1..4] OF INT;<br />

Initializing Arrays:<br />

Example for complete initialization of an array:<br />

arr1 : ARRAY [1..5] OF INT := 1,2,3,4,5;<br />

arr2 : ARRAY [1..2,3..4] OF INT := 1,3(7); (* short for 1,7,7,7 *)<br />

arr3 : ARRAY [1..2,2..3,3..4] OF INT := 2(0),4(4),2,3;<br />

(* short for 0,0,4,4,4,4,2,3 *)<br />

Example of the initialization of an array of a structure:<br />

TYPE STRUCT1<br />

STRUCT<br />

p1:int;<br />

p2:int;<br />

p3:dword;<br />

END_STRUCT<br />

ARRAY[1..3] OF STRUCT1:= (p1:=1,p2:=10,p3:=4723),(p1:=2,p2:=0,p3:=299),<br />

(p1:=14,p2:=5,p3:=112);<br />

Example of the partial initialization of an Array:<br />

arr1 : ARRAY [1..10] OF INT := 1,2;<br />

Elements to which no value is pre-assigned are initialized with the default initial value of the basic<br />

type. In the example above, the elements anarray[6] to anarray[10] are therefore initialized with 0.<br />

Array components are accessed in a two-dimensional array using the following syntax:<br />

[Index1,Index2]<br />

Example:<br />

Card_game [9,2]<br />

Note:<br />

If you define a function in your project with the name CheckBounds, you can use it to check for<br />

range overflows in your project (see chapter 'What is what in CoDeSys', 'Components of a project',<br />

'Function')<br />

Function Checkbounds<br />

If you define a function in your project with the name CheckBounds, you can automatically check for<br />

out-of-range errors in arrays. The name of the function is fixed and can only have this designation.<br />

Example for the function CheckBounds:<br />

CoDeSys V2.3 10-33

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!