PLC Code Generation Basics
Overview of how to generate structured text code
The basic steps for code generation are:
Create a model or open an existing model.
Configure the model for code generation by selecting the solver.
Configure PLC Coder code generation options.
The PLC Coder configuration parameters provide several options for the code generation.
Apps
Simulink PLC Coder | Generate structured text and ladder diagram code from Simulink models, Stateflow charts, and MATLAB functions |
Functions
plccoderdemos | Product examples |
plccoderpref | Manage user preferences |
plcgeneratecode | Generate structured text or ladder diagram (L5X) for the atomic subsystem |
plcopenconfigset | Open Configuration Parameters dialog box for subsystem |
plcsetup | Set up model parameters for PLC code generation (Since R2023b) |
Topics
Getting Started
- Generate and Examine Structured Text Code
Get started with structured text code generation from a simple Simulink® model.
- Integrate Generated Code with Custom Code
Call the generated function block with different values of thessMethodType
argument for subsystem initialization and computation steps. - Generated Code Structure for Simple Simulink Subsystems
Map model objects in your Simulink subsystem to constructs in generated structured text code.
Code Generation Files and Folders
- Files Generated by Simulink PLC Coder
Find the location of files for manually importing the generated structured text code to your IDE. - Specify Custom Names for Generated Files
Override the default naming convention, that uses the model name for the generated Structured Text code file.
Code Generation for Simulink Models
- Generated Code Structure for Triggered Subsystems
Generate structured text code from a triggered subsystem and identify the trigger event-based execution in the generated code. - Generated Code Structure for Subsystem Mask Parameters
Generate structured text code from a masked subsystem and identify the mask parameters in the generated code. - Generate Structured Text Code from For Each Subsystems
You can use For Each subsystems to convert inefficient, repetitive algorithms into compact, easy-to-read code. - Generate Structured Text Code for Width Blocks
The Width block is commonly used with the For Each subsystem block to compute the signal width inside the for-each loop.
Code Generation for Stateflow charts
- Generated Code Structure for Stateflow Charts
Generate structured text code from a Stateflow® Chart and identify event-based transitions or absolute time temporal logic in the generated code. - Generate Structured Text Code That Has Logging Instrumentation
Workflow for generating structured text code with logging instrumentation. - Generate Structured Text Code for a Stateflow Chart That Uses Absolute-Time Temporal Logic
Model, simulate, and generate code for a Stateflow® chart that uses absolute-time temporal logic. - Simulate and Generate Structured Text Code for a Stateflow Chart
This example shows how to simulate and generate code for the ControlModule Stateflow® chart from theplcdemo_stateflow_controller
model.
Code Generation for MATLAB Function Block
- Generated Code Structure for MATLAB Function Block
Generate structured text code from a MATLAB Function block and identify the MATLAB® function content in the generated code.