UDP Receive
Receive UDP packets from UDP host
Add-On Required: This feature requires the Simulink Coder Support Package for STMicroelectronics Nucleo Boards add-on.
Libraries:
Simulink Coder Support Package for STMicroelectronics Nucleo Boards /
STM32F7
Simulink Coder Support Package for STMicroelectronics Nucleo Boards /
STM32H7
Description
The UDP Receive block receives UDP packets from a UDP host. Use the block for stateless and connectionless data transmission.
With each sample, the block outputs the contents of a UDP packet as a (Nx1) array.
The block receives the packets on the port number specified in the
Local IP Port parameter from the IP address
specified in the Remote IP address (0.0.0.0 for accepting
all) parameter. To receive packets from all the sending
hosts, specify the remote IP address as 0.0.0.0
. Match
the port number specified in the Local IP Port
parameter with the remote port number of the sending host.
You can choose to receive the UDP packets in blocking or non-blocking mode.
Note
If you are having trouble using UDP to communicate with a computer, investigate if antivirus or firewall software is blocking UDP traffic. If so, try to configure the software to allow the traffic for a specific IP port number.
Ports
Output
Data — Output UDP packets received from the sending UDP
host
vector
At each sample time, the port outputs the contents of a packet received as a data vector of the size that you specify in the Data size (N) parameter. For more information, see .
Data Types: int8
| uint8
| int16
| uint16
| int32
| uint32
| single
| double
| Boolean
Status — Determine if requested data is received at the given time step
scalar
At each sample time, the port outputs
0
when the length of data
received is greater than or equal to the length
specified in the Data size
(N) parameter. A value of
0
indicates that the requested
data is received at the given time step.
Otherwise, it outputs a nonzero value, indicating
that no new data is available.
For more information, see .
Data Types: uint16
Parameters
Local IP Port — IP port on the receiving UDP host
25000
(default) | Any integer between 1 and
65535
Specify the port number of the application from which you want to receive the packets. Match the local port number with the remote port number of the sending host.
Remote IP address (0.0.0.0 for accepting all) — IP address from which the block receives UDP packets
192.168.1.15
(default)
Specify the IP address of the remote host from which
the block receives packets. To receive packets from
all the sending hosts, specify
0.0.0.0
.
Data type — Data type of the elements in UDP packets
uint8
(default) | double
| single
| int8
| int16
| uint16
| int32
| uint32
| boolean
Select the data type in which the block receives the elements from the sending host. The size of each element depends on its data type.
Data size (N) — Number of data elements in each UDP packet
1
(default) | scalar
Specify the number of elements that you want to receive in each packet. The data size must be scalar values.
Wait until data received — Wait until the requested data is available
off
(default) | on
on
— When you select this parameter, the read operation runs in the Blocking mode. The read operation is blocked while waiting for the requested data to be available. If data is available, theData
port outputs data. If data is not available, theData
port waits for data.A task overrun occurs if the target hardware is still waiting for the data to be available when the next read operation is scheduled to begin.
To fix overruns:
Increase the time step by using the Sample time parameter.
Reduce the length of data requested by using the Data size (N) parameter.
off
— When you clear this parameter, the read operation runs in the Non-blocking mode. When reading data, if data is not available, theData
port contains the packet received in the previous time step. In this mode, the block does not wait for the requested data to be available.
Output error status — Option to display the error status during data transmission
on
(default) | off
Select this option to display the error status during data transmission.
When you select the Output error status parameter, the block configures an output port. The port on the block is labeled as Status, indicating that the block outputs the status of the read operation at the output port.
Sample time — How often this block reads packets from the sending UDP host
-1
(default)
When you specify this parameter
as-1
, Simulink® determines
the best sample time for the block based on the
block context within the model.
Receive UDP Packets
This example describes the values at the output ports when the length of the packets received is less than, greater than, or equal to the length of requested data.
Length of UDP packet received = Data size (N): The
Data
port outputs the packet as a data vector of the size specified in the Data size (N) parameter.The
Status
port outputs0
, indicating that the requested data is received.Suppose that the Data size (N) parameter specified is 4 and the length of the packet received is also 4.
In this case, the
Data
port outputs a data vector of size 4 filled with the elements of the packet.The value at the
Status
port is0
.Length of UDP packet received < Data size (N): The
Data
port outputs the packet as a data vector of the size specified in the Data size (N) parameter. All the empty spaces in the vector are filled with random values.The
Status
port outputs a nonzero value, indicating that no new data is received.Suppose that the Data size (N) parameter specified is 4 and the length of data received is 3 bytes.
In this case, the
Data
port outputs a data vector of size 4. The first three elements in the vector are the elements from the received packet. The remaining space is filled with a random value.The
Status
port outputs a nonzero value.Length of UDP packet received > Data size (N): The
Data
port outputs a data vector of the size specified in the Data size (N) parameter. The vector contains only the first N elements from the packet. The remaining elements are dropped.The
Status
port outputs0
, indicating that the requested data is received.Suppose that the Data size (N) parameter specified is 4 and the length of data received is 5 bytes. In this case, the
Data
port outputs a data vector of size 4. The vector contains only the first four elements from the received packet. The remaining element is dropped. The value at theStatus
port is0
.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
Version History
Introduced in R2017a
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)