UDP Data Exchange with Shared Ethernet Board

This example shows how to set up two-way data exchange with 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 a UDP Configure block because the example uses the connection between the development and target computers. To perform real-time UDP data transfer with a dedicated Ethernet board, see Target to Target Transmission using UDP.

The example models are named udpsendreceiveA and udpsendreceiveB. Replace the port and IP address examples with ports and addresses as required by your network.

Data Transferred

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

For this example, the inputs are generated with 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.

udpsendreceiveA to udpsendreceiveB

The UDP data send is 75 bytes wide. The data to transfer is in the following 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.

udpsendreceiveB to udpsendreceiveA

The UDP data to be sent is 79 bytes wide. The data to transfer is in the following 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 udpsendreceiveA

The final udpsendreceiveA is shown in the figure.

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

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.0.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 Byte Unpacking block of udpsendreceiveB.

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

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, which in a real model would be used in the model.

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

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

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

Set Up udpsendreceiveB

The final udpsendreceiveB model is shown in the figure.

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

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

Sample time (-1 for inherited)

0.01

Byte Unpacking

Output port (unpacked) data types (cell array)

{'uint8', 'int16', '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, which in a real model would be used in the model.

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

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

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

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

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

See Also

| | | |

Related Topics