Main Content

Integrating User Defined Function Blocks, Data Types, and Global Variables into Generated Structured Text

This model shows how to integrate user defined function blocks, data types and global variables into generated structured text.

Open the top level subsystem 'Subsystem' by double clicking on it. You will notice it has a bunch of blocks including the block 'ExternallyDefinedBlock'. The user would like to replace this with an externally defined block in the PLC IDE in the generated code. In this model 'ExternallyDefinedBlock' is a MATLAB® block. This could be any other Simulink® block or subsystem as well. The input port 'In1' is a bus input of 'InBus' data type. The user would like to provide the definition of 'InBus' externally in the PLC IDE. Similarly, the user would like to provide the definition of the 'K1' global tunable parameter of the Gain block externally.

To do this, right click on the top level subsystem 'Subsystem' and select 'PLC Code -> Options ...' to bring up the 'Configuration Parameters' dialog box. In the dialog box, select 'Symbols' pane under 'PLC Code Generation'. You will see the following symbols in the 'Externally Defined Symbols' field:

  ExternallyDefinedBlock InBus K1

Specifying these symbols here would respectively omit the function block, bus data type and global variable with the same names in the generated structured text.

Now you can generate PLC Structured Text code for this subsystem by right-clicking on the subsystem block and select PLC Code -> Generate Code for Subsystem Alternatively, you can use the following command generatedFiles = plcgeneratecode('plcdemo_external_symbols/Subsystem');

After the code generation, the Diagnostic Viewer window is displayed with hyperlinks to the generated code files. You can open the generated files by clicking on the links.

In the generated code, you will notice that there are calls to function block 'ExternallyDefinedBlock', however the definition of the function block has been omitted. Similarly, the definition of bus type 'InBus1' and global variable 'K1' have also been omitted.