Skip to Main Content Skip to Search
Product Documentation

Create a Transport Layer for External Communication

About Creating a Transport Layer for External Communication

This section helps you to connect your custom target by using external mode using your own low-level communications layer. The topics include:

This section assumes that you are familiar with the execution of Simulink Coder programs, and with the basic operation of external mode.

Design of External Mode

External mode communication between the Simulink engine and a target system is based on a client/server architecture. The client (the Simulink engine) transmits messages requesting the server (target) to accept parameter changes or to upload signal data. The server responds by executing the request.

A low-level transport layer handles physical transmission of messages. Both the Simulink engine and the model code are independent of this layer. Both the transport layer and code directly interfacing to the transport layer are isolated in separate modules that format, transmit, and receive messages and data packets.

This design makes it possible for different targets to use different transport layers. The GRT, GRT malloc, ERT, and RSim targets support host/target communication by using TCP/IP and RS-232 (serial) communication. The RTWin target supports shared memory communication. The Wind River Systems Tornado target supports TCP/IP only. Serial transport is implemented only for Microsoft Windows architectures.

The Simulink Coder product provides full source code for both the client and server-side external mode modules, as used by the GRT, GRT malloc, ERT, Rapid Simulation, and Tornado targets, and the Real-Time Windows Target and xPC Target products. The main client-side module is ext_comm.c. The main server-side module is ext_svr.c.

These two modules call the specified transport layer through the following source files.

Built-In Transport Layer Implementations

Protocol

Client or Server?

Source Files

TCP/IP

Client (host)

  • matlabroot/rtw/ext_mode/common/rtiostream_interface.c

  • matlabroot/rtw/c/src/rtiostream/rtiostreamtcpip/rtiostream_tcpip.c

 

Server (target)

  • matlabroot/rtw/c/src/ext_mode/common/rtiostream_interface.c

  • matlabroot/rtw/c/src/rtiostream/rtiostreamtcpip/rtiostream_tcpip.c

Serial

Client (host)

  • matlabroot/rtw/ext_mode/serial/ext_serial_transport.c

  • matlabroot/rtw/c/src/rtiostream/rtiostreamserial/rtiostream_serial.c

 

Server (target)

  • matlabroot/rtw/c/src/ext_mode/serial/ext_svr_serial_transport.c

  • matlabroot/rtw/c/src/rtiostream/rtiostreamserial/rtiostream_serial.c

For serial communication, the modules ext_serial_transport.c and rtiostream_serial.c implement the client-side transport functions and the modules ext_svr_serial_transport.c and rtiostream_serial.c implement the corresponding server-side functions. For TCP/IP communication, the modules rtiostream_interface.c and rtiostream_tcpip.c implement both client-side and server-side functions. You can edit copies of these files (but do not modify the originals). You can support external mode using your own low-level communications layer by creating similar files using the following templates:

The file rtiostream_interface.c is an interface between the external mode protocol and an rtiostream communications channel. For more details on implementing an rtiostream communications channel, see Communications rtiostream API in the Embedded Coder documentation. Implement your rtiostream communications channel by using the documented interface to avoid having to make changes to the file rtiostream_interface.c or any other external mode related files.

You need only provide code that implements low-level communications. You need not be concerned with issues such as data conversions between host and target, or with the formatting of messages. The Simulink Coder software handles these functions.

On the client (Simulink engine) side, communications are handled by ext_comm (for TCP/IP) and ext_serial_win32_comm (for serial) MEX-files.

On the server (target) side, external mode modules are linked into the target executable. This takes place automatically if the External mode code generation option is selected at code generation time, based on the External mode transport option selected in the target code generation options dialog box. These modules, called from the main program and the model execution engine, are independent of the generated model code.

The general procedure for implementing your own client-side low-level transport protocol is as follows:

  1. Edit the template rtiostream_tcpip.c to replace low-level communication calls with your own communication calls.

  2. Generate a MEX-file executable for your custom transport.

  3. Register your new transport layer with the Simulink software, so that the transport can be selected for a model using the Interface pane of the Configuration Parameters dialog box.

For more details, see Create a Custom Client (Host) Transport Protocol.

The general procedure for implementing your own server-side low-level transport protocol is as follows:

  1. Edit the template rtiostream_tcpip.c to replace low-level communication calls with your own communication calls. Typically this involves writing or integrating device drivers for your target hardware.

  2. Modify template makefiles to support the new transport.

For more details, see Create a Custom Server (Target) Transport Protocol for TCP/IP Communication or Create a Custom Server (Target) Transport Protocol.

External Mode Communications Overview

This section gives a high-level overview of how a Simulink Coder generated program communicates with Simulink external mode. This description is based on the TCP/IP version of external mode that ships with the Simulink Coder product.

For communication to take place,

The client and server communicate by using bidirectional sockets carrying packets. Packets consist either of messages (commands, parameter downloads, and responses) or data (signal uploads).

If the target program was invoked with the -w command-line option, the program enters a wait state until it receives a message from the host. Otherwise, the program begins execution of the model. While the target program is in a wait state, the Simulink engine can download parameters to the target and configure data uploading.

When the user chooses the Connect to target option from the Simulation menu, the host initiates a handshake by sending an EXT_CONNECT message. The server responds with information about itself. This information includes

At this point, host and server are connected. The server is either executing the model or in the wait state. (In the latter case, the user can begin model execution by selecting Start real-time code from the Simulation menu.)

During model execution, the message server runs as a background task. This task receives and processes messages such as parameter downloads.

Data uploading comprises both foreground execution and background servicing of the signal packets. As the target computes model outputs, it also copies signal values into data upload buffers. This occurs as part of the task associated with each task identifier (tid). Therefore, data collection occurs in the foreground. Transmission of the collected data, however, occurs as a background task. The background task sends the data in the collection buffers to the Simulink engine by using data packets.

The host initiates most exchanges as messages. The target usually sends a response confirming that it has received and processed the message. Examples of messages and commands are

Model execution terminates when the model reaches its final time, when the host sends a terminate command, or when a Stop Simulation block terminates execution. On termination, the server informs the host that model execution has stopped, and shuts down its socket. The host also shuts down its socket, and exits external mode.

External Mode Source Files

Client (Host) MEX-file Interface Source Files

The source files for the MEX-file interface component are located in the folder matlabroot/rtw/ext_mode, except as noted:

Server (Target) Source Files

These files are part of the run-time interface and are linked into the model.exe executable. They are located within matlabroot/rtw/c/src/ext_mode/ except as noted.

Other Files in the Server Folder

Implement a Custom Transport Layer

Requirements for Custom Transport Layers

Create a Custom Client (Host) Transport Protocol

To implement the client (host) side of your low-level transport protocol,

  1. Edit the template file matlabroot/rtw/c/src/rtiostream/rtiostreamtcpip/rtiostream_tcpip.c to replace low-level communication calls with your own communication calls.

    1. Copy and rename the file to rtiostream_name.c (replacing name with a name meaningful to you).

    2. Replace the functions rtIOStreamOpen, rtIOStreamClose, rtIOStreamSend, and rtIOStreamRecv with functions (of the same name) that call your low-level communication primitives. These functions are called from other external mode modules via rtiostream_interface.c. For more information, see Communications rtiostream API in the Embedded Coder documentation.

  2. Build the customized MEX-file executable using the MATLAB mex function. See the table MATLAB Commands to Rebuild ext_comm and ext_serial_win32 MEX-Files for examples of mex invocations.

    Replace the command-line entry rtiostream_tcpip.c with your custom component's filename.

    Do not replace the existing ext_comm MEX-file if you want to preserve its existing function. Instead, use the -output option to name the resulting executable (for example, mex -output ext_myrtiostream_comm ... builds ext_myrtiostream_comm.mexext, on Windows platforms).

  3. Register your new client transport layer with the Simulink software, so that the transport can be selected for a model using the Interface pane of the Configuration Parameters dialog box. For details, see Register a Custom Client (Host) Transport Protocol.

The following table lists the commands for building the standard ext_comm module on PC and UNIX platforms, and for building the standard ext_serial_win32 model on a PC platform.

MATLAB Commands to Rebuild ext_comm and ext_serial_win32 MEX-Files

Platform

Commands

UNIX, TCP/IP

>> cd (matlabroot)
>> mex rtw/ext_mode/common/ext_comm.c ...
rtw/ext_mode/common/ext_convert.c ...
rtw/ext_mode/common/rtiostream_interface.c ...
rtw/c/src/rtiostream/rtiostreamtcpip/rtiostream_tcpip.c ...
-Irtw/c/src -Iextern/include -Irtw/c/src/rtiostream/utils ...
-Irtw/c/src/ext_mode/common -Irtw/ext_mode/common ...
-Irtw/ext_mode/common/include -DSL_EXT_SO -ldl ...
-output toolbox/rtw/rtw/ext_comm  -lut -lmwrtiostreamutils ...
-DEXTMODE_TCPIP_TRANSPORT

PC, TCP/IP

>> cd (matlabroot)
>> mex rtw\ext_mode\common\ext_comm.c ...
rtw\ext_mode\common\ext_convert.c ...
rtw\ext_mode\common\rtiostream_interface.c ...
rtw\c\src\rtiostream\rtiostreamtcpip\rtiostream_tcpip.c ...
-Irtw\c\src -Iextern\include -Irtw\c\src\rtiostream\utils ...
-Irtw\c\src\ext_mode\common -Irtw\ext_mode\common ...
-Irtw\ext_mode\common\include -DSL_EXT_DLL ...
-DWIN32 wsock32.lib -output toolbox\rtw\rtw\ext_comm ...
-Lextern\lib\win32\microsoft -lut -lmwrtiostreamutils ...
-DEXTMODE_TCPIP_TRANSPORT

    Note   The argument -Lextern\lib\win32\microsoft is compiler specific. For example, if you are using the LCC compiler, you must replace this argument with -Lextern\lib\win32\lcc.

PC, serial

>> cd (matlabroot)
>> mex rtw\ext_mode\common\ext_comm.c ...
rtw\ext_mode\common\ext_convert.c ...
rtw\ext_mode\serial\ext_serial_transport.c ...
rtw\c\src\ext_mode\serial\ext_serial_pkt.c ...
rtw\c\src\ext_mode\serial\rtiostream_serial_interface.c ...
rtw\c\src\rtiostream\rtiostreamserial\rtiostream_serial.c ...
-Irtw\ext_mode\common -Irtw\c\src -Iextern\include ...
-Irtw\c\src\ext_mode\common -Irtw\c\src\ext_mode\serial ...
-Isimulink\include -Irtw\c\src\rtiostream\utils ...
-output toolbox\rtw\rtw\ext_serial_win32_comm -DWIN32

Register a Custom Client (Host) Transport Protocol

To register a custom client transport protocol with the Simulink software, you must add an entry of the following form to an sl_customization.m file on the MATLAB path:

function sl_customization(cm)
  cm.ExtModeTransports.add('stf.tlc', 'transport', 'mexfile', 'Level1');
% -- end of sl_customization

where

You can specify multiple targets and/or transports with additional cm.ExtModeTransports.add lines, for example:

function sl_customization(cm)
  cm.ExtModeTransports.add('grt.tlc', 'mytcpip', 'ext_mytcpip_comm', 'Level1');
  cm.ExtModeTransports.add('ert.tlc', 'mytcpip', 'ext_mytcpip_comm', 'Level1');
% -- end of sl_customization

If you place the sl_customization.m file containing the transport registration information on the MATLAB path, your custom client transport protocol will be registered with each subsequent Simulink session. The name of the transport will appear in the Transport layer menu on the Interface pane of the Configuration Parameters dialog box. When you select the transport for your model, the name of the associated external interface MEX-file will appear in the noneditable MEX-file name field, as shown in the following figure.

Create a Custom Server (Target) Transport Protocol for TCP/IP Communication

The rtIOStream* function prototypes in matlabroot/rtw/c/src/rtiostream.h define the calling interface for both the server (target) and client (host) side transport layer functions. The TCP/IP implementations are in matlabroot/rtw/c/src/rtiostream/rtiostreamtcpip/rtiostream_tcpip.c.

To implement the server (target) side of your low-level TCP/IP transport protocol,

  1. Edit the template matlabroot/rtw/c/src/rtiostream/rtiostreamtcpip/rtiostream_tcpip.c to replace low-level communication calls with your own communication calls.

    1. Copy and rename the file to rtiostream_name.c (replacing name with a name meaningful to you).

    2. Replace the functions rtIOStreamOpen, rtIOStreamClose, rtIOStreamSend,and rtIOStreamRecv with functions (of the same name) that call your low-level communication drivers.

      You must implement all the functions defined in rtiostream.h, and your implementations must conform to the prototypes defined in that file. Refer to the original rtiostream_tcpip.c for guidance.

  2. Modify template makefiles to support the new transport. If you are writing your own template makefile, make sure that the EXT_MODE code generation option is defined. The generated makefile will then link rtiostream_name.c, rtiostream_interface.c, and other server code into your executable.

Create a Custom Server (Target) Transport Protocol for Serial Communication

The rtIOStream* function prototypes in matlabroot/rtw/c/src/rtiostream.h define the calling interface for both the server (target) and client (host) side transport layer functions. The serial implementations are in matlabroot/rtw/c/src/rtiostream/rtiostreamserial/rtiostream_serial.c.

To implement the server (target) side of your low-level serial transport protocol:

  1. Edit the template matlabroot/rtw/c/src/rtiostream/rtiostreamserial/rtiostream_serial.c to replace low-level communication calls with your own communication calls.

    1. Copy and rename the file to rtiostream_name.c (replacing name with a name meaningful to you).

    2. Replace the functions rtIOStreamOpen, rtIOStreamClose, rtIOStreamSend,and rtIOStreamRecv with functions (of the same name) that call your low-level communication drivers.

      You must implement all the functions defined in rtiostream.h, and your implementations must conform to the prototypes defined in that file. Refer to the original rtiostream_serial.c for guidance.

  2. Modify template makefiles to support the new transport. If you are writing your own template makefile, make sure that the EXT_MODE code generation option is defined. The generated makefile will then link rtiostream_name.c, rtiostream_serial_interface.c, and other server code into your executable.

Create a Custom Server (Target) Transport Protocol

The Ext* function prototypes in matlabroot/rtw/c/src/ext_mode/common/ext_svr_transport.h define the calling interface for the server (target) side transport layer functions.

To implement the server (target) side of your low-level transport protocol,

  1. Edit the template matlabroot/rtw/c/src/ext_mode/custom/ext_svr_custom_transport.c to replace low-level communication calls with your own communication calls.

    1. Copy and rename the file to ext_svr_name_transport.c (replacing name with a name meaningful to you).

    2. Replace the functions in the VISIBLE FUNCTIONS section with functions that call your low-level communication primitives. These are the functions called from other target modules such as the main program.

      You must implement all the functions defined in ext_svr_transport.h, and your implementations must conform to the prototypes defined in that file.

    3. Supply a definition for the ExtUserData structure. This structure is required. If your external mode implementation does not use ExtUserData, define an ExtUserData structure with one dummy field.

    4. Define the EXT_BLOCKING conditional for your implementation:

      • Define EXT_BLOCKING as 0 to poll for a connection to the host (for single-threaded applications).

      • Define EXT_BLOCKING as 1 in multithreaded applications where tasks are able to block for a connection to the host without blocking the entire program.

      See also the comments on EXT_BLOCKING in ext_svr_custom_transport.c.

    The ext_svr_*_transport source code modules are fully commented. See those files for more details.

  2. If you created an ext_name_utils.c file to define custom transport symbols and functions (see step 2 of Create a Custom Client (Host) Transport Protocol), and if the file is used by your server side protocol, include it in your custom server transport source file.

  3. Modify template makefiles to support the new transport. If you are writing your own template makefile, make sure that the EXT_MODE code generation option is defined. The generated makefile will then link ext_svr_name_transport.c and other server code into your executable.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS