Main Content

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

This example shows how to integrate user-defined function blocks, data types, and global variables into generated structured text. You can integrate user-defined function blocks, data types, and global variables into your structured text code by creating blocks, data types, and variables in Simulink® and excluding them from the code generation. You can then add your user-generated code into the empty definitions. You can use this technique to customize your model to meet specific requirements.

Open Model

Open the model.

The model consists of a top-level subsystem that accepts an input bus signal, InBus, from a bus creator block. The Subsystem block contains a MATLAB Function Block named ExternallyDefinedBlock and a Gain block with the Gain parameter set to the global tunable parameter K1.

Prepare Model for External Integration

To integrate identifiers such as externally defined function blocks, data types, and global tunable paremeters, you should prepare the model by marking these identifiers to be excluded from the generated code. To exclude these elements from the generated code:

  1. In the top-level model, select the Subsystem block.

  2. In the Apps tab, click PLC Coder.

  3. In the PLC Code tab, click Settings > PLC Code Generation Settings. In the left pane, click Identifiers.

  4. In the Identifier Names section, enter ExternallyDefinedBlock InBus K1 to omit the MATLAB Function block named ExternallyDefinedBlock, InBus bus signal, and K1 global tunable parameter. Click OK.

The identifiers you enter here are excluded from the generated structured text code. You can then replace these elements with your custom definitions.

Generate and Inspect Code

To generate structured text code:

  1. In the PLC Code tab, click Settings > PLC Code Generation Settings. Change Target IDE to 3S CoDeSys 2.3. Click OK.

  2. Select the Subsystem block and in the PLC Code tab, click Generate PLC Code.

Alternatively, use the plcgeneratecode function, to generate structured text code.

generatedFiles = plcgeneratecode('plcdemo_external_symbols/Subsystem');

Inspect Generated Code

type('plcdemo_external_symbols.exp')

The generated code contains calls to the function block ExternallyDefinedBlock, but the definition of the function block has been omitted. Similarly, the definition of bus type InBus1 and global variable K1 have also been omitted. You can then enter your own custom content into the generated definitions.

See Also

|