MATLAB: How to add your own PIL main file to the Embedded Coder build process

buildmainpilprocesssimulink

For custom communication it is sometimes useful to implement your own pil_main.c file. Is there a way to use different pil_main.c files, apart from changing template file provided in the MATLAB installation path?

Best Answer

The Embedded Coder product provides an API for creating target code for Processor-in-the-Loop (PIL). Basic information about PIL within Simulink can be found here:
PIL communication is possible over TCP/IP or serial interface:
To specify target-specific libraries and source files that are required to build the executable file, the Embedded Coder product provides an API rtw.pil.RtIOStreamApplicationFramework where you can add your custom pil_main.c file.
More details can be found in this documentation link:
1. Create an RTW.BuildInfo object
appFrameObj = rtw.pil.RtIOStreamApplicationFramework(componentArgs)
% returns an object that provides access to an RTW.BuildInfo object containing PIL-specific files (including a PIL main function)
2. To build the PIL application, a main function is required. Use this method to add one of the two provided files to the application framework.
To specify a main function that is adapted for on-target PIL and suitable for most PIL implementations, enter:
appFrameObj.addPILMain('target');
To specify a main function that is adapted for PIL on your development computer, enter:
appFrameObj.addPILMain('host');
Alternatively, you can specify your own main function:
componentArgs = appFrameObj.getComponentArgs;
buildInfo = appFrameObj.getBuildInfo;
buildInfo.addSourcePaths(pathToMyMainC);
buildInfo.addSourceFiles(myMainC);
Please note that the name of your custom main file e.g. myMainC = 'my_pil_main.c' should differ from the Embedded Coder product shipping template 'pil_main.c', otherwise the template file will be loaded again during the Embedded Coder build process.