Using Structures to Access Data
Structures
A structure combines various data types (elementary and complex data types, including arrays and structures) to form one unit. You can group the data to suit your process control. You can therefore also transfer parameters as a data unit and not as single elements. The following figure illustrates a structure consisting of an integer, a byte, a character, a floating-point number, and a Boolean value.
A structure can be nested to a maximum of 8 levels (for example, a structure consisting of structures containing arrays).
Creating a Structure
You define structures when you declare data within a DB or in the variable declaration of a logic block.
The following figure illustrates the declaration of a structure (Stack_1) that consists of the following elements: an integer (for saving the amount), a byte (for saving the original data), a character (for saving the control code), a floating-point number (for saving the temperature), and a Boolean memory bit (for terminating the signal).
Assigning Initial Values for a Structure
If you want to assign an initial value to every element of a structure, you specify a value that is valid for the data type and the name of the element. You can, for example, assign the following initial values (to the structure declared in the above figure):
Amount = 100
Original_data = B#(0)
Control_code = 'C'
Temperature = 120
End = False
Saving and Accessing Data in Structures
You access the individual elements of a structure. You can use symbolic addresses (for example, Stack_1.Temperature). You can, however, specify the absolute address at which the element is located (example: if Stack_1 is located in DB20 starting at byte 0, the absolute address for amount is DB20.DBW0 and the address for temperature is DB20.DBD6).
Using Structures as Parameters
You can transfer structures as parameters. If a parameter is declared as STRUCT in the variable declaration, you must transfer a structure with the same components. An element of a structure can, however, also be assigned to a parameter when you call a block providing the element of the structure corresponds to the data type of the parameter.
If you use structures as parameters, both structures (for the formal parameters and the actual parameters) must have the same components, in other words the same data types must be arranged in the same order.