Main Content

Import Ladder Diagram Code to CODESYS 3.5 IDE and Validate Diagram

Note

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 plcimportladder function.

To generate ladder logic from the Simulink models, use these functions: plcgeneratecode and plcgeneraterunnertb.

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.

Import Ladder Diagram XML

After code generation, you see the Ladder Diagram code XML file ModelName.xml 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:

  1. Create an empty project.

  2. 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.

  3. 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 Red state.

    The transition appears in the Ladder Diagram in three steps:

    1. The normally open contacts VLDHealthy and FaultRectified are closed. Coil T_1_1_trans receives power and is energized.

    2. The normally open contacts Fault and T_1_1_trans are closed. The coil Red_new receives power and is energized. Other conditions not shown in figure must also be satisfied.

    3. 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 InsertGuardResets in plcgenerateladder.

    For more information about these symbols, refer to the IEC 61131-3 specifications.

  4. 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: 3.5.4.30

    • CODESYS V3.5 SP6 Patch3: 3.5.6.30

    • CODESYS V3.5 SP8 Patch2: 3.5.8.20

    • CODESYS V3.5 SP8 Patch4: 3.5.8.40

Verify Ladder Diagram with Test Bench

In your project, you see the generated test bench. To simulate using the test bench and validate your generated code:

  1. Click the (Login) button and log in to the emulator device.

  2. Click the (Start) button and begin simulation.

  3. Double-click a test bench in your project. You see the following variables updating to reflect the results of validation.

    • The variable testCycleNum increases from 0 to the number of cycles.

    • The variable testVerify remains TRUE as long as the test bench verification succeeds.