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,
dxpcUDP1TgtTx, to the receiver model running in Simulink® on the development computer,
Note: When considering UDP as a protocol for communicating data to/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:
Click here to open the Tx model:
dxpcUDP1TgtTx. 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('dxpcUDP1TgtTx', systems)) mdlOpened = 1; open_system('dxpcUDP1TgtTx'); end
% Build the model and download to the target computer. set_param('dxpcUDP1TgtTx','RTWVerbose','off'); % Configure for a non-Verbose build. rtwbuild('dxpcUDP1TgtTx'); % Build and download application.
### Starting Simulink Real-Time build procedure for model: dxpcUDP1TgtTx 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: dxpcUDP1TgtTx ### Looking for target: TargetPC1 ### Download model onto target: TargetPC1
% Close the model if we opened it. if (mdlOpened) bdclose('dxpcUDP1TgtTx'); end
Click here to open the Rx model:
dxpcUDP1HostRx. This model receives data sent by
dxpcUDP1TgtTx and unpacks the data for display in a scope.
mdlOpened = 0; systems = find_system('type', 'block_diagram'); if ~any(strcmp('dxpcUDP1HostRx', systems)) mdlOpened = 1; open_system('dxpcUDP1HostRx'); end
Start model on target computer followed by model on development computer.
start(tg); % Start the target computer Tx model. pause(1); % Wait for 1 sec. set_param(bdroot,'SimulationCommand','start');% Start the development computer Rx model. pause(5); % Wait for 5 sec. while ~strcmpi(get_param(bdroot,'SimulationStatus'),'stopped') pause(1); % Wait another 1 sec. end
subplot(2,1,1); plot(tout, yout(:,1:2)); ylabel('Data'); subplot(2,1,2); plot(tout, yout(:,3)); ylim([0 2]) ylabel('N'); xlabel('Time (s)');
The received signal on the host does not look exactly like the signal that was sent from the target. The host 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 may vary depending on computer load. Therefore, data outputs may just be values held from the previous packet received. The second output 'N' of the UDP Receive Binary block may be used to detect the presence of a new packet. In the development computer plot it can be seen that whenever there is a new packet, the 'N' output goes to 1. When it is 0 the data output is held to the value from the previous packet received.