Structure

A structure is a user-defined data type and combines several variables with any data type into a logical unit. The variables declared within a structure are referred to as components.

You declare a structure in a DUT object that you create in the project using the command Add > DUT in the context menu of the PLC project tree.

Syntax:

TYPE <structure name> :
STRUCT
    (<variable declaration optional with initialization>)+
END_STRUCT
END_TYPE

The <structure name> is an identifier that is valid in the entire project, allowing you to use it like a standard data type. In addition, you can declare as many variables as you like (at least one), which are optionally supplemented by an initialization.

Furthermore, you can nest structures. This means that you declare a structure component with an existing structure type. The only restriction is that you may not assign any addresses to the variables (structure components) (the AT declaration is not permitted here).

Sample: structure declaration

TYPE ST_POLYGONLINE :
STRUCT
    aStart : ARRAY[1..2] OF INT := [-99, -99];
    aPoint1 : ARRAY[1..2] OF INT;
    aPoint2 : ARRAY[1..2] OF INT;
    aPoint3 : ARRAY[1..2] OF INT;
    aPoint4 : ARRAY[1..2] OF INT;
    aEnd : ARRAY[1..2] OF INT := [99, 99];
END_STRUCT
END_TYPE
Structure 1:

8-byte alignment

An 8-byte alignment was introduced with TwinCAT 3. Make sure the alignment is correct if data are exchanged as a complete memory block with other controllers or software components (see Alignment).

Extending a type declaration

Starting from an existing structure, a further structure is declared. In addition to its own components, the extended structure has the same structure components as the basic structure.

Syntax

TYPE <structure name> EXTENDS <basis structure> :
STRUCT
    (<variable declaration optional with initialization>)+
END_STRUCT
END_TYPE

Sample: structure declaration

TYPE ST_PENTAGON EXTENDS ST_POLYGONLINE :
STRUCT
    aPoint5 : ARRAY[1..2] OF INT;
END_STRUCT
END_TYPE

Declaration and initialization of structures

Sample

PROGRAM Line
VAR
    stPolygon  : ST_POLYGONLINE := (aStart:=[1,1], aPoint1:=[5,2], aPoint2:=[7,3], aPoint3:=[8,5], aiPoint4:=[5,7], aEnd:=[1,1]);
    stPentagon : ST_PENTAGON := (aStart:=[0,0], aPoint1:=[1,1], aPoint2:=[2,2], aPoint3:=[3,3], aPoint4:=[4,4], aPoint5:=[5,5], aEnd:=[0,0]);
END_VAR

You cannot use initializations with variables. For an example of how to initialize an array of a structure, see the help page for data type ARRAY.

See also:

Accessing a structure component

You access a structure component according to the following syntax:

<variable name>.<component name>

Sample

PROGRAM Polygon
VAR
    stPolygon : ST_POLYGONLINE := := (aStart:=[1,1], aPoint1:=[5,2], aPoint2:=[7,3], aPoint3:=[8,5], aiPoint4:=[5,7], aEnd:=[1,1]);
    nPoint    : INT;
END_VAR
// Assigns 5 to nPoint
nPoint := stPolygon.aPoint1[1];

Ergebnis: nPoint = 5

Symbolic bit access in structure variables

You can declare a structure with variables of the data type BIT in order to combine individual bits into a logical unit. You can then address the individual bits symbolically via a name (instead of via the bit index).

Syntax declaration:

TYPE <structure name> :
STRUCT
    ( <bit name> : BIT; )+
END_STRUCT
END_TYPE

Bit access syntax:

<Structure name>.<Bit name>

Sample:

Structure declaration

TYPE ST_CONTROL :
STRUCT
    bitOperationEnabled  : BIT;
    bitSwitchOnActive    : BIT;
    bitEnableOperation   : BIT;
    bioterror            : BIT;
    bitVoltageEnabled    : BIT;
    bitQuickStop         : BIT;
    bitSwitchOnLocked    : BIT;
    bitWarning           : BIT;
END_STRUCT
END_TYPE

Bit access

FUNCTION_BLOCK FB_Controller
VAR_INPUT
    bStart : BOOL;
END_VAR
VAR_OUTPUT
END_VAR
VAR
    stControl : ST_CONTROL;
END_VAR

IF bStart = TRUE THEN
// Symbolic bit access
stControl.bitEnableOperation := TRUE;
END_IF
PROGRAM MAIN 
VAR
fbController : FB_Controller;
END_VAR
fbController(); 
fbController.bStart := TRUE;
Structure 2:

References and pointers to BIT variables are invalid declarations, as are array components with the base type BIT.

See also: