Enumerated Types

An enumerated type defines a named set of values.

IEC 61131-3

Section 2.3.3.1

Support

Supported

Syntax

TYPE
    type_name : ( value1, value2, ... ) ;
END_TYPE

Example

TYPE
    TrafficLight : (Red, Yellow, Green);
END_TYPE

PROGRAM main
    VAR
        state : TrafficLight := Red;
    END_VAR

    IF state = Green THEN
        state := Yellow;
    END_IF;
END_PROGRAM

Member names must be unique within the type. The members take consecutive values starting at zero, so Red is 0, Yellow is 1 and Green is 2.

Inline Enumerations

A variable can list its values in place of a type name. The values number the same way as in a TYPE declaration, and a variable without an initial value starts at the first one:

PROGRAM main
    VAR
        mode : (Idle, Running, Stopped) := Running;
        count : DINT;
    END_VAR

    CASE mode OF
        Idle: count := 0;
        Running: count := count + 1;
        Stopped: mode := Idle;
    END_CASE;
END_PROGRAM

Each such declaration declares a type of its own, even when two declarations list the same values. The type has no name, so debugging output such as ironplcvm run --dump-vars shows the variable’s value as RUNNING (1) and describes its type by a generated name.

Values Shared by Two Enumerations

Two enumerations may declare the same value name. A value written without its type takes the type of where it is used: the variable it is assigned to, the other operand of a comparison, the selector of a CASE, the input of a function block it is passed to, or the variable it initializes:

TYPE
    Color : (Red, Green);
    Light : (Off, Green);
END_TYPE

PROGRAM main
    VAR
        shade : Color := Green;  (* 1, the Green of Color *)
        lamp : Light;
    END_VAR

    lamp := Green;               (* 1, the Green of Light *)
    IF shade = Green THEN        (* the Green of Color *)
        lamp := Off;
    END_IF;
END_PROGRAM

Where nothing says which enumeration is meant, such as an assignment to a DINT, the value is ambiguous and the compiler reports P2043.

Explicit Values

Note

This feature requires IEC 61131-3 Edition 3. Use --dialect iec61131-3-ed3 to enable it. See Enabling Dialects and Features for details.

A member can be given its own value instead of the one its position implies. Members that follow continue from the value before them, so Type_ANY below is 1 and Type_BOOL is 2:

TYPE
    E_AssertionType : (Type_UNDEFINED := 0, Type_ANY, Type_BOOL);
END_TYPE

PROGRAM main
    VAR
        kind : E_AssertionType := Type_ANY;
    END_VAR

    IF kind = Type_ANY THEN
        kind := Type_BOOL;
    END_IF;
END_PROGRAM

Values are not checked for uniqueness: (A := 1, B := 1) gives two names for the same value and is accepted. Only the names must differ.

Every value, written or continued from the one before, must be within the range of DINT: from -2147483648 to 2147483647. A value outside it is P2044, whatever base type the declaration names.

Base Type (Language Extension)

Note

This is an extension not part of the IEC 61131-3 standard. See Enabling Dialects and Features for how to enable it.

A declaration can name a base type after its members:

TYPE
    Color : (Red, Green, Blue) INT;
END_TYPE

PROGRAM main
    VAR
        shade : Color := Blue;
    END_VAR

    IF shade = Blue THEN
        shade := Red;
    END_IF;
END_PROGRAM

IronPLC accepts the base type and stores the enumeration as a DINT whatever type it names, as it stores every enumeration.

See Also