PLC

Enumerations

An enumeration is a user-defined data type, which is composed of a comma-separated set of components, also referred to as enumeration values. The enumeration values ​​are identifiers for global constants known in the project.

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

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

See also:

Object DUT
 

Declaration of the enumeration

Syntax:

TYPE <enum identifier>:
(
    <enum_0>|:=<value>,
    <enum_1>|:=<value>,
    ...,
    <enum_n>|:=<value>
) | <basic data type>|:=<default value>;
END_TYPE

Optionally possible:

Initialization of individual enumeration values.
Specification of a <basic data type>. We strongly recommend referring to the section Extensions to the IEC 61131-3 standard below!
Specification of a default value for initializing all components.
 
 

Declaration of a variable with the enumeration type

Syntax: <variable identifier> : <enum identifier>|: = <initialization value>

A variable of the type <enum identifier> can assume one of the enumeration values <enum_... >.

 
 

Initialization

The following applies, if you did not define a <default value> in the declaration of the enumeration (see below), and if you do not specify an explicit initialization value when declaring the variable with type <enum identifier>:

TwinCAT initializes the variable with the value of the first enumeration component. Except: If a component with the value 0 is initialized in the declaration of the enumeration, the variable is initialized with 0.

Example: Enumeration with an explicit initialization value for a component

Declaration of the enumeration:

TYPE E_TrafficSignal :
(
    eRed,
    eYellow,
    eGreen := 10
); (*The initial value for each of the colors is eRed = 0, eYellow = 1, eGreen = 10*)
END_TYPE

In this declaration, the first two components are assigned the default initialization values: eRed = 0, eYellow = 1; the initialization value of the third component is explicitly defined differently: eGreen = 10.

Use in implementation:

eTrafficSignal1 : E_TrafficSignal;
eTrafficSignal1 := 10; (* The value of the eTrafficSignal1 is "eGreen" *)

Example: Enumeration with defined default value

Declaration of the enumeration:

TYPE E_Colors :
(
    eRed,
    eYellow,
    eGreen
) := eGreen;
END_TYPE

Use in implementation:

eC1 : E_Colors;
eC2 : E_Colors := E_Colors.eYellow;

In this case, variable eC1 is initialized with the default value eGreen, while the initialization value eYellow is explicitly defined for eC2.

 
 

Extensions of the IEC 61131-3 standard

1. You can make the access to an enumeration constant unique by specifying the type name of the enumeration. You enter the type name (enumeration namespace) before the component.

This means that you can use the same constant in different enumerations.

Example:

Definition of two enumerations with a component of the same name:

TYPE E_Colors_1:
(
    eRed,
    eBlue
);
END_TYPE

TYPE E_Colors_2:
(
    eGreen,
    eBlue,
    eYellow
);
END_TYPE

Using components of the same name from different enumerations in one function block:

eColorvar1 : E_Colors_1;
eColorvar2 : E_Colors_2;

(* valid: *)
eColorvar1 := E_Colors_1.eBlue;
eColorvar2 := E_Colors_2.eBlue;

(* invalid: *)
eColorvar1 := eBlue;
eColorvar2 := eBlue;

2. You can explicitly define the basic data type of the enumeration, which is INT by default.

Example: Explicitly different basic data type for an enumeration

TYPE E_Colors_2 :
(
    eYellow,
    eBlue,
    eGreen := 16#8000
) DINT;
END_TYPE

See also: