main content

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 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 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.

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 or functions.

read section

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.

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 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.

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.

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 .

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 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.

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.

this parameter is read-only.

version type for ip address, specified as ipv4 or ipv6. this property is set during udp socket configuration.

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.

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 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.

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.

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

see also

apps

  • |

functions

网站地图