EnumerationsΒΆ
An enumeration is a user-defined data type that is composed of a series of comma-delimited string constants (also termed enumeration values). Enumeration values are identifiers for global constants in the project.
You declare an enumeration in a DUT object, which you have already created in the project by clicking Add Object.
Declaring enumerations
Syntax:
TYPE <enum identifier>:
(<enum_0>|:=<value>,<enum_1>|:=<value>, ...,<enum_n>|:=<value>)|<base data type>|:=<default value>;
END_TYPE
Optional:
- Initialization of single enumeration values
- Definition of a
<base data type>
(see restrictions below) - Definitions of a default value for initializing all components
Declaring variables with the enumeration type
Syntax:
<variable identifier> : <enum identifier>| := <initialization value>
A variable of the type <enum identifier>
can take the enumeration values <enum_..>
.
Initialization
If you have not defined a <default value>
in the declaration of the enumeration (see below), and
you have also not defined an explicit initialization value for the declaration of variables of the type <enum identifier>
, then
the following applies:
CODESYS initializes the variable with the value of the first enumeration component, unless a component is initialized with the value 0
in the enumeration declaration, in which 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 receive default initialization values: red
= 0
, yellow
= 1
, and 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
, and the initialization value yellow
is defined for c2
.
Extensions to the IEC 61131-3 standard
1. With additional definition of the enumeration type name, you can make unique access to an enumeration constant. You also define the type name (enumeration namespace) before the component.
In this way, you can 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;
2. You can explicitly define the base data type of the enumeration (INT by default).
Attention
As of CODESYS V3.5 SP7, each enumeration that you add to a project receives the 'strict'
attribute automatically in the line above the TYPE
declaration. For compiler versions >=3.5.7.0 , this causes compile errors 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
The attribute can also be added explicitly or removed.
Syntax: {attribute 'strict'}
Example
Explicit of other base data type for an enumeration
TYPE COLORS_2 : (yellow, blue, green := 16#8000) DINT;
END_TYPE
See also