Exploring Enumerators in WinCC OA

Introduction to Enumerators

Enumerators, or enums, are a powerful feature in programming languages like C++ and WinCC OA. An enum is a user-defined data type that consists of a set of named integral constants. They make code more readable and maintainable by giving meaningful names to values, instead of using arbitrary numbers. Enums are particularly useful in scenarios where a variable can take one out of a small set of possible values.


enum DataType {
  Integer = 1,
  Float = 2,
  String = 3,
  Password = 4,
  Time = 5
};
    

Using Enumerators in WinCC OA

In WinCC OA, enumerators can be utilized to create extended properties in a pictogram within a panel. This allows for the creation of custom rules and options that can be selected via a combobox in the GEDI UI. Here’s an example of how to define and use an enum for this purpose:


enum DataType {
  Integer = 1,
  Float = 2,
  String = 3,
  Password = 4,
  Time = 5
};

#property DataType DataTypeSelect DataType g_DataTypeSelect;

public int getDataTypeSelect() {
    return (int)g_DataTypeSelect;
}

public void setDataTypeSelect(int DataTypeSelect) {
    g_DataTypeSelect = DataTypeSelect;
}
    

In this example, the DataType enum is used to define the data type for a pictogram. The property DataTypeSelect is recognized by the GEDI UI and appears as a combobox with the options defined in the enum. This allows for easy selection and application of data types within the pictogram.

Transforming and Reading Enum Values

When working with enums, it's essential to know how to read and transform their values for different purposes. WinCC OA provides functions like enumKeys() to retrieve the list of keys in an enumeration. Here’s an example:


enum enumLocal { A, B, C };

main(mapping event) {
    DebugN(enumKeys("enumLocal"));
}
    

This function returns the keys of the enumeration as a list in the order they were added. The output of the script would look something like this:


WCCOAui1:[dyn_string 3 items
WCCOAui1:     1: "A"
WCCOAui1:     2: "B"
WCCOAui1:     3: "C"
WCCOAui1:]
    

Casting and Conversion of Enums

One of the key aspects of using enums is understanding how to cast and convert them to different types. In WinCC OA, enums can be cast to integers for various operations. This is particularly useful when passing the selected property to other functions or systems.


enum DataType {
  Integer = 1,
  Float = 2,
  String = 3,
  Password = 4,
  Time = 5
};

public int getDataTypeSelect() {
    return (int)g_DataTypeSelect;
}

public void setDataTypeSelect(int DataTypeSelect) {
    g_DataTypeSelect = DataTypeSelect;
}
    

In the example above, the enum DataType is cast to an integer using the getDataTypeSelect method. This allows for the enum value to be used in various contexts where an integer representation is required.

Practical Applications of Enums

Enumerators are incredibly versatile and can be applied in numerous scenarios. For instance, they can be used to define a set of states for a system, configuration options, or types of input data. The ability to define and use enums in WinCC OA allows for more structured and readable code, making it easier to manage complex projects.

Here’s a more comprehensive example of using enums in WinCC OA to define different data types and apply them in a pictogram:


enum DataType {
  Integer = 1,
  Float = 2,
  String = 3,
  Password = 4,
  Time = 5
};

#property DataType DataTypeSelect DataType g_DataTypeSelect;

public int getDataTypeSelect() {
    return (int)g_DataTypeSelect;
}

public void setDataTypeSelect(int DataTypeSelect) {
    g_DataTypeSelect = DataTypeSelect;
}

void applyDataType() {
    switch (g_DataTypeSelect) {
        case DataType::Integer:
            // Apply integer-specific logic
            break;
        case DataType::Float:
            // Apply float-specific logic
            break;
        case DataType::String:
            // Apply string-specific logic
            break;
        case DataType::Password:
            // Apply password-specific logic
            break;
        case DataType::Time:
            // Apply time-specific logic
            break;
        default:
            // Handle default case
            break;
    }
}