Receive data over UDP network from specified remote machine
Instrument Control Toolbox
The UDP Receive block configures and opens an interface to a specified remote address using the UDP protocol. The configuration and initialization occur once at the start of the model's execution. During the model's run time, the block acquires data either in blocking or nonblocking mode.
You need a license for both the Instrument Control Toolbox™ and Simulink® software to use this block.
This block has no input ports. It has either one or two output ports based on your selection of blocking or nonblocking mode. If you select blocking mode, the block will have one output port corresponding to the data it receives.
If you do not select blocking mode, the block will have two output ports, the Data port and the Status port.
A First In First Out (FIFO) buffer receives the data. At every time step, the Data port outputs the requested values from the buffer. In a nonblocking mode, the Status port indicates if the block has received new data.
Other Supported Features
This block supports the use of Simulink Accelerator™ mode, but not Rapid Accelerator or code generation.
The block supports the use of model referencing, so that your model can include other Simulink models as modular components.
For more information on these features, see the Simulink documentation.
You can enter MATLAB variables in the text edit fields in the UDP Receive Block Parameters dialog box, except for these fields: Local address, Remote address, ASCII format string, and Terminator.
- Local address
Specify the IP address, name, or the Web server address of the local host. This is the same as the UDP interface
localhostproperty. This field is empty by default.
- Local port
Specify the port to bind on the local machine. The default value is
-1, which automatically binds to an available port.
- Enable local port sharing
Use to enable port sharing. UDP ports can be shared by other applications to allow for multiple applications to listen to the UDP datagrams on that port. You can bind a UDP object to a specific
LocalPortnumber, and in another application bind a UDP socket to that same local port number so both can receive UDP broadcast data. Enabling this option allows other UDP sockets to bind to the UDP object’s
LocalPort. It is off by default.
- Remote address
Specify the IP address, name, or the Web server address of the machine from which you need to receive data. This field is set to
- Remote Port
Specify the remote port on the host you need to connect to. The default port value is
9090. Valid port values are
- Verify address and port connectivity
Click this button to:
Check if the specified remote address is correct.
Establish connection with the specified remote address and port.
- Output latest data
Enable to receive the latest data from the UDP Receive block. Data is normally received in a FIFO manner. When enabled, the latest available packets are received, instead of using the FIFO method. Note that this option is disabled when data type is ASCII. This option is off by default.
- Data size
Specify the output data size, or the number of values that should be read at each simulation time step. This parameter is specified as a row vector of nonnegative integers, where each element represents the length of the corresponding dimension. For example, if you specify the data size as
mrepresents the number of rows and
nrepresents the number of columns. The data size does not include the terminator value. The default value is
[1 1], which represents a single scalar value.
- Source Data type
Specify the input data type to receive from the block. You can select from the following values:
- ASCII format string
This option is only available when you select ASCII as your data type. It defines the format string for the data. For a list of formats, see the
This option is only available when you select ASCII as your data type. It can be used to set a terminator for the data read. For more information about setting the property, see
- Byte order
When using binary or binblock format with more than 8 bits, you can specify the instrument's byte order for the data. Your options are
- Enable blocking mode
Specify if you want to block the simulation while receiving data. This option is selected by default. Clear this check box if you do not want the read operation to block the simulation.
If you enable blocking mode, the model will block the simulation while it is waiting for the requested data to be available. If you do not enable blocking mode, the simulation runs continuously. The block has two output ports, Status and Data. The Data port contains the requested set of data at each time step. The Status port contains 0 or 1 based on whether it received new data at the given time step. The following diagrams show the difference between receiving data using blocking mode and nonblocking mode.
In this example, you start the simulation at time (t=0) and specify the amount of data to receive as 4 (set in the Data size field of the UDP Receive Block Parameters dialog box). After the simulation starts, the data is acquired asynchronously in a FIFO buffer.
The blocking mode simulation occurs like this:
At time step 1: The Simulink software requests data and the buffer has four values available, the block fulfills the request without interrupting the simulation. The block resets the buffer value to 0.
At time step 2: The Simulink software requests data again, and the buffer has only three values, therefore it blocks the simulation until it receives the fourth value. When the block receives the fourth value, it fulfills the request and resumes the simulation. The block resets the buffer value to 0.
At time step 3: When the Simulink software requests data, the block has five values and it returns the first four that it received and resets the buffer to 1.
If the requested data is not received within the amount of time specified in the Timeout field (of the UDP Receive Block Parameters dialog box), a Simulink software error occurs and the simulation is stopped.
In blocking mode, if you have more than one UDP model on your computer, ensure that the Receive block is receiving data. If it is not, then your model might error out. You can avoid this by either changing the block to Nonblocking mode or by resetting the block's priority.
Here the simulation is not blocked and runs continuously.
At time step 1: The Simulink software requests data and the buffer has four values available, the block fulfills the request and changes the
Statusport value to
1, indicating that new data is available. The
Dataport at this point contains the newly received values. The block resets the buffer value to 0.
At time step 2: The Simulink software requests data again, and the buffer has only three values, and the block cannot return it as data size is specified as 4. Therefore the block sets the
Statusport value to
0, indicating that there is no new data. The
Dataport contains the previously received value, and the buffer is at three (the number of values it has received since the last request was fulfilled).
At time step 3: When the Simulink software requests data here, the buffer now has five values and it returns the first four in the order it received and changes the
Statusport value to
Specify the amount of time that the model will wait for the data during each simulation time step. The default value is
inf(seconds). This field is unavailable if you have not enabled blocking mode.
- Block sample time
Specify the sampling time of the block during simulation. The default value is