Main Content

UDP Explorer

Create UDP socket and communicate over networks using UDP

Description

The UDP Explorer app creates a UDP socket. After you create a UDP socket, you can communicate over UDP, plot and analyze data, export data to the workspace, and generate MATLAB® code.

Using this app, you can:

  • Configure byte and datagram connection and communication properties.

  • Send binary or string data over UDP.

  • Read binary or string data sent over UDP.

  • Receive and read multicast data.

  • Broadcast data over a network.

  • Plot data in a figure window.

  • Analyze data by viewing it in the Signal Analyzer app.

  • Export data to the workspace.

  • Generate a MATLAB live script that uses the udpport interface.

UDP Explorer app

Open the UDP Explorer App

  • MATLAB Toolstrip: On the Apps tab, under Test and Measurement, click the app icon.

  • MATLAB command prompt: Enter udpExplorer.

Examples

expand all

Create a UDP socket using the UDP Explorer app.

Open the UDP Explorer app from either the Apps tab in the MATLAB toolstrip or the MATLAB command prompt. Click Configure UDP Socket.

UDP Explorer app with Configure UDP Socket selected.

You can leave all the parameters as the default values. Using the default settings binds to a UDP socket for byte stream communication with IP Version IPV4 and Port Sharing disabled. The Local Address is assigned 0.0.0.0 and the Local Port is automatically assigned by the operating system. You can specify the Destination Address and Destination Port for writing data afterwards from the Property Inspector.

Click Confirm Parameters to bind to a UDP socket.

UDP Explorer app with connection parameters.

The UDP Explorer tab opens in the app and your UDP Socket appears in the Device List.

UDP Explorer app showing UDP Explorer tab and Device List pane.

View Connection properties and configure Communication properties from the Property Inspector. You can specify Communication, Multicast, and Broadcast properties of the socket before writing and reading data.

UDP Explorer app showing Property Inspector.

Write and read binary data using a UDP socket. This example uses an echo server for UDP communication. An echo server is a service that returns to the sender's address and port the same bytes it receives from the sender.

Start an echo UDP server at port 9090.

echoudp("on",9090)

Open the UDP Explorer app from either the Apps tab in the MATLAB toolstrip or the MATLAB command prompt. Click Configure UDP Socket.

UDP Explorer app with Configure UDP Socket selected.

You can leave all the parameters as the default values. Using the default settings binds to a UDP socket for byte stream communication with IP Version IPV4 and Port Sharing disabled. The Local Address is assigned 0.0.0.0 and the Local Port is automatically assigned by the operating system. You can specify the Destination Address and Destination Port for writing data afterwards from the Property Inspector.

Click Confirm Parameters to bind to a UDP socket.

UDP Explorer app with connection parameters.

View Connection properties and configure Communication properties from the Property Inspector. In this example, set DestinationAddress to 127.0.0.1 and DestinationPort to 9090 to send data to the echo server.

UDP Explorer app showing Property Inspector with specified DestinationAddress and DestinationPort.

Send some data using the UDP socket. In the Write section, specify the Data to Write as 1:10. Click Write to send data using the socket.

UDP Explorer app Write section for binary data.

The echo server returns the same data that was sent by the socket. You can confirm this by checking that the Values Available in the Read section is 10. Read all available data by clicking Read.

UDP Explorer app Read section for binary data.

View both the write and read operations in the Communication Log pane. You can select a row to export it as a variable to the workspace by following the steps in Export Data from Communication Log and Generate MATLAB Script.

UDP Explorer app Communication Log pane.

The MATLAB Code Log pane shows the code for these operations. You can export this code as a MATLAB live script by following the steps in Export Data from Communication Log and Generate MATLAB Script.

UDP Explorer app MATLAB Code Log pane.

In UDP multicasting, a UDP socket is used to send data and one or more UDP sockets are used for receiving data. The UDP socket for sending datagrams has already been programmed outside this example. In this example, you configure the UDP socket that receives the multicast data.

Open the UDP Explorer app from either the Apps tab in the MATLAB toolstrip or the MATLAB command prompt. Click Configure UDP Socket.

UDP Explorer app with Configure UDP Socket selected.

Specify Communication Mode as Datagram. For the purposes of this example, Local Port is 50000 since the UDP multicast sender has already been programmed to send data to port 50000. Click Confirm Parameters.

UDP Explorer app with connection parameters.

View Connection properties and configure Communication properties from the Property Inspector. For this example, subscribe to the multicast group by setting MulticastGroup to 226.0.0.1.

UDP Explorer app showing Property Inspector with specified MulticastGroup.

In this example, the UDP socket that you created using the app is the receiver. The sender is already set up and sends data to the multicast address group 226.0.0.1. Since this receiver is subscribed to this address, it receives the multicast data. You can see that data has been received when there is a value for Datagrams Available in the Read section. Read all the available datagrams by clicking Read.

UDP Explorer app Read section for datagrams.

View the read operations in the Communication Log pane. For datagram communication, each row represents a single datagram. The Address and Port column is where the datagram is coming from. You can select a row to plot it, view it in the Signal Analyzer app, or export it as a variable to the workspace. Select the data from the read operation. You can plot this data by following the steps in Plot Data from Communication Log.

UDP Explorer app Communication Log pane.

The MATLAB Code Log pane shows the code for these operations (except for plot creation). You can export this code as a MATLAB live script by following the steps in Export Data from Communication Log and Generate MATLAB Script.

UDP Explorer app MATLAB Code Log pane.

Plot a row of data from the Communication Log in a new figure window. You can plot any numeric data that you have written or read using the UDP socket.

The Communication Log captures all the data that you have written or read using the UDP socket.

Select a row of data.

UDP Explorer Communication Log pane with row selected.

Click Plot in the Analyze section.

UDP Explorer plot button.

A new figure window with a plot of the data opens. You can modify the plot and figure from the command window.

MATLAB figure window showing a plot of the selected row of data.

Use the different options for exporting data and app interactions.

The Communication Log captures all the data that you have written or read using the UDP socket.

Select a row of data.

UDP Explorer app Communication Log pane.

Export this row of data to the workspace as the variable specified in Workspace Variable. The app provides a default variable name, but you can edit it. The data is saved in the workspace as its Data Type. Change the variable name, click Export, and select the Export Selected Row option.

UDP Explorer app showing Export Selected Row option.

You can also export the entirety of the Communication Log to the workspace as a timetable. Change the variable name, click Export, and select the Export Communication Log option.

UDP Explorer app showing Export Communication Log option.

Besides exporting data, you can also export the code from the MATLAB Code Log pane. This pane contains all udpport object creation, write, read, and property configuration operations that you do in the app.

UDP Explorer app MATLAB Code Log pane.

Generate a MATLAB live script and open it in the Live Editor by clicking Export and selecting the Generate MATLAB Script option.

UDP Explorer app showing Generate MATLAB Script option.

After the live script opens, you can modify the code to fit your needs and save the file.

Parameters

Write Section

You can write Binary, ASCII-Terminated String, or Datagram data using the UDP socket. If you select Binary or ASCII-Terminated String for this parameter, your data is still sent over UDP as datagrams.

A Binary or Datagram write is equivalent to the write function and an ASCII-Terminated String write is equivalent to the writeline function.

Dependencies

If the communication mode is Byte Stream, this parameter can be Binary or ASCII-Terminated String.

If the communication mode is Datagram, the only possible value for this parameter is Datagram.

Specify the data type of the data to write using the UDP socket. This parameter determines the number of bytes to write for each value and the interpretation of those bytes as a MATLAB data type.

Dependencies

If you set the Data Format to ASCII-Terminated String, the only possible value for this parameter is string.

This parameter can be set to uint64 or int64 only if you select the Workspace Variable option instead of Data to Write.

Specify the data to write using the UDP socket. The data is written as the type specified by Data Type, regardless of the format in this parameter. If the Data Type is a numeric type, the app evaluates the Data to Write values and operations before writing the data. For example, you can specify 1:5 instead of [1,2,3,4,5].

Select either this parameter or Workspace Variable to write data.

Select an existing workspace variable to write using the UDP socket. The data is written as the type specified by Data Type, regardless of the data type of the variable in the workspace.

If Data Format is Binary or Datagram, you can select the following types of workspace variables:

  • Row (1-by-N) or column (N-by-1) vector of numeric values

  • 1-by-N character vector

  • 1-by-1 string scalar

If Data Format is ASCII-Terminated String, you can select the following types of workspace variables:

  • 1-by-N character vector

  • 1-by-1 string scalar

Select either this parameter or Data to Write to write data.

Click this button to write the data specified in Data to Write or Workspace Variable as the specified Data Type using the UDP socket. If Data Format is ASCII-Terminated String, the write terminator specified by the Terminator property is automatically appended to the data being written.

Clicking this button is equivalent to performing the write or writeline functions.

Read Section

Read Binary, ASCII-Terminated String, or Datagram data using the UDP socket.

A Binary or Datagram read is equivalent to the read function and an ASCII-Terminated String read is equivalent to the readline function.

Dependencies

If the communication mode is Byte Stream, this parameter can be Binary or ASCII-Terminated String.

If the communication mode is Datagram, the only possible value for this parameter is Datagram.

Specify the data type of the data to read using the UDP socket. This parameter determines the number of bytes to read for each value and the interpretation of those bytes as a MATLAB data type.

Note

If the Data Format is Datagram, you must specify the correct Data Type to avoid returning an error and losing data.

Dependencies

If you set the Data Format to ASCII-Terminated String, the only possible value for this parameter is string.

Specify the number of values or datagrams to read as a positive integer. This parameter must be less than or equal to Values Available (Datagrams Available if the communication mode is Datagram). If you leave this parameter empty, the app reads all available values using the UDP socket and the specified Data Type.

Dependencies

If the communication mode is Byte Stream, enable this parameter by setting Data Format to Binary.

If the communication mode is Datagram, this parameter is labeled Num Datagrams to Read.

This property is read-only.

This is the number of values or datagrams available to read in the format specified by Data Type.

Dependencies

If the communication mode is Byte Stream, enable this parameter by setting Data Format to Binary.

If the communication mode is Datagram, this parameter is labeled Datagrams Available.

Click this button to read data using the UDP socket. If Data Format is Binary or Datagram, read the number of values specified by Num Values to Read (Num Datagrams to Read if the communication mode is Datagram) in the form specified by Data Type. If Data Format is ASCII-Terminated String, read data until the first occurrence of the read terminator specified by the Terminator property.

Clicking this button is equivalent to the read or readline functions.

Communication Log Section

View the data in the Data column of the Communication Log as Binary, ASCII, or Hexadecimal, as applicable based on the data type. This parameter does not change the original value or data type of the data. For more information about these formats, see Data Type Conversion.

Click this button to clear all the contents of the Communication Log.

Analyze Section

Click this button to open a new figure window that plots the data currently selected in the Communication Log. You can select only one row of data, and the selected data must be numeric.

Unlike Write and Read, this operation is not captured in the MATLAB Code Log pane.

Click this button to launch the Signal Analyzer app and send it the data currently selected in the Communication Log. You can select only one row of data, and the selected data must be a numeric vector.

You must have Signal Processing Toolbox™ installed to use the Signal Analyzer app.

Export Section

Edit the name of the workspace variable that you want to export data to. The Export Selected Row and Export Communication Log options in Export save your data in the workspace as the variable specified by this parameter.

You must specify a valid MATLAB variable name that does not already exist in the workspace. If you specify an invalid name, it is automatically changed to a valid variable name.

Click this button to select one of the following options for exporting data from this app:

  • Export Selected Row — Save the data currently selected in the Communication Log to the workspace as the variable specified by Workspace Variable.

  • Export Communication Log — Save all of the Communication Log data to the workspace as a timetable with the variable name specified by Workspace Variable.

  • Generate MATLAB Script — Generate a MATLAB live script populated with the content in MATLAB Code Log and open it in the Live Editor.

Property Inspector

This property is read-only.

Local host name or dotted-decimal IP address, specified as a character vector. This property is set during UDP socket configuration. If you do not specify a value when you create the UDP socket, the default value is 0.0.0.0 for IPV4, or :: for IPV6.

This property is read-only.

Local port number that the UDP socket binds to, specified as a numeric value from 0 to 65535. This property is set during UDP socket configuration. If you do not specify a value when you create the UDP socket, a value is automatically assigned by the operating system.

This property is read-only.

Version type for IP address, specified as IPV4 or IPV6. This property is set during UDP socket configuration.

This property is read-only.

Setting to allow other UDP sockets to bind to the same local port as this socket, specified as true or false. This property is set during UDP socket configuration.

Destination address to write data to, specified as a host name or dotted-decimal IP address.

You can set this as an optional property during UDP socket configuration.

Destination port to write data to, specified as a numeric value.

You can set this as an optional property during UDP socket configuration.

Maximum number of bytes of data to be written in a datagram packet, specified as a numeric value from 1 to 65507. If you are writing more bytes than the specified OutputDatagramSize, the data is split into multiple datagrams before it is sent, each with a maximum size of OutputDatagramSize . For example, if the value of OutputDatgramSize is 10, 15 bytes of data are broken into two packets.

Allowed time in seconds to complete read operations, specified as a numeric value.

Sequential order in which bytes are arranged into larger numerical values. If the byte order is little-endian, then the first byte is stored in the first memory address. If the byte order is big-endian, then the last byte is stored in the first memory address.

Configure the byte order to match the appropriate value for your network.

Terminator characters for reading and writing ASCII-terminated data, specified as LF, CR, CR/LF, or a number from 0 to 255. The read terminator is followed by the write terminator and the two are separated by a comma. Click the vertical ellipsis icon Vertical ellipsis icon to specify read and write terminator character values separately.

Dependencies

To enable this property, the communication mode must be Byte Stream.

This property is read-only.

Number of bytes available to be read, specified as a numeric value.

Dependencies

To enable this property, the communication mode must be Byte Stream.

This property is read-only.

Number of datagrams available to be read, specified as a numeric value.

Dependencies

To enable this property, the communication mode must be Datagram.

Select this parameter to indicate looping back of data in multicast if the sender is subscribed to the same multicast group.

IP address group to subscribe to for receiving multicast data, specified as a string.

Select this parameter to allow sending broadcast data.

Version History

Introduced in R2022a