xPC Target | ![]() ![]() |
UDP Example
In this section, we provide an example of how to set up two way data exchange between two xPC Target systems, between xPC Target and Simulink, or between two Simulink models. When one or both of the systems is running Simulink in non real-time, care must be taken to set the sample time properly.
Our hypothetical models are called Model A and Model B. Two different sets of data are transferred between these two models. One set from Model A to
Model B and another set in the opposite direction.
The data to transfer is in the following order:
For the purposes of this example, all the inputs are generated using Simulink Constant blocks that use the MATLAB random number function (rand). The random numbers are generated by Real Time Workshop using this function at the time of code generation. To generate the vector of uint8
(3x3), use the MATLAB function.
since 255 is the maximum value for an unsigned 8-bit integer. The other values are generated similarly.
With this setup, construct the send side of modelA.
Note that Port Data Types and Signal dimensions have been turned on from the Format menu, showing us that the width of the UDP packet to be sent is 75 bytes. The parameters used in the Pack block are Input port datatypes {'uint8','int16','double'}
and Byte Alignment 1.
For the Send block, set the IP Address to send to to 192.168.0.2
. This is the hypothetical address of the system that will run Model B. Set the IP Port to send to to 25000
(picked arbitrarily). The sample time is set to 0.01
.
Use this information to construct the receive end of Model B.
For setting up the Receive block, IP address to receive from is set to 192.168.0.1
(the hypothetical address of the system that will run Model B). The IP port to receive from is set to 25000
(the same value as set in the Send block in Model A). The Output port width is set to 75
which is obtained from the output port width of the Pack block in Model A.
For the Unpack block, Byte Alignment is set to 1
, and the Output port datatypes is set to {'uint8','int16','double'}
from the Pack block in Model A. The Output port dimensions is set to {[3 3],1,[2 4]}
from the dimensions of the inputs to the Pack block in modelA.
Note that in Model B, the second output port of the Receive block is sent into a terminator. This may be used to determine when a packet has arrived. The same is true for the outputs of the Unpack block, which in a real model would be used in the model.
For constructing the Model B to Model A side of the communication, follow an analogous procedure. The final models are shown below.
The following table lists the parameters in Model A.
The following table lists the parameters in Model B
Block |
Parameter |
Value |
Pack |
Input Port Datatypes |
{'single','double', |
Byte Alignment |
2 |
|
Send |
IP address |
192.168.0.1 |
IP port |
25000 |
|
Sample time |
0.01 |
Note on UDP Communication
The UDP blocks work in the background while the real-time application is not running. Also, the UDP communication has been set up to have a maximum of two UDP packets waiting to be read. All subsequent packets are rejected. This has been done to prevent excessive memory usage, and to minimize the load on the TCP/IP stack. Consequently, when any large background task is being performed, such as uploading a screen shot or communicating large pages through the WWW interface, packet loss may occur. Applications should be designed such that this is not critical. That is, the receipt of further packets after the ones that were lost will ensure graceful continuation.
![]() | xPC Target UDP Examples | Access IO | ![]() |