Generate Bus Variables as STRUCT Data Types in Structured Text Code
This example shows how to use Simulink® PLC Coder™ to generate bus variables as STRUCT
data types in the generated structured text. To generate bus variables as STRUCT
data types in the generated code, you must model your buses as nonvirtual buses. To create a nonvirtual bus, see Create Nonvirtual Buses. Simulink® PLC Coder™ does not support the generation of STRUCT
data types from virtual buses.
Load Bus Element Descriptions
To load the bus element definitions and descriptions, run:
load("busdetails.mat")
Open the Model
To open the model, run:
open_system('NonVirtualBus.slx')
The model consists of two buses and a subsystem that accept inputs from the bus elements.
Convert Virtual Buses to Nonvirtual Buses
To convert the model virtual buses to nonvirtual buses:
Double click the Bus Creator block named Bus Creator. In the Block Parameters dialog box, select the Output as nonvirtual bus checkbox, and click OK.
Double click the Bus Creator block named Bus Creator1. In the Block Parameters dialog box, select the Output as nonvirtual bus checkbox, and click OK.
Generate Structured Text Code
To generate structured text code select Subsystem
and then,
In the Apps tab, click PLC Coder.
In the PLC Coder tab, clock Settings > PLC Code Generation Settings. Change the Target IDE to
3S CodeSys 2.3
. Click OK.Select the
Subsystem2
block. In the PLC Code tab, click Generate PLC Code.
Alternatively, to generate structured text code from the MATLAB command line, use the plcgeneratecode
function.
generatedfiles = plcgeneratecode('NonVirtualBus/Subsystem');
### Generating PLC code for 'NonVirtualBus/Subsystem'. ### Using model settings from 'NonVirtualBus' for PLC code generation parameters. ### Begin code generation for IDE codesys33. ### Emit PLC code to file. ### Creating PLC code generation report NonVirtualBus_codegen_rpt.html. ### PLC code generation successful for 'NonVirtualBus/Subsystem'. ### Generated files: plcsrc/NonVirtualBus.xml
Inspect Generated Code
Open the generated structured text code file and view the generated code.
The generated structured text code contains VAR_INPUT
and VAR_OUTPUT
declarations with the bus elements generated as STRUCT
data types
FUNCTION_BLOCK Subsystem VAR_INPUT ssMethodType: SINT; signal1: Sinusoidal; signal2: Cosinusoidal; END_VAR VAR_OUTPUT signal1b: Sinusoidal; signal2b: Cosinusoidal; END_VAR VAR Delay_1_DSTATE: Sinusoidal; Delay_2_DSTATE: Cosinusoidal; Delay_1_InitialCondition: Sinusoidal; Delay_2_InitialCondition: Cosinusoidal; END_VAR CASE ssMethodType OF . . . . . . . . . TYPE Cosinusoidal: STRUCT Chirp: LREAL; Cosine: LREAL; END_STRUCT END_TYPE TYPE Sinusoidal: STRUCT Chirp: LREAL; Sine: LREAL; END_STRUCT END_TYPE