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 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:
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(fullfile(matlabroot,'toolbox','rtw','targets','xpc','xpcdemos','TargetToHostUDPTx')); end
Build the model and download to the target computer.
Configure for a non-Verbose build.
Configure UDP Send block for development computer address.
Mark the Signal Generator and Transfer Function 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('TargetToHostUDPTx','RTWVerbose','off'); set_param('TargetToHostUDPTx/Send','toAddress','10.10.10.128'); hSigGen = get_param('TargetToHostUDPTx/Signal Generator','PortHandles'); SigGen = hSigGen.Outport(1); Simulink.sdi.markSignalForStreaming(SigGen,'on'); hTranFun = get_param('TargetToHostUDPTx/Transfer Function','PortHandles'); TranFun = hTranFun.Outport(1); Simulink.sdi.markSignalForStreaming(TranFun,'on'); rtwbuild('TargetToHostUDPTx'); Simulink.sdi.view;
### 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) bdclose('TargetToHostUDPTx'); end
Click here to open the Rx model:
TargetToHostUDPRx. This model receives data sent by
mdlOpened = 0; systems = find_system('type', 'block_diagram'); if ~any(strcmp('TargetToHostUDPRx', systems)) mdlOpened = 1; open_system(fullfile(matlabroot,'toolbox','rtw','targets','xpc','xpcdemos','TargetToHostUDPRx')); end
Before running this model:
Configure UDP Send block for target computer address.
Mark the Receive block Data and Length outputs for data logging.
set_param('TargetToHostUDPRx/Receive','fmAddress','10.10.10.15'); hUdpReceiveData = get_param('TargetToHostUDPRx/Receive','PortHandles'); UdpReceiveData = hUdpReceiveData.Outport(1); Simulink.sdi.markSignalForStreaming(UdpReceiveData,'on'); hUdpReceiveLength = get_param('TargetToHostUDPRx/Receive','PortHandles'); UdpReceiveLength = hUdpReceiveLength.Outport(2); Simulink.sdi.markSignalForStreaming(UdpReceiveLength,'on');
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.
Open the Simulation Data Inspector
start(tg); pause(1); set_param(bdroot,'SimulationCommand','start'); pause(5); while ~strcmpi(get_param(bdroot,'SimulationStatus'),'stopped') pause(1); end Simulink.sdi.view; stop(tg);
The signal that the development computer receives
Receive:2 does not look exactly like the signal that the target computer sent
Transfer Function:1. On the development computer, the model does not run in real-time. The model on the development computer 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, the
Data output values are sometimes held from the previous packet that the block received by the development computer model. You can use the second output
Receive:2 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 second output
Receive:2 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.