Main Content

UDP Data Exchange by Using Shared Ethernet Board

This example shows how to set up two-way data exchange by using an Ethernet board that is shared with the connection between the development and target computers. Using this configuration, you can communicate between two Simulink® Real-Time™ systems, between the Simulink Real-Time and Simulink products, or between two Simulink models. When one or both of the systems are running as a non-real-time Simulink model, be sure to set the sample time.

This example does not require configuring a dedicated Ethernet card because the example uses the connection between the development and target computers.

The example models are named slrt_ex_udpsendreceiveA and slrt_ex_udpsendreceiveB. Replace the port and IP address examples with ports and addresses as required by your network. This example uses a target computer located at IP address 192.168.7.5 and uses a development computer located at IP address 192.168.7.2.

UDP Data Transfer

The models transfer two different data sets between them, one data set from slrt_ex_udpsendreceiveA to slrt_ex_udpsendreceiveB and another data set in the opposite direction.

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

uint8(255 * rand(3,3))

because 255 is the maximum value for an unsigned 8-bit integer. The other values are generated similarly.

slrt_ex_udpsendreceiveA to slrt_ex_udpsendreceiveB

The UDP data to send is 75 bytes wide. The data to transfer is in these formats:

  • [3 3] of uint8 (9 bytes)

  • [1 1] of uint16 (2 bytes)

  • [2 4] of double (64 bytes)

When packed, the data is aligned on 1-byte boundaries.

slrt_ex_udpsendreceiveB to slrt_ex_udpsendreceiveA

The UDP data to be sent is 79 bytes wide. The data to transfer is in these formats:

  • [4 1] of single (16 bytes)

  • [2 2] of double (32 bytes)

  • [2 2] of uint32 (16 bytes)

  • [5 3] of int8 (15 bytes)

When packed, the data is aligned on 2-byte boundaries. A zero-valued pad byte is added during packing.

Set Up slrt_ex_udpsendreceiveA

The final slrt_ex_udpsendreceiveA is shown in the figure.

The tables list the parameters for the send and receive sides of the model.

Image of slrt_ex_udp send receive model

slrt_ex_udpsendreceiveA Send Side

Block

Parameter

Value

Byte Packing

Output port (packed) data type

'uint8'

Input port (unpacked) data types (cell array)

{'uint8', 'uint16', 'double'}

Byte alignment

1

UDP Send

Local IP address

Use host-target connection

Local port

-1 (autoselect)

To IP address

192.168.7.5

To port

25000

Sample time (-1 for inherited)

0.01

  • The Length input port receives the output of a Width block that calculates the width of the signal connected to the Data port.

  • The Byte Packing block settings match the Byte Unpacking block of slrt_ex_udpsendreceiveB.

slrt_ex_udpsendreceiveA Receive Side

Block

Parameter

Value

UDP Receive

Local IP address

Use host-target connection

Local port

25000

Receive width

80

Receive from any source

off

From IP address

192.168.7.2

Sample time (-1 for inherited)

0.01

Byte Unpacking

Output port (unpacked) data types (cell array)

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

Output port (unpacked) dimensions (cell array)

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

Byte alignment

2

  • The second output port of the UDP Receive block is sent into a terminator. You can use this output to determine when a packet has arrived. The same is true for the outputs of the Byte Unpack block.

  • The Receive width of the UDP Receive block matches the output port width of the Byte Packing block in slrt_ex_udpsendreceiveB.

  • The Byte Unpacking block settings match the settings of the Byte Packing block of slrt_ex_udpsendreceiveB.

  • The number of unpacked bytes is 79. The byte alignment is 2. The Byte Unpacking block assumes that the input vector includes a pad 0 to align the vector on an even-numbered boundary.

Set Up slrt_ex_udpsendreceiveB

The final slrt_ex_udpsendreceiveB model is shown in the figure.

The tables list the parameters for the receive side and the send side of the model.

Image of slrt_ex_udp send receive model

slrt_ex_udpsendreceiveB Receive Side

Block

Parameter

Value

UDP Receive

Local IP address

Use host-target connection

Local port

25000

Receive width

75

Receive from any source

off

From IP address

192.168.7.5

Sample time (-1 for inherited)

0.01

Byte Unpacking

Output port (unpacked) data types (cell array)

{'uint8', 'uint16', 'double'}

Output port (unpacked) dimensions (cell array)

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

Byte alignment

1

  • The second output port of the UDP Receive block is sent into a terminator. You can use this output to determine when a packet has arrived. The same is true for the outputs of the Byte Unpack block.

  • The Receive width of the UDP Receive block matches the output port width of the Byte Packing block in slrt_ex_udpsendreceiveA.

  • The Byte Unpacking block settings match the Byte Packing block in slrt_ex_udpsendreceiveA.

slrt_ex_udpsendreceiveB Send Side

Block

Parameter

Value

Byte Packing

Output port (packed) data type

'uint8'

Input port (unpacked) data types (cell array)

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

Byte alignment

2

UDP Send

Local IP address

Use host-target connection

Local port

-1 (autoselect)

To IP address

192.168.7.2

To port

25000

Sample time (-1 for inherited)

0.01

  • The Length input port receives the output of a Width block that calculates the width of the signal connected to the Data port.

  • The Byte Packing block settings match the settings of the Byte Unpacking block of slrt_ex_udpsendreceiveA.

  • The number of unpacked bytes is 79. The byte alignment is 2. The Byte Packing block pads the output vector with 0 to align on an even-numbered boundary.

See Also

|

Related Examples

More About