Does code generation work even if I integrate S-Functions into my model?

S-Functions can be integrated into Simulink® models, which can then be built for use in the TwinCAT runtime.

There are various workflows, which are based on different circumstances. The most common four cases are briefly explained here, and the appropriate solution for integration into the code generation process is shown.

Case 1: I have access to the source code used in the S-Function.

In this case, the location of the source code can be specified in the S-Function. The code generation process can be started directly without any further steps. The source code is found and compiled for use in TwinCAT.

Case 2: I have an inlined S-Function (TLC file)

In this case, the code generation process can be started directly without any further steps, since the code of the S-Function to be inserted is contained in the TLC file. How to create a TLC file for an S-function can be found in the documentation of The MathWorks®: https://de.mathworks.com/help/simulink/sfg/how-to-implement-s-functions.html

Case 3: I have a compiled MEX file without access to the source code

In this case, a function was created by third parties and compiled as a MEX file. The source code or the TLC file was not included, e.g. to protect intellectual property. In this case, the third party supplying the MEX file must compile the source code as a TwinCAT-capable library, so that this library can be linked in real-time. A guide can be found under Samples: SFunStaticLib.

Case 4: I integrate a MEX library, whose source code I do not have, into my S-Function (whose source code is available).

In this case, too, the third party supplying the MEX file must compile the source code as a TwinCAT-compatible library. A guide can be found under Samples: SFunWrappedStaticLib.