Import Structured Text Code Generated from Model Reference Hierarchy
This example shows how to import and integrate the IEC 61131-3 Structured Text code generated from a model reference hierarchy in the CODESYS 3.5 IDE. Use Model referencing in Simulink® to improve:
Symbol naming consistency
Model configuration consistency
Generate Structured Text Code and Test Bench
Generate the Structured Text code and test bench for the model reference hierarchy.
Open the
GenerateStructuredTextCodeForModelReferenceHierarchyExample
example filesopenExample("plccoder/GenerateStructuredTextCodeForModelReferenceHierarchyExample")
Open the
simple_top
model. Open the PLC Coder app, and click Settings. In the PLC Code Generation pane, set Target IDE toCODESYS 3.5
, and select the Generate testbench for subsystem check box. Click OK.Select the Subsystem block. In the PLC Code tab, click Generate PLC Code.
Import Generated Structured Text Code
To import the generated Structured Text code, you must import these files:
If present, the file that contains the global symbols for the top and reference models in the target IDE. Symbol file names have the format
top_model_name_symbols.*
.The Structured Text code files for the referenced model subsystems. Import the files in the reverse order of the model dependency. You can use the Dependency Analyzer tool to analyze the model dependency order. See, Analyze Model Dependencies.
To import the generated Structured Text files and the symbols file:
Open the CODESYS V3.5 IDE. You can use CODESYS 3.5 SP 16+ or later.
Create a new project. Select Standard project and set the project name. Set Device to
CODESYS Control Win3 x64 (CODESYS)
and set PLC_PRJ in toStructured Text (ST)
.Select Application and click Project > Import PLCopenXML. Navigate and select the
simple_top_symbols.xml
file. SelectGVL_CONSTS
andGVL_VARS
, and click OK. If you do not have a symbols file, skip this step.Click Project > Import PLCopenXML. Navigate and select the
simple_sub1.xml
andsimple_sub2.xml
files. SelectSubsystem
andb_Subsystem
, and click OK.Click Project > Import PLCopenXML. Navigate and select the
simple_top.xml
. Selectc_Subsystem
,MainTB
, andTestBench
. Click OK.
Build, Download, and Verify Generated Code
Build and download the generated Structured Text code to the CODESYS software-based programmable logic controller (SoftPLC).
in the CODESYS IDE set the main task interval time to match the top model top-level subsystem sample time. To set the main task interval time, in the CODESYS IDE double-click MainTask (IEC-Tasks) in the Devices pane and set the time in Interval (e.g. t#200ms). To find the top-model top-level subsystem time in the
simple_top
model, open the model, right-click the Subsystem block, select Block Parameters (Subsystem), and view the time set in the Sample time (-1 for inherited) parameter. If the value is-1
set the Sample time (-1 for inherited) to0.1
and set the Interval (e.g. t#200ms) time in CODESYS to100
.In the CODESYS IDE, click Build > Build, and Build > Generate Code.
Start the SoftPLC by selecting CODESYS Control Win SysTray - x64 from the Windows® tray, and then select Start PLC.
Click Online > Login. If you are logging in for the first time, set up your user name and password. If you are an existing user, then enter your login credentials.
Delete PLC_PRG from Application and Task Configuration. Right-click MainTask (IEC-Tasks) and select Add Object > Program Call. Under POU to call, browse and select MainTB. Click OK.
Click Online > Download to download the code to the SoftPLC.
Double-click TestBench and click Debug > Start. Observe the values changing in the TestBench program. When
testCycleNum
is51
andtestVerify
isTRUE
, the test bench code has passed verification and the imported code is verified.