Main Content

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:

  1. Create a custom pre- or post- code generation callback function. Place the callback functions on the MATLAB® path.

  2. 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 NamePre-Code Generation Callback File NamePost-Code Generation Callback File Name

3S-Smart Software Solutions CODESYS Version 2.3

plc_precg_callback_codesys23plc_postcg_callback_codesys23

3S-Smart Software Solutions CODESYS Version 3.3

plc_precg_callback_codesys33plc_postcg_callback_codesys33

3S-Smart Software Solutions CODESYS Version 3.5

plc_precg_callback_codesys35plc_postcg_callback_codesys35

B&R Automation Studio® Version 3.0

plc_precg_callback_brautomation30plc_postcg_callback_brautomation30

B&R Automation Studio Version 4.0

plc_precg_callback_brautomation40plc_postcg_callback_brautomation40

Beckhoff® TwinCAT® 2.11

plc_precg_callback_twincat211plc_postcg_callback_twincat211

Beckhoff TwinCAT 3.0

plc_precg_callback_twincat3plc_postcg_callback_twincat3

PHOENIX CONTACT Software MULTIPROG® 5.0

plc_precg_callback_multiprog50plc_postcg_callback_multiprog50

Phoenix Contact® PC WORX™ 6.0

plc_precg_callback_pcworx60plc_postcg_callback_pcworx60

Rockwell Automation® RSLogix™ 5000

plc_precg_callback_rslogix5000plc_postcg_callback_rslogix5000

Rockwell Automation Studio 5000

plc_precg_callback_studio5000plc_postcg_callback_studio5000

Siemens® SIMATIC® STEP® 7

plc_precg_callback_step7plc_postcg_callback_step7

Siemens TIA Portal

plc_precg_callback_tiaportalplc_postcg_callback_tiaportal

Siemens TIA Portal: Double Precision

plc_precg_callback_tiaportal_doubleplc_postcg_callback_tiaportal_double

Generic

plc_precg_callback_genericplc_postcg_callback_generic

PLCopen XML

plc_precg_callback_plcopenplc_postcg_callback_plcopen

Rexroth IndraWorks

plc_precg_callback_indraworksplc_postcg_callback_indraworks

OMRON® Sysmac® Studio

plc_precg_callback_omronplc_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

  1. 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
  2. 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:

    1. Reads the generated code file. For example, plcdemo_simple_subsystem.exp.

    2. Scans the read string and finds the string S1 and replaces it with the string s1.

    3. 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  
  3. Open and run the Generate Structured Text Code for a Simple Simulink Subsystem.

    openExample('plccoder/GenerateStructuredTextCodeForASimpleSimulinkRSubsystemExample')

  4. Open the PLC Coder app, click Settings > PLC Code Generation. Change Target IDE to 3S CoDeSys 2.3. Click OK.

  5. 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.

Custom code block with S1 replaced by s1.

Related Topics