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 Add Object dialog box.
NOTE: Local enumeration declaration is only possible within TYPE.
Syntax for Declaring Enumerations
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:
oInitialization of single enumeration values
oDefinition of a <base data type> (refer to the paragraph Second Extension to the IEC 61131-3 Standard)
oDefinitions of a default value for initializing all components
Syntax for Declaring Variables with the Enumeration Type
<variable identifier> : <enum identifier>| := <initialization value>
A variable of type <enum identifier> can take the enumeration values <enum_..>.
If... |
Then ... |
---|---|
oIf you have not defined a <default value> in the declaration of the enumeration (see following example), and oIf you have neither defined an explicit initialization value for the declaration of variables of the type <enum identifier> |
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. |
Example of an Enumeration with an Explicit Initialization Value for a Component
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;
Example of an Enumeration with a Defined Default Value
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.
First Extension to the IEC 61131-3 Standard
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;
Second Extension to the IEC 61131-3 Standard
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
NOTE: The 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:
oArithmetic operation with variables of the enumeration type
oAssignment of a constant value to a variable of the enumeration type, in which the constant does not correspond to an enumeration value
oAssignment 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'}