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]
ofuint8
(9 bytes)[1 1]
ofuint16
(2 bytes)[2 4]
ofdouble
(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]
ofsingle
(16 bytes)[2 2]
ofdouble
(32 bytes)[2 2]
ofuint32
(16 bytes)[5 3]
ofint8
(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.
slrt_ex_udpsendreceiveA
Send Side
Block | Parameter | Value |
---|---|---|
Byte Packing | Output port (packed) data type |
|
Input port (unpacked) data types (cell array) |
| |
Byte alignment |
| |
UDP Send | Local IP address |
|
Local port |
| |
To IP address |
| |
To port |
| |
Sample time (-1 for inherited) |
|
The
Length
input port receives the output of a Width block that calculates the width of the signal connected to theData
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 |
|
Local port |
| |
Receive width |
| |
Receive from any source |
| |
From IP address |
| |
Sample time (-1 for inherited) |
| |
Byte Unpacking | Output port (unpacked) data types (cell array) |
|
Output port (unpacked) dimensions (cell array) |
| |
Byte alignment |
|
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 is2
. The Byte Unpacking block assumes that the input vector includes a pad0
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.
slrt_ex_udpsendreceiveB
Receive Side
Block | Parameter | Value |
---|---|---|
UDP Receive | Local IP address |
|
Local port |
| |
Receive width |
| |
Receive from any source |
| |
From IP address |
| |
Sample time (-1 for inherited) |
| |
Byte Unpacking | Output port (unpacked) data types (cell array) |
|
Output port (unpacked) dimensions (cell array) |
| |
Byte alignment |
|
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 |
|
Input port (unpacked) data types (cell array) |
| |
Byte alignment |
| |
UDP Send | Local IP address |
|
Local port |
| |
To IP address |
| |
To port |
| |
Sample time (-1 for inherited) |
|
The
Length
input port receives the output of a Width block that calculates the width of the signal connected to theData
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 is2
. The Byte Packing block pads the output vector with0
to align on an even-numbered boundary.