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
Error using matlabshared.asyncio.internal.MessageHandler/onError An established connection was aborted by the software in your host machine Error in matlabshared.asyncio.internal.Stream/wait (line 184) drawnow('limitrate'); Error in matlabshared.asyncio.internal.OutputStream/drain (line 206) status = obj.wait(@(obj) obj.getDataAvailable() == 0); Error in matlabshared.transportlib.internal.asyncIOTransportChannel.AsyncIOTransportChannel/write (line 222) obj.AsyncIOChannel.OutputStream.drain(); Error in matlabshared.network.internal.TCPClient/write (line 957) obj.TransportChannel.write(varargin{2:end}); Error in matlab.internal.tcpclient.TCPCustomClient/write (line 147) write(obj.Transport,data); Error in tcpclient/write (line 595) write(obj.TCPCustomClient,varargin{:}); Error in SlrtTargetToHostCommunicationUsingTCPExample (line 59) write(t,data); Error in evalmxdom>instrumentAndRun (line 116) text = evalc(evalstr); Error in evalmxdom (line 21) [data,text,laste] = instrumentAndRun(file,cellBoundaries,imageDir,imagePrefix,options); Error in publish (line 213) dom = evalmxdom(file,dom,cellBoundaries,prefix,imageDir,outputDir,options); Error in publishMainFile (line 19) publish(metadata.main,opts); Error in examples.previewExampleInSandbox (line 79) publishMainFile(metadata, metadata.filepath); Error in examples.connector.previewExampleInSandbox (line 3) examples.previewExampleInSandbox(id); Error using matlabshared.asyncio.internal.MessageHandler/onError An established connection was aborted by the software in your host machine Error in matlabshared.asyncio.internal.Stream/wait (line 184) drawnow('limitrate'); Error in matlabshared.asyncio.internal.OutputStream/drain (line 206) status = obj.wait(@(obj) obj.getDataAvailable() == 0); Error in matlabshared.transportlib.internal.asyncIOTransportChannel.AsyncIOTransportChannel/write (line 222) obj.AsyncIOChannel.OutputStream.drain(); Error in matlabshared.network.internal.TCPClient/write (line 957) obj.TransportChannel.write(varargin{2:end}); Error in matlab.internal.tcpclient.TCPCustomClient/write (line 147) write(obj.Transport,data); Error in tcpclient/write (line 595) write(obj.TCPCustomClient,varargin{:}); Error in SlrtTargetToHostCommunicationUsingTCPExample (line 59) write(t,data); Error in evalmxdom>instrumentAndRun (line 116) text = evalc(evalstr); Error in evalmxdom (line 21) [data,text,laste] = instrumentAndRun(file,cellBoundaries,imageDir,imagePrefix,options); Error in publish (line 213) dom = evalmxdom(file,dom,cellBoundaries,prefix,imageDir,outputDir,options); Error in publishMainFile (line 19) publish(metadata.main,opts); Error in examples.previewExampleInSandbox (line 79) publishMainFile(metadata, metadata.filepath); Error in examples.connector.previewExampleInSandbox (line 3) examples.previewExampleInSandbox(id);
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();