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(fullfile(matlabroot,'toolbox','slrealtime','examples','slrt_ex_target_to_host_TCP'))

Open, Build, and Download Server Application

Open the model.

model = 'slrt_ex_target_to_host_TCP';
mdlOpen = 0;
systems = find_system('type', 'block_diagram');
if ~any(strcmp(model, systems))
    mdlOpen = 1;
    open_system(fullfile(matlabroot,'toolbox','slrealtime','examples',model));
end

Build Model and Download to Target Computer

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)');
tg = slrealtime;
load(tg,model);

Close the model.

if (mdlOpen)
  bdclose(model);
end

Create TCP/IP Object in MATLAB on Development Computer

Create a TCP/IP object and connect the TCP/IP object to the development computer.

t = tcpclient(targetIP,5027);

Run Real-Time Application on Target Computer

start(tg);
pause(3);

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();