generate and examine structured text code
to generate structured text code from simulink® models, stateflow® charts, and matlab® functions, use the simulink plc coder™ product. use the generated structured text code in applications such as rapid prototyping, control algorithm validation, and test bench verification.
generate structured text from the model window
to generate structured text code from your simulink model, complete the steps to prepare your model for structured text code
generation. for more information, see . this tutorial uses the
plcdemo_simple_subsystem
.
in the command window, enter the model name to open the model.
plcdemo_simple_subsystem
open the plc coder app. click the plc code tab.
click settings.
on the plc code generation pane, select an option from the target ide list, for example,
codesys 2.3
.the default target ide list displays the full set of supported ides. to see a reduced subset of the target ides supported by simulink plc coder, clear the show full target list check box. to customize this list, use the
plccoderpref
function.click ok.
click generate plc code to:
generate structured text code.
store generated code in
(for example,model_name
.expplcdemo_simple_subsystem.exp
)
when code generation is complete, a view diagnostics hyperlink 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, see .
generate structured text through the matlab interface
you can generate structured text code for a subsystem in the command window by using the function. you must have already configured the parameters for the model or, alternatively, you can use the default settings.
for example, to generate code from the simplesubsystem
subsystem in
the plcdemo_simple_subsystem
model:
open the
plcdemo_simple_subsystem
model:plcdemo_simple_subsystem
open the configuration parameters dialog box by using the function:
plcopenconfigset('plcdemo_simple_subsystem/simplesubsystem')
select a target ide.
configure the subsystem by preparing your model for structured text code generation. for more information, see .
generate code for the subsystem:
generatedfiles = plcgeneratecode('plcdemo_simple_subsystem/simplesubsystem')
when using plcgeneratecode
for 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, see . you can view:
matrix data types: simulink plc 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. see .
this 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 another format.
once you are satisfied with the generated structured text code, optionally change your workflow to automatically generate and import code to the target ide. for more information, see import structured text code automatically.