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. This example uses a target computer located at IP address and uses a development computer located at IP address

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.

Create Target Object and Connect

Create a Target object for the default target computer and connect to the target computer. In the Command Window, type:

tg = slrealtime;

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:

model = 'slrt_ex_target_to_host_UDP';
modelOpened = 0;
systems = find_system('type', 'block_diagram');
if ~any(strcmp(model, systems))
  modelOpened = 1;
modelSTF = getSTFName(tg);

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.

targetIP = '';
set_param([model,'/UDP Receive'],'ipAddress',targetIP);
hostIP = '';
set_param([model,'/UDP Send'],'toAddress',hostIP)
set_param([model,'/UDP Receive'],'fmAddress',hostIP)
handle = get_param([model,'/Byte Unpacking '],'PortHandles');
Outport = handle.Outport(1);

Close the model if it is opened.

if (modelOpened)

Create UDP object in MATLAB on Development Computer

uByte = udpport("IPV4","LocalHost",hostIP,"LocalPort",8002);

Run Model on Target Computer


Read Data and Write Development Computer

while (toc<10)
    data = read(uByte,16);
    data = read(uByte,16);

View Signals in Simulation Data Inspector


Disconnect UDP Object on Development Computer

clear uByte;