Structures (STRUCT)
Structures are created as objects in the Object Organizer under the register card Data types. They begin with the keyword TYPE and end with END_TYPE.The syntax for structure declarations is as follows:
TYPE <Structurename>:
STRUCT
<Declaration of Variables 1>
.
.
<Declaration of Variables n>
END_STRUCT
END_TYPE
<Structurename> is a type that is recognized throughout the project and can be used like a standard data type. Interlocking structures are allowed. The only restriction is that variables may not be placed at addresses (the AT declaration is not allowed!).
Example for a structure definition named Polygonline:
TYPE Polygonline:
STRUCT
Start:ARRAY [1..2] OF INT;
Point1:ARRAY [1..2] OF INT;
Point2:ARRAY [1..2] OF INT;
Point3:ARRAY [1..2] OF INT;
Point4:ARRAY [1..2] OF INT;
End:ARRAY [1..2] OF INT;
END_STRUCT
END_TYPE
You can gain access to structure components using the following syntax:
<Structure_Name>.<Componentname>
For example, if you have a structure named "Week" that contains a component named "Monday", you can get to it by doing the following: Week.Monday
Notice | |
Different structures Due to different alignments, structures and arrays may have different configurations and sizes on different hardware platforms (e.g. CX1000 and CX90xx). |
Example for a structure definition with name ST_ALIGN_SAMPLE:
TYPE ST_ALIGN_SAMPLE:
STRUCT
_diField1 : DINT;
_byField1 : BYTE;
_iField : INT;
_byField2 : BYTE;
_diField2 : DINT;
_pField : POINTER TO BYTE;
END_STRUCT
END_TYPE
On CX90xx (RISC) platforms the member components of structure ST_ALIGN_SAMPLE have the following sizes and offsets:
_diField1 (DINT), Offset = 0 (16#0), Size = 4
_byField1 (BYTE), Offset = 4 (16#4), Size = 1
_iField (INT), Offset = 6 (16#6), Size = 2
_byField2 (BYTE), Offset = 8 (16#8), Size = 1
_diField2 (DINT), Offset = 12 (16#C), Size = 4
_pField (POINTER TO BYTE), Offset = 16 (16#10), Size = 4
Overall size through natural alignment with Pack(4) and so-called padding bytes: 20
On CX10xx platforms the member components of structure ST_ALIGN_SAMPLE have the following sizes and offsets:
_diField1 (DINT), Offset = 0 (16#0), Size = 4
_byField1 (BYTE), Offset = 4 (16#4), Size = 1
_iField (INT), Offset = 5 (16#5), Size = 2
_byField2 (BYTE), Offset = 7 (16#7), Size = 1
_diField2 (DINT), Offset = 8 (16#8), Size = 4
_pField (POINTER TO BYTE), Offset = 12 (16#C), Size = 4
Overall size: 16
Display of structure ST_ALIGN_SAMPLE for CX90xx platforms (RISC) with representation of the padding bytes:
TYPE ST_ALIGN_SAMPLE:
STRUCT
_diField1 : DINT;
_byField1 : BYTE;
_byPadding : BYTE;
_iField : INT;
_byField2 : BYTE;
_a_byPadding : ARRAY[0..2] OF BYTE;
_diField2 : DINT;
_pField : POINTER TO BYTE;
END_STRUCT
END_TYPE