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 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
set_param(mdl, 'RTWVerbose', 'off'); set_param(mdl, 'StopTime','10'); targetIP = '192.168.7.5'; set_param([mdl,'/TCP Server'],'serverAddress',targetIP); slbuild(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 variable C has changed. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 28.66s
if (mdlOpen) bdclose(mdl); end
Create a TCP/IP object and connect the TCP/IP object to the development computer.
t = tcpclient(targetIP,5027);
Read from the target computer and write back.
tic while (toc<5) data = read(t,16); write(t,data); end