how to create an Array in ST (TC3)
Learn more at Part 4 - Data types & arrays (IEC 61131-3)
1) Example
PROGRAM MAIN
VAR
// Create an Array
aOneArray : ARRAY[1..5] OF INT := [10,20,30,40,50];
// Create OneInt
nOneInt : INT;
END_VAR
-----------------------------------------------------------------------
//Change the value of the first element
aOneArray[1] := 42;
//Access the elements of the array and add them
nOneInt := aOneArray[1] + aoNEaRRAY[4]
1.1) Example - Working with Arrays
In this example we have declared an array with five elements, and in the declaration set five values.
We have also declared an integer with the name nOneInt.
We will just do some simple calculations on the array and store the result in the integer.
First we change the value of the first element, so the value 10 is changed to 42
Next we assign the variable nOneInt the values of the first position in the array plus the value of the fourth position in the array. This will add the values of the two positions in the array, and store it in the integer nOneInt.
2) Documentation
2.1) How to declare an Array
An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier
You declare an array in this way, we have created an array that stores 5 integers.
In this example we can see that all values in the array are initialized with 0 just as any other integer if we haven't assigned a value to it.
We can initialize the array with other values in the declaration.
You can assign a new value to any element in the array by simply writing the name of the array brackets, the position of the element in the array and then the value it should have instead.
So here we have replaced the value 20 in the position 2 in the array to the value 133.
Compared to most other programming languages, an array in IEC 61131-3 can be declared to start and end at basically any position.
In the first example we had an array starting at position 1 and ending at position 5.
We can start at position 0 as well, which is more traditional.
We might also start at -1 and end at 1, so this array has three values.
2.2) How to declare an Multi-Dimensional Arrays
Arrays don't have to be one dimensional, you can create multi-dimensional arrays. In this example we have a 2 dimensional array, and declared in two different ways.
-
In alternative #1 we declare a two-dimensional array with two rows and three columns
-
In alternative #2 we also declare a two-dimensional array with two rows and three columns.
Accessing the arrays is done slightly different depending on how you declare them.
For example look how we access the value in the first row and first column using alternative #1,
compared to alternative #2
Compared to for example C++ the declaration for 2D arrays doesn't look the same for neither of these two alternatives
but ACCESSING the array is similar in alternative #2 to C++
Also note that the online view with the TwinCAT IDE looks slightly different depending on how you have declared your 2 dimensional array.