Main Content

Generate Structured Text Code by Using a Custom Supported Function and Block List

This example shows how to generate structured text code by using a custom supported function and block lists. Simulink® PLC Coder™ returns an error when generating code for unsupported blocks or functions. Generate structured text code for the unsupported blocks or functions by creating a custom hook file function. You must validate the generated code when using a custom hook file.

Custom Function Model Description

The model consists of a subsystem block called Subsystem that accepts two integers as inputs to a MATLAB Function block. The MATLAB Function block adds the two integers to a double precision number generated by using the str2double function inside the function block. The str2double function is currently unsupported for PLC code generation.

open_system("CustomizedMLFcn.slx");

Create Custom Function List

To create a custom function list, follow these guidelines:

  • The function name must be plc_library_function_hook.

  • The function definition must include the path the other functions for which you want to generate code. To list all functions inside a toolbox or folder, use an asterisk.

You can modify the plc_library_function_hook.m function to add, remove, or modify the registered library functions.

For example, this function definition shis you how to support an individual function fcn_xyz or all functions inside the folder lib_bar.

 function lib_map = plc_library_function_hook(lib_map)
% register toolbox/tbx_foo/tbx_foo/lib_bar/fcn_xyz
 lib_map('toolbox/tbx_foo/tbx_foo/lib_bar/fcn_xyz.m') = {'fcn_xyz'}; 
% register all functions or blocks under toolbox/tbx_foo1/tbx_foo1/lib_bar
 lib_map('toolbox/tbx_foo1/tbx_foo1/lib_bar') = {'*'}; 
 end

This custom function list file enables code generation for the str2double function.

type plc_library_function_hook.m
function lib_map = plc_library_function_hook(lib_map)
    lib_map('toolbox/eml/lib/matlab/strfun/str2double.m') = {'str2double'};
    lib_map('toolbox/eml/lib/matlab/strfun/char.m') = {'char'};
end

Place the custom function list on the MATLAB path.

Custom Block Model Description

The model consists of a subsystem block called Subsystem that accepts pulse and ramp signals as inputs to an LPV System block. The LPV system block generates linear parameter-varying output and state values. The LPV System block is currently unsupported for PLC Code generation.

open_system("CustomizedBlock.slx");

Create a Custom Block List

To create a custom block list, follow these guidelines:

  • The function name must be plc_library_hook.

  • The function definition must include the path to the blocks for which you want to generate code. To list all blocks inside a toolbox or folder, use an asterisk.

You can modify the plc_library_hook.m function to add, remove, or modify the registered library blocks.

For example, this function definition shis you how to support an individual block blk_xyz or all functions inside the folder lib_bar1.

function lib_map = plc_library_hook(lib_map)
% register toolbox/tbx_foo/tbx_foo/lib_bar/.slx library with block blk_xyz

 lib_map('toolbox/tbx_foo/tbx_foo/lib_bar') = {'blk_xyz'}; 
% register all blocks in toolbox/tbx_foo1/tbx_foo1/lib_bar1.slx

 lib_map('toolbox/tbx_foo1/tbx_foo1/lib_bar1') = {'*'}; 
 end

This custom file enables code generation for the LPV System block.

edit plc_library_hook.m

Place the plc_library_hook.m file on the MATLAB® path.

Generate Structured Text Code

To generate structured text code using the Simulink® PLC Coder™ app:

  1. Open the PLC Coder app. Click Settings > PLC Code Generation. Change the Target IDE to 3S CodeSys 2.3. Click OK.

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

Alternatively, to generate structured text code at the MATLAB command line, use the plcgeneratecode function.

generatedfiles = plcgeneratecode('CustomizedMLFcn/Subsystem');
### Generating PLC code for 'CustomizedMLFcn/Subsystem'.
### Using model settings from 'CustomizedMLFcn' for PLC code generation parameters.
### Gathering test vectors for PLC testbench.
### Begin code generation for IDE codesys23.
### Emit PLC code to file.
### Creating PLC code generation report CustomizedMLFcn_codegen_rpt.html.
### PLC code generation successful for 'CustomizedMLFcn/Subsystem'.
### Generated files:
plcsrc\CustomizedMLFcn.exp
generatedfilescustomblock = plcgeneratecode("CustomizedBlock/Subsystem");
### Generating PLC code for 'CustomizedBlock/Subsystem'.
### Using model settings from 'CustomizedBlock' for PLC code generation parameters.
### Begin code generation for IDE codesys23.
### Emit PLC code to file.
### Creating PLC code generation report CustomizedBlock_codegen_rpt.html.
### PLC code generation successful for 'CustomizedBlock/Subsystem'.
### Generated files:
plcsrc\CustomizedBlock.exp

Limitations

When you remove supported functions or blocks from the custom hook files, Simulink PLC Coder™ does not continue to support the functions or blocks for code generation. When you generate code after adding functions or blocks to custom hook files, you must verify and validate the generated code before you import the code into your target IDE.

See Also

Topics