Main Content

Generate Structured Text for Integer-to-Enumerated Data Conversion

R2026b

This example shows how to generate IEC 61131-3 Structured Text code that converts an integer value into an enumerated type. The model represents a motor command system with a variable frequency drive (VFD). PLCs typically read integer fault codes from drives by using field bus networks such as Modbus, PROFINET, and EtherNet/IP. Converting these raw integers to an enumerated type improves code readability and enables symbolic fault handling in PLC logic.

Open the Model

Open the plc_int_to_enum_fault_model. The example model accepts an integer fault code sequence from the drive, converts it to the enumerated data value, and then displays the enumerated fault value.

modelName = "plc_int_to_enum_fault";
open_system(modelName)

The top-level atomic subsystem, FaultDecoder accepts an integer fault code input and outputs the corresponding enumerated fault type. The FaultDecoder subsystem uses a Multiport Switch block to map each integer value to a DriveFaultCode enumerated value.

Simulink model showing a signal flow from a FaultCodeSequence source through a DoubleToUint16 data type conversion block into the FaultDecoder atomic subsystem, which has a FaultCodeIn input port and a FaultNameOut output port, connected to a FaultDisplay sink block.

This table lists the integer fault code read from the drive, the enumerated fault name, and the fault description:

Integer Code

Enumerated Fault Name

Fault Description

0

NoFault

No active fault (normal operation)

1

Overcurrent

Motor current exceeded rated limit

2

Overvoltage

DC bus voltage above maximum

3

OverTemperature

Heatsink temperature exceeded safe limit

4

GroundFault

Ground fault detected on motor phases

5

CommLoss

Communication timeout with PLC

6

MotorStall

Motor failed to accelerate within time limit

7

UnderVoltage

DC bus voltage below minimum

The DriveFaultCode script file contains the enumerated data type definitions. Because the MotorCommand script contains the enumerated data type definitions, it must be in the same folder as the plc_int_to_enum_fault model.

Configure Model and Generate Code Using the PLC Coder App

You can generate Structured Text code for the plc_int_to_enum_fault model by using the PLC Coder app or by using the plcopenconfigset and plcgeneratecode functions.

First, configure the model for Structured Text code generation. To configure the model, open the PLC Coder app and in the PLC Code tab, click Settings. In the Configuration Parameters dialog box, click PLC Code Generation Settings > Identifiers. Select the Override Target Default enum Name Behavior parameter. When you select this parameter, the generated code uses the enumerated commands such as NoFault, Overcurrent, Overvoltage, and OverTemperature, instead of the corresponding integers.

To generate code:

  1. In the Configuration Parameters dialog box, in the left pane, click PLC Code Generation.

  2. Set the Target IDE to KW-Software MULTIPROG 5.0.

  3. Click OK.

  4. In the Simulink model, select the FaultDecoder subsystem.

  5. In the toolstrip, click Generate PLC Code.

Configure Model and Generate Code Programmatically

Configure the model to preserve the enumerated data names in the generated code. Set the target IDE to KW_Software MULTIPROG 5.0.

plcopenconfigset(modelName);
set_param(modelName,PLC_TargetIDE="multiprog50");
set_param(modelName,PLC_GenerateEnumSymbolicName="on");

Generate code by using the plcgeneratecode function.

generatedFiles = plcgeneratecode("plc_int_to_enum_fault/FaultDecoder")
### Generating PLC code for 'plc_int_to_enum_fault/FaultDecoder'.
### Using model settings from 'plc_int_to_enum_fault' for PLC code generation parameters.
### Begin code generation for IDE KW-Software MULTIPROG 5.0 (multiprog50).
### Emit PLC code to file.
### Creating PLC code generation report index.html.
### PLC code generation successful for 'plc_int_to_enum_fault/FaultDecoder'.
### Generated files:
plcsrc/plc_int_to_enum_fault.xml
generatedFiles = 1×1 cell array
    {'plcsrc/plc_int_to_enum_fault.xml'}

Examine the Generated Structured Text Code

The generated Structured Text code contains the definition for the DriveFaultCode enumeration type and a function block for the FaultDecoder subsystem.

coder.example.extractLines(fullfile("plcsrc","plc_int_to_enum_fault.st"), ...
    "TYPE DriveFaultCode", "END_TYPE", true, true)
TYPE DriveFaultCode:
    (NoFault, Overcurrent, Overvoltage, OverTemperature, GroundFault, CommLoss, MotorStall, UnderVoltage);
END_TYPE

View the generated function block.

coder.example.extractLines(fullfile("plcsrc","plc_int_to_enum_fault.st"), ...
    "FUNCTION_BLOCK FaultDecoder", "END_FUNCTION_BLOCK", true, true)
FUNCTION_BLOCK FaultDecoder
VAR_INPUT
    FaultCodeIn: UINT;
END_VAR
VAR_OUTPUT
    FaultNameOut: DriveFaultCode;
END_VAR
(* Outputs for Atomic SubSystem: '<Root>/FaultDecoder' *)
(* MultiPortSwitch: '<S1>/MultiportSwitch' *)
CASE UINT_TO_INT(FaultCodeIn) OF
    0:
        (* Outport: '<Root>/FaultNameOut' incorporates:
         *  Constant: '<S1>/Const_NoFault' *)
        FaultNameOut := NoFault;
    1:
        (* Outport: '<Root>/FaultNameOut' incorporates:
         *  Constant: '<S1>/Const_Overcurrent' *)
        FaultNameOut := Overcurrent;
    2:
        (* Outport: '<Root>/FaultNameOut' incorporates:
         *  Constant: '<S1>/Const_Overvoltage' *)
        FaultNameOut := Overvoltage;
    3:
        (* Outport: '<Root>/FaultNameOut' incorporates:
         *  Constant: '<S1>/Const_OverTemperature' *)
        FaultNameOut := OverTemperature;
    4:
        (* Outport: '<Root>/FaultNameOut' incorporates:
         *  Constant: '<S1>/Const_GroundFault' *)
        FaultNameOut := GroundFault;
    5:
        (* Outport: '<Root>/FaultNameOut' incorporates:
         *  Constant: '<S1>/Const_CommLoss' *)
        FaultNameOut := CommLoss;
    6:
        (* Outport: '<Root>/FaultNameOut' incorporates:
         *  Constant: '<S1>/Const_MotorStall' *)
        FaultNameOut := MotorStall;
    ELSE
        (* Outport: '<Root>/FaultNameOut' incorporates:
         *  Constant: '<S1>/Const_UnderVoltage' *)
        FaultNameOut := UnderVoltage;
END_CASE;
(* End of MultiPortSwitch: '<S1>/MultiportSwitch' *)
(* End of Outputs for SubSystem: '<Root>/FaultDecoder' *)
END_FUNCTION_BLOCK

The CASE statement in the generated function block switches on integer values and assigns enumerated data values to the FaultNameOut variable.

If you do not enable the Override Target Default enum Name Behavior parameter, the CASE statement in the generated code assigns integer values to the FaultNameOut variable. To see the difference, disable the parameter, generate code, and view the CASE statement.

set_param(modelName,PLC_GenerateEnumSymbolicName="off");
generatedFiles = plcgeneratecode("plc_int_to_enum_fault/FaultDecoder")
### Generating PLC code for 'plc_int_to_enum_fault/FaultDecoder'.
### Using model settings from 'plc_int_to_enum_fault' for PLC code generation parameters.
### Begin code generation for IDE KW-Software MULTIPROG 5.0 (multiprog50).
### Emit PLC code to file.
### Creating PLC code generation report index.html.
### PLC code generation successful for 'plc_int_to_enum_fault/FaultDecoder'.
### Generated files:
plcsrc/plc_int_to_enum_fault.xml
generatedFiles = 1×1 cell array
    {'plcsrc/plc_int_to_enum_fault.xml'}

coder.example.extractLines(fullfile("plcsrc","plc_int_to_enum_fault.st"), ...
    "FUNCTION_BLOCK FaultDecoder", "END_FUNCTION_BLOCK", true, true)
FUNCTION_BLOCK FaultDecoder
VAR_INPUT
    FaultCodeIn: UINT;
END_VAR
VAR_OUTPUT
    FaultNameOut: INT;
END_VAR
(* Outputs for Atomic SubSystem: '<Root>/FaultDecoder' *)
(* MultiPortSwitch: '<S1>/MultiportSwitch' *)
CASE UINT_TO_INT(FaultCodeIn) OF
    0:
        (* Outport: '<Root>/FaultNameOut' incorporates:
         *  Constant: '<S1>/Const_NoFault' *)
        FaultNameOut := 0;
    1:
        (* Outport: '<Root>/FaultNameOut' incorporates:
         *  Constant: '<S1>/Const_Overcurrent' *)
        FaultNameOut := 1;
    2:
        (* Outport: '<Root>/FaultNameOut' incorporates:
         *  Constant: '<S1>/Const_Overvoltage' *)
        FaultNameOut := 2;
    3:
        (* Outport: '<Root>/FaultNameOut' incorporates:
         *  Constant: '<S1>/Const_OverTemperature' *)
        FaultNameOut := 3;
    4:
        (* Outport: '<Root>/FaultNameOut' incorporates:
         *  Constant: '<S1>/Const_GroundFault' *)
        FaultNameOut := 4;
    5:
        (* Outport: '<Root>/FaultNameOut' incorporates:
         *  Constant: '<S1>/Const_CommLoss' *)
        FaultNameOut := 5;
    6:
        (* Outport: '<Root>/FaultNameOut' incorporates:
         *  Constant: '<S1>/Const_MotorStall' *)
        FaultNameOut := 6;
    ELSE
        (* Outport: '<Root>/FaultNameOut' incorporates:
         *  Constant: '<S1>/Const_UnderVoltage' *)
        FaultNameOut := 7;
END_CASE;
(* End of MultiPortSwitch: '<S1>/MultiportSwitch' *)
(* End of Outputs for SubSystem: '<Root>/FaultDecoder' *)
END_FUNCTION_BLOCK

See Also

Model Settings

Functions

Topics