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.

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.

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

Build Model and Download to Target Computer

set_param(mdl, 'RTWVerbose', 'off');
set_param(mdl, 'StopTime','10');
targetIP = '10.10.10.35';
set_param([mdl,'/TCP Server'],'serverAddress',targetIP);
rtwbuild(mdl);
tg = slrealtime;
load(tg,mdl);
### Successful completion of build procedure for: slrt_ex_target_to_host_TCP
### Created MLDATX ..\slrt_ex_target_to_host_TCP.mldatx

Build Summary

Top model targets built:

Model                       Action                       Rebuild Reason                  
=========================================================================================
slrt_ex_target_to_host_TCP  Code generated and compiled  Global variables have changed.  

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 37.964s

Close the model.

if (mdlOpen)
  bdclose(mdl);
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 = tcpip(targetIP,5027);
t.BytesAvailableFcnMode = 'byte';
t.BytesAvailableFcnCount = 16;
fopen(t);

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.

while (strcmp(t.Status,'open'))
   data = fread(t,16);
   fwrite(t,data);
end

Stop Real-Time Application on Target Computer

stop(tg);

Close TCP/IP Object on Development Computer

fclose(t);
delete(t);
clear t;

View Signal Received on Target Computer

Simulink.sdi.view();