Generate Custom Code by Using IDE-Specific Callback Functions
Replace strings, add custom headers, print a list of variables, and so on by generating custom code. Customize the code generated for supported target IDEs by creating IDE-specific custom callback functions. The custom callback functions can modify the generated code by using post-code generation callback functions or the intermediate controller cell array structure by using pre-code generation callback functions.
Custom Code Generation Workflow
To generate custom code for your target IDEs by using custom callback functions:
Create a custom pre- or post- code generation callback function. Place the callback functions on the MATLAB® path.
Generate custom code by using Simulink® PLC Coder™.
Create a Custom Callback Function
Create a custom pre-code generation callback function by using this template.
Replace the Pre-Code Generation Callback File Name
with the name
of the pre-code generation callback file for your target IDE from the table. The
pre-code generation function operates on the controller
structure
which contains information such as variable names, data types, and so
on.
function controller = Pre-Code Generation Callback File Name(controller) % add your custom actions here end
Create a custom post-code generation callback function by using this template.
Replace the Post-Code Generation Callback File Name
with the name
of the post-code generation callback file for the target IDE from the table. The
post-code generation function performs operations such as replacing string values,
adding a header to every code section, and so
on.
function generatedFiles = Post-Code Generation Callback File Name(fileNames) fileName = fileNames{1}; str = fileread(fileName); % reading the file contents% do modifications to str here, f.ex.: % str = regexprep(str,'BOOL_TO_LREAL','BOOL_TO_INT'); % str = regexprep(str,'<USINT/>','<INT/>'); % str = regexprep(str, 'END_STRUCT','END_STRUCT;'); sfprivate ('str2file', str, fileName); generatedFiles = {fileName}; end
Target IDE Name | Pre-Code Generation Callback File Name | Post-Code Generation Callback File Name |
---|---|---|
3S-Smart Software Solutions CODESYS Version 2.3 | plc_precg_callback_codesys23 | plc_postcg_callback_codesys23 |
3S-Smart Software Solutions CODESYS Version 3.3 | plc_precg_callback_codesys33 | plc_postcg_callback_codesys33 |
3S-Smart Software Solutions CODESYS Version 3.5 | plc_precg_callback_codesys35 | plc_postcg_callback_codesys35 |
B&R Automation Studio® Version 3.0 | plc_precg_callback_brautomation30 | plc_postcg_callback_brautomation30 |
B&R Automation Studio Version 4.0 | plc_precg_callback_brautomation40 | plc_postcg_callback_brautomation40 |
Beckhoff® TwinCAT® 2.11 | plc_precg_callback_twincat211 | plc_postcg_callback_twincat211 |
Beckhoff TwinCAT 3.0 | plc_precg_callback_twincat3 | plc_postcg_callback_twincat3 |
PHOENIX CONTACT Software MULTIPROG® 5.0 | plc_precg_callback_multiprog50 | plc_postcg_callback_multiprog50 |
Phoenix Contact® PC WORX™ 6.0 | plc_precg_callback_pcworx60 | plc_postcg_callback_pcworx60 |
Rockwell Automation® RSLogix™ 5000 | plc_precg_callback_rslogix5000 | plc_postcg_callback_rslogix5000 |
Rockwell Automation Studio 5000 | plc_precg_callback_studio5000 | plc_postcg_callback_studio5000 |
Siemens® SIMATIC® STEP® 7 | plc_precg_callback_step7 | plc_postcg_callback_step7 |
Siemens TIA Portal | plc_precg_callback_tiaportal | plc_postcg_callback_tiaportal |
Siemens TIA Portal: Double Precision | plc_precg_callback_tiaportal_double | plc_postcg_callback_tiaportal_double |
Generic | plc_precg_callback_generic | plc_postcg_callback_generic |
PLCopen XML | plc_precg_callback_plcopen | plc_postcg_callback_plcopen |
Rexroth IndraWorks | plc_precg_callback_indraworks | plc_postcg_callback_indraworks |
OMRON® Sysmac® Studio | plc_precg_callback_omron | plc_postcg_callback_omron |
Create a custom pre-code generation callback function by using this template.
Replace the Pre-Code Generation Callback File Name
with the name
of the pre-code generation callback function from the table. The pre-code generation
function operates on the controller
structure which contains
information such as variable names, data types, and so
on.
function controller = Pre-Code Generation Callback File Name(controller) % add your custom actions here end
Create a custom post-code generation callback function by using this template.
Replace the Post-Code Generation Callback File Name
with the name
of the post-code generation callback function from the table. The post-code
generation function performs operations such as replacing string values, adding a
header to every code section, and so
on.
function generatedFiles = Post-Code Generation Callback File Name(fileNames) fileName = fileNames{1}; str = fileread(fileName); % reading the file contents% do modifications to str here, f.ex.: % str = regexprep(str,'BOOL_TO_LREAL','BOOL_TO_INT'); % str = regexprep(str,'<USINT/>','<INT/>'); % str = regexprep(str, 'END_STRUCT','END_STRUCT;'); sfprivate ('str2file', str, fileName); generatedFiles = {fileName}; end
Generate Custom Code
To create a custom pre—code generation callback function file called
plc_precg_callback_codesys23.m
, copy and paste this code into a MATLAB script and save the file. Place the file on the MATLAB path.function controller = plc_precg_callback_codesys23(controller) fprintf(1, 'processing by plc_postcg_callback_codesys23\n'); end
To create a custom post—code generation callback function file called
plc_postcg_callback_codesys23.m
, copy and paste this code into a MATLAB script and save the file. Place the file on the MATLAB path. This callback function:Reads the generated code file. For example,
plcdemo_simple_subsystem.exp
.Scans the read string and finds the string
S1
and replaces it with the strings1
.Writes the modified string back to the generated file.
function generatedFiles = plc_postcg_callback_codesys23(fileNames) fileName = fileNames{1}; str = fileread(fileName); % reading the file contents% do modifications to str here, f.ex.: % str = regexprep(str,'BOOL_TO_LREAL','BOOL_TO_INT'); % str = regexprep(str, 'END_STRUCT','END_STRUCT;'); str = regexprep(str,'<S1>','<s1>'); sfprivate ('str2file', str, fileName); generatedFiles = {fileName}; end
Open and run the
Generate Structured Text Code for a Simple Simulink Subsystem
.openExample('plccoder/GenerateStructuredTextCodeForASimpleSimulinkRSubsystemExample')
Open the PLC Coder app, click Settings > PLC Code Generation. Change Target IDE to
3S CoDeSys 2.3
. Click OK.Select the
SimpleSubsystem
block. In the PLC CODE tab, click Generate PLC Code.
Observe the MATLAB command line output, the string processing by
plc_postcg_callback_codesys23
is printed during code generation. In
the generated code, the string S1
is replaced with the string
s1
. This image shows the generated custom code with the
replaced string.