Main Content

Target to Host Transmission by Using UDP

This example shows how to use UDP blocks to send data from a target computer to a development computer. The transmit real-time application slrt_ex_target_to_host_UDP runs on the target computer and send signal data to the UDP object that the script creates in MATLAB on the development computer.

When using the UDP protocol for communicating data to or from the target computer, consider these issues:

  • The Simulink model on the development computer runs as fast as it can. The model run speed is not synchronized to a real-time clock.

  • UDP is a connectionless protocol that does not check to confirm that packets were transmitted. Data packets can be lost or dropped.

  • On the target computer, UDP blocks run in a background task that executes each time step after the real-time task completes. If the block cannot run or complete the background task before the next time step, data may not be communicated.

  • UDP data packets are transmitted over the Ethernet link between the development and target computers. These transmissions share bandwidth with the Ethernet link.

For more information about UDP and Simulink Real-Time, see UDP Communication Setup.

Open Model, Build, and Load Real-Time Application

This model drives a first order transfer function with a square wave signal and sends the transfer function input and output signals to the development computer using UDP. To open the model, in the MATLAB Command Window, type:

open_system(fullfile(matlabroot,'toolbox','slrealtime','examples','slrt_ex_target_to_host_UDP'));

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

Build the model and download to the target computer.

  • Configure for a non-Verbose build.

  • Mark the Byte Unpacking block output for data logging.

  • Build and download application.

  • Open the Simulation Data Inspector.

This code shows how to mark signals programmatically for data logging. You can also mark signals for data logging in the Simulink Editor. You can view the logged data in in the Simulation Data Inspector.

set_param(mdl,'RTWVerbose','off');
set_param(mdl,'StopTime','10');
targetIP = '10.10.10.35';
set_param([mdl,'/UDP Receive'],'ipAddress',targetIP);
hostIP = '10.10.10.128';
set_param([mdl,'/UDP Send'],'toAddress',hostIP)
set_param([mdl,'/UDP Receive'],'fmAddress',hostIP)
handle = get_param([mdl,'/Byte Unpacking '],'PortHandles');
Outport = handle.Outport(1);
Simulink.sdi.markSignalForStreaming(Outport,'on');
rtwbuild(mdl);
tg = slrealtime;
load(tg,mdl);
### Successful completion of build procedure for: slrt_ex_target_to_host_UDP
### Created MLDATX ..\slrt_ex_target_to_host_UDP.mldatx

Build Summary

Top model targets built:

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

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

Close the model if it is opened.

if (mdlOpened)
  bdclose(mdl);
end

Create UDP object in MATLAB on Development Computer

udpObj = udp('','LocalHost',hostIP,'LocalPort',8002,'RemoteHost',targetIP,'RemotePort',25000);
udpObj.InputBufferSize = 16;
fopen(udpObj);

Run Model on Target Tomputer

start(tg);

Read Data and Write Development Computer

[data,count,errmsg] = fread(udpObj,16);
while count~=0
    fwrite(udpObj,data);
    [data,count,errmsg] = fread(udpObj,16);
end

View Signals in Simulation Data Inspector

Simulink.sdi.view;

Disconnect UDP Object on Development Computer

fclose(udpObj);