Accessing the structured data type and structure elements

If you want to declare a variable based on a structured data type (this is declaring a structured variable), enter the name of the structured data type (related to the example: Range or RangeS) as data type within the declaration of variables. You may initialize the structure elements within this declaration.

If you want to use structured variables in assignments, enter the name of the variable (e.g. VRange) and the name of the structure element (e.g. min) separated by the character . (e.g. VRange.min). You can assign a structured variable to another structured variable, if both data types are identical regarding the following:

  • number/order, name and data type of their structure elements

  • statement {KeepElementOrder}

The other data of the structured data types, such as the →initial values of the structure elements, a description/comment of the data types and even the names of the data types, may differ.

Examples
TYPE
Range : STRUCT
min : INT;
max : INT := 300;
END_STRUCT;
RangeS : STRUCT
signal : BOOL;
scaleMin : DINT;
scaleMax : DINT;
END_STRUCT;
struct1 : STRUCT
elem1 : BYTE;
elem2 : WORD;
END_STRUCT;
struct2 : STRUCT
elem1 : BYTE;
elem2 : WORD := 4;
END_STRUCT;
END_TYPE
 
FUNCTION_BLOCK UseStructTypes
VAR
var1 : INT;
VRange : Range;
(* declaring variable 'VRange' of structured data type 'Range' *)
VRangeS : RangeS := (scaleMin := -5, scaleMax := 5);
(* structure initialization for variable 'VRangeS' of structured data type 'RangeS': initializing the structure elements 'scaleMin' and 'scaleMax' *)
 
VarA : struct1; (* more declarations of variables, each with a different structured data type *)
VarB : struct2;
END_VAR
VRange.min := 2;
(* assigning value '2' to structure element 'min' of variable 'VRange' *)
var1 := VRange.max;
(* assigning the value of the structure element 'max' of variable 'VRange' to variable 'var1' *)
VarA := VarB; (* correct assignment because the structure elements of the structured data typs differ only regarding the intial values *)
VRangeS := VRange; (* faulty assignment because the structure elements of the structured data typs differ regarding number and name *)
END_FUNCTION_BLOCK