create udp socket and communicate over networks using udp -凯发k8网页登录
create udp socket and communicate over networks using udp
since r2022a
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 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 afterwards 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 afterwards 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
function and an ascii-terminated string
write is equivalent
to the
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 or 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
function and an ascii-terminated string
read is equivalent to
the
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 or 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 .
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 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.
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
see also
apps
- |
functions
matlab 命令
您点击的链接对应于以下 matlab 命令:
请在 matlab 命令行窗口中直接输入以执行命令。web 浏览器不支持 matlab 命令。
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
- (español)
- (english)
- (english)
europe
- (english)
- (english)
- (deutsch)
- (español)
- (english)
- (français)
- (english)
- (italiano)
- (english)
- (english)
- (english)
- (deutsch)
- (english)
- (english)
- switzerland
- (english)
asia pacific
- (english)
- (english)
- (english)
- 中国
- (日本語)
- (한국어)