Main Content

Generate and Examine Structured Text Code

To generate structured text code from Simulink®models, Stateflow®charts, and MATLAB®functions, use theSimulink PLC Coder™product. Use the generated structured text code in applications such as rapid prototyping, control algorithm validation, and test bench verification.

Generate Structured Text from the Model Window

To generate structured text code from your Simulink model, complete the steps to prepare your model for structured text code generation. For more information, seePrepare Model for Structured Text Generation. This tutorial uses theplcdemo_simple_subsystem.

  1. In the Command Window, enter the model name to open the model.

    plcdemo_simple_subsystem
  2. Open thePLC Coderapp. Click thePLC Codetab.

  3. ClickSettings.

  4. On thePLC Code Generationpane, select an option from theTarget IDElist, for example,3 s CoDeSys 2.3.

    The defaultTarget IDElist displays the full set of supported IDEs. To see a reduced subset of the target IDEs supported bySimulink PLC Coder, clear the显示傅ll target listcheck box. To customize this list, use theplccoderpreffunction.

  5. ClickOK.

  6. ClickGenerate PLC Codeto:

    • Generate structured text code.

    • Store generated code inmodel_name.exp(for example,plcdemo_simple_subsystem.exp)

    当完成代码生成,View diagnosticshyperlink 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, seeFiles Generated by Simulink PLC Coder.

Generate Structured Text Through theMATLABInterface

You can generate structured text code for a subsystem in the Command Window by using theplcgeneratecodefunction. You must have already configured the parameters for the model or, alternatively, you can use the default settings.

For example, to generate code from theSimpleSubsystemsubsystem in theplcdemo_simple_subsystemmodel:

  1. Open theplcdemo_simple_subsystemmodel:

    plcdemo_simple_subsystem

  2. Open the Configuration Parameters dialog box by using theplcopenconfigsetfunction:

    plcopenconfigset('plcdemo_simple_subsystem/SimpleSubsystem')

  3. 选择一个目标IDE。

  4. Configure the subsystem by preparing your model for structured text code generation. For more information, seePrepare Model for Structured Text Generation.

  5. Generate code for the subsystem:

    generatedfiles = plcgeneratecode('plcdemo_simple_subsystem/SimpleSubsystem')

When usingplcgeneratecodefor 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, seeVerify Generated Code by Using Code Tracing. You can view:

  • Matrix data types:Simulink PLC Coderconverts 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. SeePropagate Block Descriptions to Code Comments.

This 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 another format.

Once you are satisfied with the generated structured text code, optionally change your workflow to automatically generate and import code to the target IDE. For more information, seeImport Structured Text Code Automatically.