Declaration of a data type with named values (enums) in ST
logi.CAD 3 offers the enum-editor as an alternative to display and edit data types with named values (also identified as →enums). In this case, you do not need to know the following syntax.
TYPE
name_1 : data-type (name_e1 := value_1, name_e2 := value_2, ..., name_en := value_n) := initial-value;
name_2 : data-type (name_e1 := value_1, name_e2 := value_2, ..., name_en := value_n) := initial-value;
...
END_TYPE
Meaning |
Declaration of one or more →data types with named values
(also identified as
→enums)
, name_1, name_e1 must be →IEC-identifiers. This declaration is possible within the declaration of a user-defined data type (TYPE ... END_TYPE). Use the optional →initial value to assign a value to the data type. This value might be a constant expression as well, such as a named element (see example Colors1) or a literal matching the data type (see example Colors2). If there is no initial value for the data type, the first element is applied as the initial value (see example TrafficLight). Deviations from IEC-standard You always have to specify the base type in logi.CAD 3. This is in contrast to the IEC-standard where the base type is optional. It is possible to use a data type with named values in all places where it is possible to use the base type. Example: It is possible to use the data type TrafficLight of the following example as input data type for the ADD block. |
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.
TYPE
TrafficLight : INT
(Red := 1, Yellow:= 2, Green := 3);
(* 'Red' is used as initial value for 'TrafficLight'. *)
Colors1 : DWORD
(Red := 16#00FF0000, Green := 16#0000FF00, Blue := 16#000000FF) := Colors1#Green;
(* 'Green' is used as initial value for 'Colors1'. *)
Colors2 : DWORD
(Orange := 16#00FF7F00, Pink := 16#00FF33FF, Red := Colors1#Red) := 16#00990000;
(* '16#00990000' is used as initial value for 'Colors2'. *)
END_TYPE
Using the named values
Use the following syntax in order to uniquely address a named value within an editor (e.g. within an ST-editor): name_1#name_e1 (with name_1 being the name of the enum and name_e1 being the name of the element) – example: TrafficLight#Red
logi.cals recommends to address named values always with the unique name. This will help to avoid later conflicts when modifying the application – because e.g. a named value is also used in a newly installed library.
However, if logi.CAD 3 is able to derive the named value uniquely from the context, it is possible to only enter the named value. See examples Yellow and Green in the following example (for this example, a declaration as specified under " Declaration of a data type with named values (enums) in ST " is required).
VAR
Var1 : TrafficLight;
Var2 : STRING[20];
END_VAR
IF Var1 = TrafficLight#Red THEN (* OK because of the unique access *)
Var2 := 'STOP';
END_IF;
IF Var1 = Yellow THEN (* OK because 'Var1' is the context for 'Yellow'; but recommended is the usage of the unique name 'TrafficLight#Yellow' *)
Var2 := 'BEWARE';
END_IF;
IF Var1 = Green THEN (* OK although 'Green' is not unique but 'Var1' is the context; but recommended is the usage of the unique name 'TrafficLight#Green' *)
Var2 := 'WALK';
END_IF;
IF Var1 = 16#00000000 THEN (* OK because other values than the named values are allowed *)
Var2 := 'No traffic light';
END_IF;
Depending on your logi.CAD 3 c onfiguration (i.e., the start option lc3.expensive.enum.resolving.enable ), you might be able to specify a named value without a unique name – even though the context is not known. However, the named value must be unique. Please mind that the performance might decrease when saving, importing and opening objects and/or projects. If you are interested in changing the current configuration, contact your system integrator and clarify the possible effects of this start option .