A couple of previous posts covered sorting CODESYS arrays and using them with pointers. There was never a preface to the topic of arrays. So, this is a stab at clarifying the definition and application of CODESYS arrays.
What and why of arrays?
An array is an set of data. It could be a collection of numbers or a collection of custom data types. One analogy of arrays is a bucket with numbered containers in it., arranged from container 0 or 1 to container x. X being the length of the array. Each container can store some data.
The purpose:
- Collect and organize data of a certain type.
- Data in an array can be called with a common namespace.
- Makes it easier to sort data
- Easier to do math on a set of data points
In the memory layout of a PLC, array elements are usually stored in contiguous ( back-to-back) memory locations. This has some benefits for retrieval when reading from another PLC, HMI or SCADA system.
How are they used?
The basic declaration of an array:
TestArray : ARRAY[1..5] of INT;
It also offers the option of initializing the element values in the declaration:
TestArray : ARRAY[1..5] of INT:= [5,3,2,5,2];
Array lengths can be defined using a variable in the declaration. That is to tie the array size to a variable name instead of a fixed number in the declaration. NOTE: The variable has to be declared as a VAR CONSTANT and can not be altered in runtime. The value of the VAR CONSTANT variable needs to be declared and initialized with a constant value in the programming environment. The IEC 61131-3 standard also makes reference to this point. So, the feature of using a constant in the declaration of array length only serves to centralize the location of initial setup of parameters, if required by the control program.
TestArray : ARRAY[1..UserDefinedVarLength] of INT:= [5,3,2,5,2];
VAR_CONSTANT
UserDefinedVarLength : INT:= 5;
END_VAR
The above as noted in the CODESYS environment:
CODESYS Array declaration in the actual declaration window
CODESYS Arrays can be multi-dimensional. That is, they can have more than one attribute and form a matrix. Multi-dimensional arrays are declared as follows:
MotorData: ARRAY [ 1..2,1..5] OF REAL: [845, 822, 808, 795, 834, 30.2, 29.0, 28.5, 29.5, 30.2 ];
The above array will initialize as follows:
[1,1] := 845;
[1,2] := 822;
[1,3] := 808;
[1,4 := 795;
[1,5] := 834;
[2,1] := 30.2;
[2,2] := 29.0;
[2,3] := 28.5;
[2,4] := 29.5;
[2,5] :=30.2;
…
An example application with a visual representation of the multi-dimensional array above could be as follows:
CODESYS Functions that can be used with Arrays:
There are a number of functions that can be used with arrays. Often times, for organizing and sorting data in arrays, pointers are used. Other than that, the following are some functions and their applications to arrays:
SizeOf
This function returns the number of bytes reserved for an area. If an array is made up of INT datatypes, each element would take up 2 bytes. The total number of bytes would be number of elements times 2. For REAL datatypes it would be a multiplier of 4.
CheckBounds
This is added as an implicit check. It shows up as ‘ POU for Implicit Checks’ when Add Object is selected and check arrays during runtime to determine if any code is attempting to access array elements that are outside the declared range.
NOTE: Sum and arithmetic functions ( ADD, MUL, SUB…etc) usually accept INT, REAL’s and ANY_NUM type inputs. As such, feeding an array to the input of a math function will usually result in an error. Arrays have to be broken out to individual elements to be manipulated and used for number crunching.
To wrap up, a video on arrays in CODESYS from Kurt Braun’s YouTube channel
There are probably a number of functions related to arrays that folks out there use. If you know of one, please share in the comments section below.
Resources: Staying on the topics of arrays, the following articles are on the same topic:
CODESYS Array Sorting in PLC Programs
CODESYS Pointers and Dereferencing
If organizing and handling data in your PLC programs is important to you, check them out.