An enumeration is a user-defined data type composed of a series of comma-separated
components (enumeration values) for declaring user-defined variables. Moreover, you
can use the enumeration components like constants whose identifier <enumeration name>.<component name>
is recognized globally in the project.
You declare an enumeration in a DUT object, which you have already created in the project by clicking “Add Object”.
Declaration
Syntax
( {attribute 'strict'} )? // Pragma optional but recommended TYPE <enumeration name> : ( <first component declaration>, ( <component declaration> ,)+ <last component declaration> )( <basic data type> )? ( := <default variable initialization> )? ; END_TYPE ( ... )? : Optional <component declaration> : <component name> ( := <component initialization> )? <basic data type> : INT | UINT | SINT | USINT | DINT | UDINT | LINT | ULINT | BYTE | WORD | DWORD | LWORD <variable initialization> : <one of the component names>
In an enumeration declaration, at least 2 components are usually declared. However,
you can declare as many as you want. Every single component can be assigned its own
initialization. Enumerations automatically have the basic data type INT
, but you can specify another basic data type. Moreover, you can specify a component
in the declaration with which an enumeration variable is then initialized.
The pragma {attribute 'strict'}
causes a strict type test to be performed as described below.
Example
{attribute 'qualified_only'} {attribute 'strict'} TYPE COLOR_BASIC : ( yellow, green, blue, black ) ; // Basic data type is INT, default initialization for all COLOR_BASIC variables is yellow END_TYPE
Enumeration with explicit basic data type
Extensions to the IEC 61131-3 standard
The basic data type for an enumeration declaration is INT
by default. However, you can also declare enumerations that are based explicitly
on another integer data type.
<basic data type> : INT | UINT | SINT | USINT | DINT | UDINT | LINT | ULINT | BYTE
| WORD | DWORD | LWORD
Example
Enumeration with basic data type DWORD
TYPE COLOR : ( white := 16#FFFFFF00, yellow := 16#FFFFFF00, green := 16#FF00FF00, blue := 16#FF0000FF, black := 16#88000000 ) DWORD := black; // Basic data type is DWORD, default initialization for all COLOR variables is black END_TYPE
Strict programming rules




NOTICE

In CODESYS V3.5 SP7 and later, the pragma {attribute 'strict'}
is added automatically in the first line when declaring an enumeration.
The strict programming rules are activated when adding the pragma {attribute 'strict'}
.
The following code is considered a compiler error:
-
Arithmetic operations with enumeration components
For example, an enumeration variable cannot be used as a counter variable in a
FOR
loop. -
Assignment of a constant value, which does not correspond to an enumeration value, to an enumeration component
-
Assignment of a non-constant variable, which has another data type as the enumeration, to an enumeration component
Arithmetic operations can lead to undeclared values being assigned to enumeration
components. A better programming style is to use SWITCH
/CASE
statements for processing component values.
Declaration and initialization of enumeration variables
Syntax
<variable name> : <enumeration name> ( := <initialization> )? ;
For a declaration of an enumeration variable with user-defined data type <enumeration name>
, this can be initialized with an enumeration component.
Example
PROGRAM PLC_PRG VAR colorCar: COLOR; colorTaxi : COLOR := COLOR.yellow; END_VAR
The variable colorCar
is initialized with COLOR.black
. That is the default initialization for all enumeration variables of type COLOR
and defined this way in the type declaration. The variable colorTaxi
has its own initialization.
If no initializations are specified, then the initialization value is 0.
Example
PROGRAM PLC_PRG VAR cbFlower : COLOR_BASIC; cbTree: COLOR_BASIC := COLOR_BASIC.green; END_VAR
The variable cbFlower
is initialized with COLOR_BASIC.yellow
. That is the default initialization for all enumeration variables of type COLOR_BASIC
. Because the enumeration declaration does not specify a component for initialization,
the system automatically initializes with the component that has the value 0. This
is usually the first of the enumeration components. However, it can also be another
component that is not in the first position but explicitly initialized with 0.
The variable cbTree
has an explicit initialization.
If no value is specified for both the type and the variable, then the following rule applies: If an enumeration contains a value for 0, then this value is the default initialization, and if not, then the first component in the list.
Example
Initialization with the 0 component
TYPE ENUM : ( e1 := 2, e2 := 0, e3 ) ; END_TYPE PROGRAM PLC_PRG VAR e : ENUM; END_VAR
The variable e
is initialized with ENUM.e2
.
Initialization with the first component
TYPE ENUM2 : ( e1 := 3, e2 := 1, e3 ) ; END_TYPE PROGRAM PLC_PRG VAR e2 : ENUM2; END_VAR
The variable e2
is initialized with ENUM.e1
.
Unique access to enumeration components
Extensions to the IEC 61131-3 standard
The enumeration components can also be used as constant variables with the identifier
<enumeration name>.<component name>
. Enumeration components are recognized globally in the project and access to them
is unique. Therefore, a component name can be used in different enumerations.
Example
Component blue
PROGRAM PLC_PRG VAR cbFlower : COLOR_BASIC; colorCar : COLOR; END_VAR (* unambiguous identifiers although the component names are identical *) cbFlower := COLOR_BASIC.blue; colorCar := COLOR.blue; (* invalid code *) cbFlower := blue; colorCar := blue;
See also