Enumerations

An enumeration is a user-defined data type composed of a comma-separated series of components, also called enumeration values, that is used to declare user-defined variables. In addition, you can use the enumeration components like constant variables, whose identifiers <enumeration name>.<component name> are globally known in the project.

You declare an enumeration in a DUT object that you create in the project using the command Add > DUT in the context menu of the PLC project tree.

Enumerations 1:

Each enumeration that you add to a project is automatically given an attribute 'strict' and an attribute 'qualified only' in the line above the TYPE declaration. The attributes can also be added or removed explicitly.

See also:

Declaration

Syntax

{attribute 'strict'} 
TYPE <enumeration name>:
(
    <component declaration>,
    <component declaration>
) <basic data type> := <default variable initialization>
;
END_TYPE

{attribute 'strict'}

Optional

The pragma causes a strict type check, as described below, to be carried out.

The pragma is optional, but recommended.

<enumeration name>

Name of the enumeration that can be used as a data type in the code

Example: E_ColorBasic

<component declaration>

Two or above components (any number equal to or above two)

The values of the components are initialized automatically: starting at 0, the values are incremented consecutively by 1. However, you can also explicitly assign fixed initial values to the individual components.

Example: eYellow := 1

<basic data type>

Optional

You can explicitly declare one of the following basic data types:

UINT | SINT | USINT | DINT | UDINT | LINT | ULINT | BYTE | WORD | DWORD | LWORD

If no explicit base data type is declared, the INT base data type is used automatically.

<default variable initialization>

Optional

One of the components can be explicitly declared as the initial component. If no explicit initialization is specified, initialization is automatically carried out with the top component.

Example

{attribute 'qualified_only'}
{attribute 'strict'}
TYPE E_ColorBasic :
(
    eRed,
    eYellow,
    eGreen := 10,
    eBlue,
    eBlack
) // Basic data type is INT, default initialization for all E_ColorBasic  variables is eYellow
;
END_TYPE

In this declaration, the first two components receive the standard initialization values: eRed = 0, eYellow = 1, and the initialization value of the third component is explicitly defined differently: eGreen = 10. The following components then receive standard initialization values: eBlue = 11, eBlack = 12.

Enumeration with explicit base data type

Extensions for the IEC 61131-3 standard

The base data type for an enumeration declaration is INT by default. However, you can also declare enumerations that are explicitly based on a different integer data type.

Example: Enumeration with DWORD basic data type

TYPE E_Color :
(
    eWhite  := 16#FFFFFF,
    eYellow := 16#FFFF00,
    eGreen  := 16#FF00FF,
    eBlue   := 16#0000FF,
    eBlack  := 16#000000
)DWORD := eBlack
; // Basic data type is DWORD, default initialization for all E_Color variables is eBlack
END_TYPE

Strict programming rules

Enumerations 2:

From TwinCAT 3.1 Build 4026, the pragma {attribute 'strict'} is automatically added to the first line of an enumeration.

The strict programming rules are activated on adding the pragma {attribute 'strict'}.

The following code is then classified as a compiler error:

Arithmetical operations can lead to undeclared values being assigned to enumeration components. It is better programming style to use SWITCH/CASE instructions in order to process component values.

Declaration and initialization of enumeration variables

Syntax

<variable name> : <enumeration name> ( := <initialization> )? ;

When declaring an enumeration variable with user-defined data type <enumeration name>, this can be initialized with an enumeration component.

Sample

PROGRAM MAIN
VAR
    eColorCar  : E_Color;
    eColorTaxi : E_Color := E_Color.eYellow;
END_VAR

The variable eColorCar is initialized with E_Color.eblack. This is the standard initialization for all enumeration variables of the type E_Color and is thus defined in this way in the type declaration. The variable eColorTaxi has its own initialization.

If no initializations are specified, initialization takes place with 0.

PROGRAM MAIN
VAR
    eColorFlower : E_ColorBasic;
    eColorTree   : E_ColorBasic := E_ColorBasic.eGreen;
END_VAR

The variable eColorFlower  is initialized with E_ColorBasic.eYellow. This is the standard initialization for all enumeration variables of the type E_ColorBasic. Since no component for the initialization is specified in the enumeration declaration, initialization automatically takes place with the component that has the value 0. This is normally the first of the enumeration components. However, this can also be another component that is not in first place, but is explicitly initialized with 0. The variable eColorTree has an explicit initialization.

If no value is specified for both the type and the variable, the following rule applies: if an enumeration contains a value for 0, then this value is the standard initialization; if not, then the first component in the list.

Samples

Initialization with the 0 component:

TYPE E_SampleA :
(
    e1 := 2,
    e2 := 0,
    e3
);
END_TYPE
PROGRAM MAIN
VAR
    eSampleA  : E_SampleA;
END_VAR

The variable eSampleA is initialized with E_SampleA.e2.

Initialization with the first component:

TYPE E_SampleB :
(
    e1 := 3,
    e2 := 1,
    e3
);
END_TYPE
PROGRAM MAIN
VAR
    eSampleB  : E_SampleB;
END_VAR

The variable eSampleB is initialized with E_SampleB.e1.

Extensions of 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 globally known in the project and access to them is explicit. Therefore, a component name can be used in different enumerations.

Sample: component blue

PROGRAM MAIN
VAR
    eFlower   : E_ColorBasic;
    eColorCar : E_Color;
END_VAR
// unambiguous identifiers although the component names are identical
eFlower   := E_ColorBasic.eBlue;
eColorCar := E_Color.eBlue;

// invalid code
eFlower   := eBlue;
eColorCar := eBlue;