MATLAB Answers

S-function support with Simulink Desktop Real-Time in External Mode

19 views (last 30 days)
I have written an S-function that uses Windows API functions. When I try to compile this model using sldrt.tlc to run the real-time simulation in External Mode, I receive the following build error using MATLAB R2020a:
In file included from ...\myFunction.c:134:
C:/PROGRA~1/MATLAB/R2020a/toolbox/sldrt/lib/include\Windows.h:16:2: error:
Attempt to include "windows.h"
C:/PROGRA~1/MATLAB/R2020a/toolbox/sldrt/lib/include\Windows.h:17:2: error:
The model being compiled contains a block that can run on host only and cannot be
compiled for External Mode. Please either switch to Normal Mode or remove the block.
When I add an S-Function to my model, is there a limitation as to the C library functions it can use with Simulink Desktop Real-Time simulation in External Mode?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 4 Jun 2020
Edited: MathWorks Support Team on 4 Jun 2020
S-functions can be used in Simulink Desktop Real-Time (SLDRT) External Mode only if they are self-contained, i.e. they do not call anything but a limited subset of the standard C library, like math and string functions, but not file or console I/O, or Windows API functions (manifested by the need to #include <windows.h>).
Note that being able to run an S-function with other targets (such as grt.tlc, ert.tlc or slrt.tlc) is not a proof that it will run with sldrt.tlc. For example, S-functions that are linked with dynamically linked libraries (DLLs) on Windows generally do not run with SLDRT because DLLs are not available to the real-time kernel.
In particular, the following functions are not supported in the C source code:
  • File I/O (fopen etc.)
  • Process management (spawn, exit, etc.)
  • Signals and exceptions (signal, longjmp, __try etc.)
  • Time functions (clock etc.)
  • anything from the Windows API
The following C library functions are supported for use with Simulink Desktop Real-Time:
  • Data conversion functions: abs atof atoi atol itoa labs ltoa strtod strtol strtoul ultoa
  • Memory allocation functions: calloc free malloc
  • Memory manipulation functions: _memccpy memcpy memchr memcmp _memicmp memmove memset
  • String manipulation functions: strcat strchr strcmp strcpy strcspn _strdup _stricmp strlen _strlwr strncat strncmp strncpy _strnset strpbrk strrchr _strrev _strset strspn strstr strtok _strupr
  • Mathematical functions: acos asin atan atan2 ceil cos cosh div exp fabs floor fmod frexp ldexp ldiv log log10 max min modf pow rand sin sinh sqrt srand tan tanh uldiv
  • Character class tests and conversion: isalnum isalpha _isascii iscntrl isdigit isgraph islower isprint ispunct isspace isupper isxdigit isxupper isxlower _toascii tolower toupper
  • Searching and sorting: bsearch qsort
  • Dummy functions - can be there but do nothing: exit
  • Console I/O: fprintf printf
This information is currently not part of the Simulink Desktop Real-Time documentation. However, we submitted an internal request to add this list of functions to a future release of the documentation.
Note that the above limitation do not apply for Normal or Accelerator Mode real-time simulations. To learn more about the different SLDRT simulation modes, see the following MATLAB Answers article:

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!