Array and Enumeration in Structured Text
What is an array:
An array can be defined as an ordered collection of items indexed by contiguous integers. An array is a data structure that can store a number of variables of the same data type in sequence. These similar elements could be of type int, float, double, char, Function Block, etc.
Arrays are used when there is a need to use many variables of the same type. It can be defined as a sequence of objects which are of the same data type. It is used to store a collection of data, and it is more useful to think of an array as a collection of variables of the same type. Arrays can be declared and used. A programmer has to specify the types of elements and the number of elements that are required by an array. This is called a single-dimensional array. The array size should be an integer constant and greater than zero.
One-, two-, and three-dimensional fields (arrays) are supported as elementary data types. Arrays can be defined both in the declaration part of a POU and in the global variable lists.
In the following statement, we declare an array of 5 integers and initialized them with 1, 2, 3, 4, and 5 respectively.
ArrayInt : ARRAY [1..5] OF INT := [1,2,3,4,5];
Look at the syntax ARRAY[lower..upper] OF allows us to define any lower or upper bounds. As compared to C#, our lower bound doesn’t have to be zero. The total number of elements in the array is upper – lower + 1. We could have declared the above statement as the following as well
ArrayInt : ARRAY [2..6] OF INT := [1,2,3,4,5];
In this case, we have accessed first number by by the following way.
firstOne := ArrayInt; // 1
Now we shall describe, how we can declare an array of FUNCTION_BLOCK
Sample function block
FUNCTION_BLOCK MotorAC VAR_INPUT machineName : WSTRING :="Type A"; END_VAR VAR_OUTPUT machineAlarm : BOOL := FALSE; END_VAR VAR maxRotationValue : REAL := 0.0; rotationPerSeconds : INT := 0; myTimer : TON ; startMachine : BOOL := FALSE; END_VAR
We declare and initialize those array of 3 instances in the following way.
ArrayMotorAc : ARRAY [1..3] OF MotorAC := [ (machineName:= "Motor_one"), (machineName:= "Motor_two"), (machineName:= "Motor_three") ];
Initialization can be done in the following way.
FOR index := 1 TO number_of_motor DO