PLC

Structure

A structure combines several variables with any data types into one logical unit. The variables declared within a structure are referred to as components.

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

Syntax:

TYPE <Structure name>:
STRUCT
    <Variable declaration 1>
    ...
    <Variable declaration n>
END_STRUCT
END_TYPE

<Structure name> is a type that TwinCAT recognizes throughout the entire project, and which you can use as a standard data type.

You can also use nested structures. The only limitation is that you cannot assign variables to addresses (the AT declaration is not permitted!).

Example: "Polygonline" structure definition

TYPE ST_Polygonline:
STRUCT
    aStartPoint : ARRAY [1..2] OF INT;
    aPoint1     : ARRAY [1..2] OF INT;
    aPoint2     : ARRAY [1..2] OF INT;
    aPoint3     : ARRAY [1..2] OF INT;
    aPoint4     : ARRAY [1..2] OF INT;
    aEndPoint   : ARRAY [1..2] OF INT;
END_STRUCT
END_TYPE

See also:

Object DUT
EQ
 

Initialization of structures:

Example:

stPoly1 : ST_Polygonline := (aStartPoint:=[3,3],aPoint1:=[5,2], aPoint2:=[7,3],aPoint3:=[8,5],aPoint4:=[5,7],aEndPoint:=[3,5]);

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:

 
 

Access to structure components

You access a structure component according to the following syntax:

<structure name>.<component name>

You can therefore access the component aStartPoint for the above example of the structure ST_Polygonline with stPoly1.aStartPoint.

See also:

ARRAY
 
 

Bit access in structures

Bit is a special data type, which can only be defined in structures. It occupied 1 bit of memory and enables individual bits of a structure to be addressed via a name.

TYPE <strukturname>:
STRUCT
    <Bitname bit1> : BIT;
    <Bitname bit2> : BIT;
    <Bitname bit3> : BIT;
    ...
    <Bitname bitn> : BIT;
END_STRUCT
END_TYPE

You can access the BIT structure component using the following syntax:

<structure name>.<bit name>

 
You cannot use references and pointers to BIT variables. Furthermore, you cannot use BIT variables in data fields.

See also:

BIT