Main Content

Target to Development Computer Communication by Using TCP

This example shows how to use TCP blocks to send data from the target computer to MATLAB® running on the development computer. This example uses a target computer located at IP address 192.168.7.5.

The TCP Send block in the server real-time application slrt_ex_target_to_host_TCP sends data from the target computer to the TCP/IP object that is created in MATLAB on the development computer. The MATLAB m-script sends the received data back to the real-time application.

To open this example, in the MATLAB Command Window, type:

open_system('slrt_ex_target_to_host_TCP')

Open, Build, and Download Server Application

Open the model.

tg = slrealtime;
connect(tg);
model = 'slrt_ex_target_to_host_TCP';
open_system(model);

Build Model and Download to Target Computer

modelSTF = getSTFName(tg);
set_param(model,"SystemTargetFile",modelSTF);
set_param(model, 'RTWVerbose', 'off');
set_param(model, 'StopTime','10');
targetIP = '192.168.7.5';
set_param([model,'/TCP Server'],'serverAddress',targetIP);
evalc('slbuild(model)');
load(tg,model);

Run Real-Time Application on Target Computer

start(tg);
pause(3);

Create TCP/IP Object in MATLAB on Development Computer

After starting the real-time application, create a TCP/IP object and connect the TCP/IP object to the development computer.

t = tcpclient(targetIP,5027);

Read Data Packets and Send Back to Target Computer

Read from the target computer and write back.

tic
while (toc<5)
   data = read(t,16);
   write(t,data);
end

Stop Real-Time Application on Target Computer

stop(tg);

Close TCP/IP Object on Development Computer

clear t;

View Signal Received on Target Computer

Simulink.sdi.view();

Close the Model

bdclose(model);