Generate and Examine Structured Text Code

Generate Structured Text from the Model Window

You must already have set up your environment and Simulink® model to use the Simulink PLC Coder™ software to generate Structured Text code. If you have not yet done so, see Prepare Model for Structured Text Generation.

  1. If you do not have the plcdemo_simple_subsystem model open, open it now.

  2. Right-click the Subsystem block and select PLC Code > Options.

    The Configuration Parameters dialog box is displayed.

  3. On the PLC Code Generation pane, select an option from the Target IDE list, for example, 3S CoDeSys 2.3.

    The default Target IDE list displays the full set of supported IDEs. To see a reduced subset of the target IDEs supported by Simulink PLC Coder, disable the option Show full target list. To customize this list, use the plccoderpref function.

  4. Click Apply.

  5. Click Generate code.

    This button:

    • Generates Structured Text code (same as the PLC Code > Generate Code for Subsystem option)

    • Stores generated code in model_name.exp (for example, plcdemo_simple_subsystem.exp)

    When code generation is complete, a View diagnostics hyperlink appears at the bottom of the model window. Click this hyperlink to open the Diagnostic Viewer window.

    This window has links that you can click to open the associated files. For more information, see Files Generated with Simulink PLC Coder.

Generate Structured Text with the MATLAB Interface

You can generate Structured Text code for a subsystem in the Command Window with the plcgeneratecode function. You must have already configured the parameters for the model or, alternatively, you can use the default settings.

For example, to generate code from the SimpleSubsystem subsystem in the plcdemo_simple_subsystem model:

  1. Open the plcdemo_simple_subsystem model:

    plcdemo_simple_subsystem

  2. Open the Configuration Parameters dialog box using the plcopenconfigset function:

    plcopenconfigset('plcdemo_simple_subsystem/SimpleSubsystem')

  3. Select a target IDE.

  4. Configure the subsystem as described in Prepare Model for Structured Text Generation.

  5. Generate code for the subsystem:

    generatedfiles = plcgeneratecode('plcdemo_simple_subsystem/SimpleSubsystem')

When using plcgeneratecode for code generation, all diagnostic messages are printed to the MATLAB® command window.

View Generated Code

After generating the code, you can view it in the MATLAB Editor. For a description of how the generated code for the Simulink components map to Structured Text components, see PLC Code Generation Basics. In addition, note the following:

  • Matrix data types: The coder converts matrix data types to single-dimensional vectors (column-major) in the generated Structured Text.

  • Generated code header: If your model has author names, creation dates, and model descriptions, the generated code contains these items in the header comments. The header also lists fundamental sample times for the model and the subsystem block for which you generate code.

  • Code comments: You can choose to propagate block descriptions to comments in generated code. See Propagate Block Descriptions to Code Comments.

The figure illustrates generated code for the CoDeSys Version 2.3 PLC IDE. Generated code for other platforms, such as Rockwell Automation® RSLogix™ 5000, is in XML or other format and looks different.

If you are confident that the generated Structured Text is good, optionally change your workflow to automatically generate and import code to the target IDE. For more information, see Import Structured Text Code Automatically.