Ladder diagram generation from Stateflow® charts will be removed in a future release. To generate ladder diagrams, use Simulink® models instead. To create Simulink models compatible with ladder logic generation, do one of the following:
Use the blocks from the PLC Ladder library to create a model that is compatible with
ladder diagram generation. To open the PLC Ladder library, type
plcladderlib at the MATLAB command prompt.
Import ladder logic from a L5X file with the
To generate ladder logic from the Simulink models, use these functions:
This example shows how to import generated Ladder Diagram code to an IDE and validate the generated code against the original Stateflow chart by using the generated test bench.
For this example, the CODESYS 3.5 IDE is used. You can also use one of the other supported IDE. See IDEs Supported for Ladder Diagram Code Generation.
For the complete Ladder Diagram code generation workflow, see Ladder Diagram Generation Workflow.
After code generation, you see the Ladder Diagram code XML file
in a subfolder
plcsrc of the current working folder. To import the generated XML and view
the Ladder Diagram in the CODESYS 3.5 IDE:
Create an empty project.
Import the Ladder Diagram code to the project.
Select Project > Import PLCOpenXML and navigate to the folder containing the XML file.
A dialog box opens with a full list of the components imported from the XML. If you generate a test bench for validation, you also see the testbench.
On the POUs pane, you see the project. View the Ladder Diagram in the project.
You can compare the Ladder Diagram with the original Stateflow chart.
For instance, if you generate Ladder Diagram code from the model
plcdemo_ladder_three_aspect, in the Ladder Diagram, you can identify the
transition from the
Fault state to the
The transition appears in the Ladder Diagram in three steps:
The normally open contacts
FaultRectified are closed. Coil
receives power and is energized.
The normally open contacts
T_1_1_trans are closed. The coil
power and is energized. Other conditions not shown in figure must also be
The normally open contact
Red_new is closed. The coil
Red receives power and is energized.
Besides coils and normally open contacts , the Ladder Diagram also uses:
Normally closed contacts : They appear if the
~ operator is used
in a transition condition.
Set coils and reset coils : They are used in the Ladder Diagram for chart
initialization. Reset coils are also used if you enforce additional safeguards against
multiple states from being simultaneously active. See the argument
For more information about these symbols, refer to the IEC 61131-3 specifications.
Select Online > Simulation. Click the (Build) button and verify that there are no build errors.
If the option is not active, you might have to change the version number in your XML. Search for the version number in the XML and depending on the patch that you have, replace it with the following version number:
CODESYS V3.5 SP6 Patch1: 184.108.40.206
CODESYS V3.5 SP6 Patch3: 220.127.116.11
CODESYS V3.5 SP8 Patch2: 18.104.22.168
CODESYS V3.5 SP8 Patch4: 22.214.171.124
In your project, you see the generated test bench. To simulate using the test bench and validate your generated code:
Click the (Login) button and log in to the emulator device.
Click the (Start) button and begin simulation.
Double-click a test bench in your project. You see the following variables updating to reflect the results of validation.
testCycleNum increases from 0 to the number of
TRUE as long as
the test bench verification succeeds.