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,
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:
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; open_system('TargetToHostUDPTx'); end
% Build the model and download to the target computer. set_param('TargetToHostUDPTx','RTWVerbose','off'); % Configure for a non-Verbose build. rtwbuild('TargetToHostUDPTx'); % Build and download application.
### Starting Simulink Real-Time build procedure for model: TargetToHostUDPTx ### Generated code for 'TargetToHostUDPTx' is up to date because no structural, parameter or code replacement library changes were found. ### Successful completion of build procedure for model: TargetToHostUDPTx ### Looking for target: TargetPC1 ### Download model onto target: TargetPC1
% Close the model if we opened it. if (mdlOpened) bdclose('TargetToHostUDPTx'); end
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; open_system('TargetToHostUDPRx'); 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
yout = yout.signals.values; subplot(2,1,1); plot(tout, yout(:,1:2)); ylabel('Data'); subplot(2,1,2); plot(tout, yout(:,3)); ylim([0 16]) ylabel('N'); 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.