MATLAB/Simulink TCP/IP Communication MARTIN SYSEL Department of Computer and Communication Systems Faculty of Applied Informatics Tomas Bata University in Zlín nám. T. G. Masaryka 5555, 760 01 Zlín CZECH REPUBLIC Sysel@fai.utb.cz Abstract: - This paper describes TCP/IP communication blocks in the program MATLAB/Simulink. The new developed Simulink blocks and detailed instructions for building them are described here. This server and client blocks enable Simulink models to communicate with remote applications, devices over TCP/IP communications. A very similar functionality (more complex) is provided by the TCP/IP block in the Instrument Control Toolbox offered by MathWorks as closed-source software. Key-Words: - Simulink, communications, TCP/IP, client, server 1 Introduction Simulink can communicate with remote applications using developed Simulink blocks. Client block enables sending live data from Simulink model to an application, devices using TCP/IP. It is possible to send data to the TCP/IP Server block. The server block accepts data from the network socket; it uses TCP/IP protocol and blocking mode. The data are received at fixed intervals based on Simulink schema loop. The base element of the block is S-function block, which use C MEX file [3]. To create S-functions, it is needed to understand how S- functions work. It requires understanding how the Simulink engine simulates a model. The first, Simulink model stages and callback methods are described, then Windows Socket API implementation is briefly presented and finally the procedure how to create both communication blocks is described. 2 S-Function S-functions (system-functions) provide a powerful mechanism for extending the capabilities of the Simulink environment. An S-function is a computer language description of a Simulink block written in MATLAB, C, C++, Ada, or Fortran. S-functions are compiled as MEX-files using the MEX utility. S- functions are dynamically linked subroutines that the MATLAB interpreter can automatically load and execute. S-functions use a special calling syntax called the S-function API that enables to interact with the Simulink engine. This interaction is very similar to the interaction that takes place between the engine and built-in Simulink blocks. S-functions follow a general form and can accommodate continuous, discrete and hybrid systems. By following a set of simple rules, it can be implemented an algorithm in an S-function and used the S-Function block to add it to a Simulink model. After writing S-function and place its name in an S- Function block (available in the User-Defined Functions block library), it can customize the user interface using masking [1]. 2.1 S-function Simulation Stages Execution of a Simulink model proceeds in stages. First comes the initialization phase. In this phase, the Simulink engine incorporates library blocks into the model, propagates signal widths, data types, and sample times, evaluates block parameters, determines block execution order, and allocates memory. The engine then enters a simulation loop, where each pass through the loop is referred to as a simulation step. During each simulation step, the engine executes each block in the model in the order determined during initialization. For each block, the engine invokes functions that compute the block states, derivatives, and outputs for the current sample time. The entire simulation loop then continues until the simulation is complete. A MEX S-function consists of a set of callback methods that the Simulink engine invokes to perform various block related tasks during a simulation. Because the engine invokes the functions directly, MEX S-functions must follow Recent Researches in Computer Science ISBN: 978-1-61804-019-0 71