Main Content

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 theSimulink PLC Coder™software to generate Structured Text code. If you have not yet done so, seePrepare Model for Structured Text Generation.

  1. If you do not have theplcdemo_simple_subsystemmodel open, open it now.

  2. Open thePLC Coderapp. ClickPLC Codetab.

  3. ClickSettings.

    配置参数对话框显示ed.

  4. On thePLC Code Generationpane, select an option from theTarget IDElist, for example,3S 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, disable the optionShow full target list. To customize this list, use theplccoderpreffunction.

  5. ClickOK.

  6. ClickGenerate PLC Code.

    This button:

    • Generates Structured Text code (same as thePLC Code>Generate Code for Subsystemoption)

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

    When code generation is complete, aView 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 with theMATLABInterface

You can generate Structured Text code for a subsystem in the Command Window with 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 using theplcopenconfigsetfunction:

    plcopenconfigset('plcdemo_simple_subsystem/SimpleSubsystem')

  3. Select a target IDE.

  4. Configure the subsystem as described inPrepare 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, seePLC 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. SeePropagate 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, seeImport Structured Text Code Automatically.