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 this command.
load("busdetails.mat")Open the Model
To open the model and display bus line styles, run these commands.
mdl = "NonVirtualBus"; open_system(mdl) set_param(mdl,SimulationCommand="update")

The model consists of two buses and a subsystem that accept inputs from the bus elements.
Convert Virtual Buses to Nonvirtual Buses
To output nonvirtual buses instead of virtual buses, the Bus Creator blocks named Bus Creator and Bus Creator1 each have:
Output data type set to a
Simulink.BusobjectOutput as nonvirtual bus selected
To view these specifications, double-click the Bus Creator blocks or use the Property Inspector.
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
Subsystem2block. 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 CODESYS 3.3 (codesys33). ### Emit PLC code to file. ### Creating PLC code generation report index.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