MATLAB Examples

Target to Host Transmission using UDP

This example shows how to use UDP blocks to send data from a target computer to a development computer. Signal data are sent by the transmit model running on the target computer, TargetToHostUDPTx, to the receiver model running in Simulink® on the development computer, TargetToHostUDPRx.

Note: When considering UDP as a protocol for communicating data to or from the Simulink Real-Time™ environment it is important to be aware of the following:

  • The Simulink model on the development computer is running as fast as it can and is therefore 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 and must therefore share bandwidth with the Ethernet link.
  • For more information on using UDP with Simulink Real-Time, see the documentation on UDP I/O support.

Click here to open this example: TargetToHostUDP.


Open, Build, and Download the Target Computer Model

Click here to open the Tx model: TargetToHostUDPTx. 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.

Open the model.

mdlOpened = 0;
systems = find_system('type', 'block_diagram');
if ~any(strcmp('TargetToHostUDPTx', systems))
  mdlOpened = 1;

Build the model and download to the target computer.

  • Configure for a non-Verbose build.
  • Build and download application.
### Starting Simulink Real-Time build procedure for model: TargetToHostUDPTx
Warning: This model contains blocks that do not handle sample time
changes at runtime. To avoid incorrect results, only change
the sample time in the original model, then rebuild the model. 
### Successful completion of build procedure for model: TargetToHostUDPTx
### Created MLDATX ..\TargetToHostUDPTx.mldatx
### Looking for target: TargetPC1
### Download model onto target: TargetPC1

Close the model if we opened it.

if (mdlOpened)

Open the development computer Model

Click here to open the Rx model: TargetToHostUDPRx. This model receives data sent by TargetToHostUDPTx and unpacks the data for display in a scope.

mdlOpened = 0;
systems = find_system('type', 'block_diagram');
if ~any(strcmp('TargetToHostUDPRx', systems))
  mdlOpened = 1;

Run Both Models

Start model on target computer followed by model on development computer.

  • Start the target computer Tx model.
  • Wait for 1 sec.
  • Start the development computer Rx model.
  • Wait for 5 sec.
  • Wait another 1 sec.
while ~strcmpi(get_param(bdroot,'SimulationStatus'),'stopped')

Generate target computer plot


Generate development computer plots

yout = yout.signals.values;
plot(tout, yout(:,1:2));
plot(tout, yout(:,3));
ylim([0 16])
xlabel('Time (s)');

The signal that the development computer receives does not look exactly like the signal that the target computer sent. On the development computer, the model does not run in real-time; for this model, it actually runs faster than real-time. Additionally, it does not run at constant intervals, and the number of steps processed per second varies depending on computer load. Therefore, data outputs can be values held from the previous packet that the block received. You can use the second output 'N' of the UDP Receive Binary block to detect the presence of a new packet. The development computer plot shows that whenever there is a new packet, the 'N' output goes to a non-zero value indicating the number of bytes received. When it is 0, the data output remains the value from the previous packet that the block received.