A structure is a user-defined data type, which combines multiple variables of any data type into a logical unit. The variables declared within a structure are called members.
You make the type declaration of a structure in a “DUT” object which you create in the “Project Add Object DUT” menu or in the context menu of an application.
Syntax
TYPE <structure name> : STRUCT ( <variable declaration optional with initialization> )+ END_STRUCT END_TYPE
<structure name>
is an identifier which is valid in the entire project so that you can use it like
a standard data type. Moreover, you can declare any number of variables (at least
one) which are supplemented optionally by an initialization.
Structures can also be nested. This means that you declare a structure member with an existing structure type. Then the only restriction is that you must not assign any address to the variable (structure member). (The AT declaration is not permitted here.)
Example
Type declaration
TYPE S_POLYGONLINE : STRUCT aiStart : ARRAY[1..2] OF INT := [-99, -99]; aiPoint1 : ARRAY[1..2] OF INT; aiPoint2 : ARRAY[1..2] OF INT; aiPoint3 : ARRAY[1..2] OF INT; aiPoint4 : ARRAY[1..2] OF INT; aiEnd : ARRAY[1..2] OF INT := [99, 99]; END_STRUCT END_TYPE
Extension of a type declaration
An additional structure is declared from an existing structure. In addition to its own members, the extended structure also has the same structure members as the base structure.
Syntax
TYPE <structure name> EXTENDS <basis structure> : STRUCT ( <variable declaration optional with initialization> )+ END_STRUCT END_TYPE
Example
Type declaration
TYPE S_PENTAGON EXTENDS S_POLYGONLINE : STRUCT aiPoint5 : ARRAY[1..2] OF INT; END_STRUCT END_TYPE
Declaration and initialization of structure variables
Example
PROGRAM progLine VAR sPolygon : S_POLYGONLINE := (aiStart:=[1,1], aiPoint1:=[5,2], aiPoint2:=[7,3], aiPoint3:=[8,5], aiPoint4:=[5,7], aiEnd:=[1,1]); sPentagon : S_PENTAGON := (aiStart:=[0,0], aiPoint1:=[1,1], aiPoint2:=[2,2], aiPoint3:=[3,3], aiPoint4:=[4,4], aiPoint5:=[5,5], aiEnd:=[0,0]); END_VAR
You must not permitted to use initializations with variables. For an example of initializing
an array of a structure, see the help page for the data type ARRAY
.
Access to a structure member
You access structure members with the following syntax:
<variable name> . <component name>
Example
PROGRAM prog_Polygon VAR sPolygon : S_POLYGONLINE := (aiStart:=[1,1], aiPoint1:=[5,2], aiPoint2:=[7,3], aiPoint3:=[8,5], aiPoint4:=[5,7], aiEnd:=[1,1]); iPoint: INT; END_VAR // Assigns 5 to aiPoint iPoint := sPolygon.aiPoint1[1];
Result: iPoint = 5
Symbolic bit access in structure variables
You can declare a structure with variables of data type BIT
to combine individual bits into a logical unit. Then you can symbolically address
individual bits by a name (instead of by a bit index).
Syntax declaration
TYPE <structure name> : STRUCT ( <bit name> : BIT; )+ END_STRUCT END_TYPE
Syntax of bit access
<structure name> . <bit name>
Example
Type declaration
TYPE S_CONTROL : STRUCT bitOperationEnabled : BIT; bitSwitchOnActive : BIT; bitEnableOperation : BIT; bitError : BIT; bitVoltageEnabled : BIT; bitQuickStop : BIT; bitSwitchOnLocked : BIT; bitWarning : BIT; END_STRUCT END_TYPE
Bit access
FUNCTION_BLOCK FB_Controller VAR_INPUT xStart : BOOL; END_VAR VAR_OUTPUT END_VAR VAR ControlDriveA : S_CONTROL; END_VAR IF xStart = TRUE THEN // Symbolic bit access ControlDriveA.bitEnableOperation := TRUE; END_IF PROGRAM PLC_PRG VAR fbController : FB_Controller; END_VAR fbController(); fbController.xStart := TRUE;
References and pointers to BIT
variables are invalid declarations, as well as array elements with base type BIT
.
See also