Skip to Main Content Skip to Search
Product Documentation

xPC Target UDP Example

This section provides an example of how to set up two-way data exchange between two xPC Target systems, between the xPC Target and Simulink products, or between two Simulink models. When one or both of the systems is running Simulink in nonreal time, be sure to set the sample time properly.

The hypothetical models are called udpsendreceiveA and udpsendreceiveB. Two different sets of data are transferred between these two models, one set from udpsendreceiveA to udpsendreceiveB and another set in the opposite direction.

The data to transfer is in the following order:

udpsendreceiveA to udpsendreceiveB

udpsendreceiveB to udpsendreceiveA

For the purposes of this example, all the inputs are generated using Simulink Constant blocks that use the MATLAB random number function (rand). The Simulink Coder software uses this function at the time of code generation to generate random numbers. To generate the vector of uint8 (3x3), use the MATLAB function

uint8(255 * rand(3,3))

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 udpsendreceiveA.

Note 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','uint16','double'} and Byte Alignment 1.

For the Send block, set the IP Address to send to parameter to 192.168.0.11. This is the hypothetical address of the system that will run udpsendreceiveA. Set the IP Port to send to parameter to 25000 (picked arbitrarily). The sample time is set to 0.01.

Use this information to construct the receive end of udpsendreceiveB.

For setting up the Receive block, set IP address to receive from to 192.168.0.10 (the hypothetical address of the system that will run udpsendreceiveB). The IP port to receive from is set to 25000 (the same value as set in the Remote IP port to send to parameter of the Send block in udpsendreceiveA). The Output port width is set to 75, which is obtained from the output port width of the Pack block in udpsendreceiveA.

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 udpsendreceiveA. The Output port dimensions is set to {[3 3],1,[2 4]} from the dimensions of the inputs to the Pack block in udpsendreceiveA.

Note that in udpsendreceiveB, the second output port of the Receive block is sent into a terminator. You can use this 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.

To construct the udpsendreceiveB to udpsendreceiveA side of the communication, follow an analogous procedure. The final udpsendreceiveA is shown below.

The following table lists the parameters in udpsendreceiveA.

Block

Parameter

Value

Receive

IP address

192.168.0.11

IP port

25000

Output port width

80

Sample time

0.01

Unpack

Output port dimensions

{4,[2 2],[2 2],[5 3]}

Output port data types

{'single','double', 'uint32','int8'}

Byte alignment

2

The final udpsendreceiveB model is shown below.

The following table lists the parameters in udpsendreceiveB.

Block

Parameter

Value

Pack

Input port data types

{'single','double', 'uint32','int8'}

Byte alignment

2

Send

IP address

192.168.0.10

IP port

25000

Sample time

0.01

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS