Main Content

Hello World! External Code Integration for Simulink Real-Time

This example shows how to use an S-Function Builder block for external code integration. The example adds a hello message to the system log.

Before running this example, install the Simulink Real-Time Target Support Package. The support package includes the tools that compile code that runs on the target computer.

Open the Model

Use the Open Model button to open the helloworld.slx model.


Open the S-Function Block

Double-click the helloworld-sfun S-Function block. The S-Function Builder opens and displays the S-Function code.

/* Includes_BEGIN */
#include "slrt_log.hpp"
/* Includes_END */
/* Externs_BEGIN */
/* extern double func(double a); */
/* Externs_END */
void helloworld_sfun_Start_wrapper(SimStruct *S)
/* Start_BEGIN */
/* Start_END */
void helloworld_sfun_Outputs_wrapper(const real_T *u0,
                                     real_T *y0,
                                     SimStruct *S)
/* Output_BEGIN */
// Create custom message
static char hellomsg[100];
sprintf(hellomsg,"Hello World! t=%f \n",*u0);
// Use macros for platform dependent code
// Generic platform independent code
*y0 = *u0;
/* Output_END */
void helloworld_sfun_Terminate_wrapper(SimStruct *S)
/* Terminate_BEGIN */
 * Custom Terminate code goes here.
/* Terminate_END */

Build Model and Run Real-Time Application

Before building the model, you can run the model on your desktop and view the output message in the Simulink Real-Time system log viewer.

When you are ready to build the model, on the Simulink Editor Real-Time tab, connect to the target computer and click Run on Target. Or, in the MATLAB Command Window, type:

tg = slrealtime;
model = 'slrt_ex_helloworld_sfunbuilder';

View Message in Status Log

Open the target computer status log and view the Hello World! message. On the Simulink Editor Real-Time tab, select Prepare > SLRT Explorer. Then, select the System Log Viewer tab. Or, in the MATLAB Command Window, type:


The viewer shows the Hello World! messages in the system log.

Close All