UDP Explorer
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.
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
Create UDP Socket for Byte Stream Communication
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.
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 afterward from the Property Inspector.
Click Confirm Parameters to bind to a UDP socket.
The UDP Explorer tab opens in the app and your UDP Socket appears in the Device List.
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.
Write and Read Binary Data with UDP Socket
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.
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 afterward from the Property Inspector.
Click Confirm Parameters to bind to a UDP socket.
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.
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.
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.
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.
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.
Receive Multicast Datagram Packets
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.
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.
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
.
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.
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.
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.
Plot Data from Communication Log
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.
Click Plot in the Analyze section.
A new figure window with a plot of the data opens. You can modify the plot and figure from the command window.
Export Data from Communication Log and Generate MATLAB Script
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.
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.
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.
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.
Generate a MATLAB live script and open it in the Live Editor by clicking Export and selecting the Generate MATLAB Script option.
After the live script opens, you can modify the code to fit your needs and save the file.
Parameters
Data Format
— Select type of write operation
Binary
| ASCII-Terminated String
| Datagram
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
.
Data Type
— Select MATLAB data type to write
uint8
| int8
| uint16
| int16
| uint32
| int32
| uint64
| int64
| single
| double
| char
| string
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.
Data to Write
— Specify numeric or ASCII data to write
numeric | character vector | string scalar
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.
Workspace Variable
— Select workspace variable to write
workspace variable
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 values1-by-
N
character vector1-by-1 string scalar
If Data Format is ASCII-Terminated
String
, you can select the following types of workspace variables:
1-by-
N
character vector1-by-1 string scalar
Select either this parameter or Data to Write to write data.
Write
— Write data using specified settings
button
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.
Data Format
— Select type of read operation
Binary
| ASCII-Terminated String
| Datagram
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
.
Data Type
— Select MATLAB data type to read
uint8
| int8
| uint16
| int16
| uint32
| int32
| uint64
| int64
| single
| double
| char
| string
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
.
Num Values to Read
— Specify number of values of selected Data Type to read
numeric
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.
Values Available
— Maximum possible number of values of selected Data Type that can be read
numeric
This parameter 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.
Read
— Read data using specified settings
button
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.
Display
— Select format to view data in Communication Log
Default
(default) | Binary
| ASCII
| Hexadecimal
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.
Clear
— Clear Communication Log
button
Click this button to clear all the contents of the Communication Log.
Plot
— Plot selected row of data
button
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.
Signal Analyzer
— View selected row of data in Signal Analyzer app
button
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.
Workspace Variable
— Specify name of workspace variable to export data to
valid variable name
Edit the name of the workspace variable that you want to export data to. The
Export Selected Row
and Export Communication
Log
options in the Export drop-down menu save your
data in the workspace as the variable specified in this field.
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.
Export
— Export Communication Log data or MATLAB code
Export Selected Row
| Export Communication Log
| Generate MATLAB Script
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 atimetable
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.
LocalHost
— Local host name or IP address
0.0.0.0
| ::
| valid host name | valid dotted-decimal IP address
This parameter 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.
LocalPort
— Local port number
numeric
This parameter 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.
IPAddressVersion
— Version type for IP address
IPV4
(default) | IPV6
This parameter is read-only.
Version type for IP address, specified as IPV4
or
IPV6
. This property is set during UDP socket configuration.
EnablePortSharing
— Allow other UDP sockets to bind to same local port
false
(default) | true
This parameter 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.
DestinationAddress
— Specify address to write to
valid host name | valid dotted-decimal IP address
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.
DestinationPort
— Specify port to write to
numeric
Destination port to write data to, specified as a numeric value.
You can set this as an optional property during UDP socket configuration.
OutputDatagramSize
— Specify maximum number of bytes written in datagram packet
512 (default) | numeric
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.
Timeout
— Specify allowed time to complete operations
10 (default) | numeric
Allowed time in seconds to complete read operations, specified as a numeric value.
ByteOrder
— Select sequential order of bytes
little-endian
(default) | big-endian
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
— Terminator characters for data
LF
(default) | CR
| CR/LF
| 0 to 255
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 to specify read and write terminator character values
separately.
Dependencies
To enable this property, the communication mode must be Byte Stream.
NumBytesAvailable
— Number of bytes available to be read
numeric
This parameter 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.
NumDatagramsAvailable
— Number of datagrams available to be read
numeric
This parameter 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.
EnableMulticastLoopback
— Allow looping back if sender is in multicast group
on (default) | off
Select this parameter to indicate looping back of data in multicast if the sender is subscribed to the same multicast group.
MulticastGroup
— Specify IP address group to receive multicast data from
valid host name | valid dotted-decimal IP address
IP address group to subscribe to for receiving multicast data, specified as a string.
EnableBroadcast
— Allow broadcasting
off (default) | on
Select this parameter to allow sending broadcast data.
Version History
Introduced in R2022a
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)