An enumeration is a user-defined type that is made up of a number of comma-delimited string constants. These constants are referred to as enumeration values. Enumeration values are identifiers for global constants in the project.
Enumeration values are recognized globally in all areas of the project even if they are declared within a POU.
An enumeration is created in a project as a DUT object via the
dialog box.TYPE <enum identifier>: (<enum_0>|:=<value>,<enum_1>|:=<value>, ...,<enum _n>|:=<value>)|<base data type>|:=<default value>;
END_TYPE
The following elements can additionally be used:
Initialization of single enumeration values
Definition of a <base data type>
(refer to the paragraph Second Extension to the IEC 61131-3 Standard)
Definitions of a default value for initializing all components
<variable identifier> : <enum identifier>| := <initialization value>
A variable of type <enum identifier>
can take the enumeration values <enum_..>
.
If... |
Then ... |
---|---|
|
The variable is initialized with the value of the first enumeration component. This applies unless a component is initialized with the value 0 in the enumeration declaration. In this case, the variable is also initialized with the value 0. |
Declaration of the Enumeration
TYPE TRAFFIC_SIGNAL: (red, yellow, green:=10);
(* The initial value for each of the colors is red 0, yellow 1, green 10 *)
END_TYPE
In this declaration, the first two components get default initialization values:
red
= 0
, yellow
= 1
The initialization value of the third component is explicitly defined:
green
= 10
Use in the Implementation
TRAFFIC_SIGNAL1 : TRAFFIC_SIGNAL;
TRAFFIC_SIGNAL1:=10; (* The value of the TRAFFIC_SIGNAL1 is "green" *)
FOR i:= red TO green DO
i := i + 1;
END_FOR;
Declaration of the Enumeration
TYPE COLOR :
(
red,
yellow,
green
) := green;
END_TYPE
Use in the Implementation
c1 : COLOR;
c2 : COLOR := yellow;
In this case, the variable c1
is initialized with the value green
. The initialization value yellow
is defined for c2
.
You can use the type name of enumerations (as a scope operator) to disambiguate the access to an enumeration constant.
Therefore, it is possible to use the same constant in different enumerations.
Example
Definition of two enumerations with same-named components
TYPE COLORS_1: (red, blue);
END_TYPE
TYPE COLORS_2: (green, blue, yellow);
END_TYPE
Use of same-named components from different enumerations in one block
colorvar1 : COLORS_1;
colorvar2 : COLORS_2;
(* valid: *)
colorvar1 := COLORS_1.blue;
colorvar2 := COLORS_2.blue;
(* invalid: *)
colorvar1 := blue;
colorvar2 := blue;
You can specify explicitly the base data type of the enumeration, which by default is INT.
Example of an Explicit Other Base Data Type for an Enumeration
TYPE COLORS_2 : (yellow, blue, green:=16#8000)DINT;
END_TYPE
strict
attribute is automatically assigned to each enumeration that you add to a project in the line above the TYPE
declaration. This leads to errors that are detected during the compile process in the following cases:
Arithmetic operation with variables of the enumeration type
Assignment of a constant value to a variable of the enumeration type, in which the constant does not correspond to an enumeration value
Assignment of a non-constant value to a variable of the enumeration type, in which the non-constant has another data type than the enumeration type
You can explicitly add or remove the attribute.
Syntax: {attribute 'strict'}