Application Case of Enumerated Variables in Inovance PLC

Application Case of Enumerated Variables

· An enumeration is a user-defined data type that allows users to define meaningful names for a set of integer values, making the program more readable and maintainable.

· Enumeration variables can only take one of the identifiers defined in the enumeration, which helps to limit the range of values the variable can take, enhancing the robustness of the code.

· When programming, enumeration members can be used directly without having to memorize their corresponding integer values.

Applicable Scenarios:

· When a variable can only choose from a predefined set of values, using an enumeration can enhance the readability and maintainability of the code.

For example: state machines (such as device operating states: stopped, running, fault), mode selection (such as manual mode, automatic mode), error codes, etc.

Usage Notes:

· When defining an enumeration variable, the enumeration structure (i.e., enumeration type) must be defined first, and then the variable of that enumeration type must be defined in the variable table.

· When assigning values to enumeration type variables, only members defined in the enumeration can be used; integers cannot be used directly (unless type casting is performed, which is not recommended).

· When defining enumeration members, their corresponding integer values can be specified; if not specified, they will increment from 0 by default.

Common Case:

Assuming we have a device with three operating modes: manual mode, automatic mode, and debug mode. We can use an enumeration to define these modes and use them in the program.

Steps:

Define the enumeration structure:

In the project management tree under “Global Variables”, right-click on “Enumeration” and select “New Enumeration Structure”.

Name the enumeration structure “OperationMode”.

Add three members to the enumeration structure: Manual, Auto, Debug, and specify their values (or leave unspecified, defaulting to increment from 0).

Define the enumeration variable:

In the variable table, define a variable with the data type set to “OperationMode”.

Using the enumeration variable in the program:

In ST language, enumeration members can be directly assigned to the enumeration variable.

In the ladder diagram, the MOV instruction can be used to assign enumeration members (which are essentially integers) to the enumeration variable, but it is more recommended to use ST.

Assuming we have an ST program that performs different operations based on the current mode variable.

(* Define enumeration structure *)

TYPE OperationMode :

(Manual := 0, // Manual Mode

Auto := 1, // Automatic Mode

Debug := 2 // Debug Mode);

END_TYPE

(* Define variable in the variable table *)

VAR

currentMode : OperationMode;

END_VAR

(* In the ST program, values can be assigned and checked as follows *)

IF condition1 THEN

currentMode := OperationMode.Manual;

ELSIF condition2 THEN

currentMode := OperationMode.Auto;

ELSE currentMode := OperationMode.Debug; END_IF;

CASE currentMode OF

OperationMode.Manual:

(* Execute manual mode logic *)

OperationMode.Auto:

(* Execute automatic mode logic *)

OperationMode.Debug:

(* Execute debug mode logic *) END_CASE;

Based on the above analysis, we first define two enumerations: “DeviceStatus” for representing status and “OperationMode” for representing modes, and set their members accordingly.

Application Case of Enumerated Variables in Inovance PLC

Then, two variables can be defined in the global variable table, with their data types set to the corresponding enumerations. As shown in the figure below:

Application Case of Enumerated Variables in Inovance PLC

Then, the corresponding variables can be used in the ST program to write the program.

Application Case of Enumerated Variables in Inovance PLC

Thus, by switching different modes and pressing different buttons, different state displays can be switched. Then, under different states, different statements can be executed.

Application Case of Enumerated Variables in Inovance PLC

However, programming is not a fixed method; it can also be used in ladder diagrams. In this case, the enumeration is essentially an integer (DINT), so integer operations can be used for assignment. Although it can be used, it seems redundant, so enumerations are generally used more in ST. Therefore, it is recommended to use ST language in scenarios with complex logic or multiple mode handling to fully leverage the advantages of enumerations.

Leave a Comment