Declaration of a structured data type in ST

Syntax
TYPE
{KeepElementOrder} (* optional specification per structured data type; affects the sorting of the structure elements within the generated C-code *)
name_1: STRUCT
name_e1 : type := initial-value; (* Partial addresses are also possible for the structure element. *)
name_e2 : REF_TO type; (* Partial addresses are also possible for the structure element. *)
name_e3 : FB-type; (* Partial addresses are also possible for the structure element. *)
name_e4 : interface; (* Partial addresses are also possible for the structure element. *)
...
name_en : type := initial-value;
END_STRUCT;
{KeepElementOrder}
name_2: STRUCT
name_e1 : type := initial-value;
...
END_STRUCT;
...
{KeepElementOrder}
name_n: STRUCT
...
END_STRUCT;
END_TYPE

Meaning

Declaration of one or more →structured data types (name_1, name_2 etc.) with a collection of →named elements (the structure elements name_e1, name_e2 etc.) of specified →data types (type), →function blocks (FB-type) or →interface ( interface )

STRUCT and END_STRUCT are →keywords for the declaration of the structured data type. This declaration is possible within the declaration of a user-defined data type (TYPE ... END_TYPE).
name_1, name_2 etc. and name_e1, name_e2 etc. must be →IEC-identifiers.

A data type is possible as a type for a structure element. See "Supported data types" to learn which data types are supported for the declaration. Moreover, a →reference (REF_TO), an existing function block (created in →ST, →FBD or →LD) and an interface are possible as types for the structure element.

Use the optional →initial value := initial-value to assign a value to the structure element. The initial value (on the right side of the assignment operator ":=") may be a →constant expresseion as listed under "Initialization of variables in ST". Condition: The initial value must be of the same data type (type) or of a data type that can be converted into this data type due to the implicit conversion. If you do not enter any initial value, the default initial value of the data type is applied.

The optional statement {KeepElementOrder} sorts the structure elements in the generated C-code as they are sorted within the ST-code. Just insert the statement in front of the affected structure data type (see the following examples).

Current restrictions

Good to know

images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/lightbulb.svg See "Accessing the structured data type and structure elements", if you need information about accessing the structure elements.

images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/lightbulb.svg Moreover, logi.CAD 3 allows you to define a not yet fully specified location for the declaration. See "Declaration of a language element with partial address in ST" for details.

images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/lightbulb.svg Moreover, logi.CAD 3 allows you to specify additional data for the declaration . See " Defining description, comment, JSON string or type for variables or data types " for details.

Examples without the specification affecting the sorting of the structure elements
TYPE
Range : STRUCT
min : INT; (* no initialization *)
max : INT := 300; (* initialization *)
END_STRUCT;
RangeS : STRUCT
signal : BOOL;
scaleMin : DINT;  
scaleMax : DINT;
END_STRUCT;
RangeD : STRUCT
signal : BOOL;
scaleMin : ARRAY [1..3] OF Data_MIN; (* 'Data_MIN' is a user-defined data type. *)
scaleMax : Data_MAX; (* 'Data_MAX' is a user-defined data type. *)
myRef: REF_TO INT; (* 'myRef' is a reference to 'INT'. *)
END_STRUCT;
Cooler : STRUCT
Temp : INT;
Cooling : TOF; (* function block 'TOF' as structure element *)
END_STRUCT;
Data_MIN : INT;
Data_MAX : ARRAY [1..3] OF INT;
END_TYPE

Without the statement {KeepElementOrder}, the structure elements are automatically sorted within the generated C-code. For this automatic sorting, the data type is used at first (starting with BOOL, continuing with rising data type size, concluding with structure and array data types). In case of a same data type, sorting is done according to the element name (with alphanumeric order).

Example with the specification affecting the sorting of the structure elements
TYPE
{KeepElementOrder}
Range1 : STRUCT
min: INT;
scaling : BOOL;
max: INT;
noScaling : Range2; (* 'Range2' is a structure data type as well. *)
END_STRUCT;
END_TYPE

Without statement {KeepElementOrder}, the above structure elements would be automatically sorted as follows:

  1. scaling : BOOL;

  2. max: INT;

  3. min: INT;

  4. noScaling : Range2;

If an element is using another structure data type and you want to have the elements of this structure data type sorted as declared, you have to insert the statement {KeepElementOrder} for this other struture data type as well. Regarding the example, the statement {KeepElementOrder} would have to be inserted in front of the declaration of the structure data type Range2, too.

Example for the initialization of a structure element where a different structure data type is used
TYPE
myStruct : STRUCT
elem : int;
myStruct : myStruct2 := (elem := 2);
END_STRUCT;
 
myStruct2 : STRUCT
elem : int;
END_STRUCT;
END_TYPE