UDP Receive
Libraries:
STM32 Microcontroller Blockset /
(Legacy) STM32 MBED Based Boards /
STM32F7
STM32 Microcontroller Blockset /
(Legacy) STM32 MBED Based 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
Ensure to configure the Ethernet options in STM32CubeMX project. For more, see Ethernet Configuration in STM32CubeMX for STM32 Processor-Based Boards.
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.
Examples
Get Started with STMicroelectronics STM32 Processor Based Boards
Run a Simulink model on STM32 processor.
Ports
Output
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.
Data Types: int8 | uint8 | int16 | uint16 | int32 | uint32 | single | double | Boolean
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 Receive UDP Packets.
Dependencies
To enable this port, select the Output error status parameter.
Data Types: uint16
Parameters
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.
Programmatic Use
Block
Parameter:
RemoteIPAddress |
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.
Programmatic Use
Block
Parameter:
LocalPort |
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, theDataport outputs data. If data is not available, theDataport 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, theDataport contains the packet received in the previous time step. In this mode, the block does not wait for the requested data to be available.
Programmatic Use
Block
Parameter:
BlockingMode |
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.
Programmatic Use
Block
Parameter:
DataType |
Specify the number of elements that you want to receive in each packet. The data size must be scalar values.
Programmatic Use
Block
Parameter:
DataLength |
Select this parameter 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.
Programmatic Use
Block
Parameter:
OutputStatus |
When you specify this parameter
as-1, Simulink® determines the best sample time for
the block based on the block context within the
model.
Programmatic Use
Block
Parameter:
SampleTime |
More About
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
Dataport outputs the packet as a data vector of the size specified in the Data size (N) parameter.The
Statusport 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
Dataport outputs a data vector of size 4 filled with the elements of the packet.The value at the
Statusport is0.
Length of UDP packet received < Data size (N): The
Dataport 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
Statusport 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
Dataport 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
Statusport outputs a nonzero value.
Length of UDP packet received > Data size (N): The
Dataport 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
Statusport 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
Dataport 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 theStatusport is0.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
Version History
Introduced in R2022b
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)

