Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Create a UDP Object

UDP Object

You create a UDP object with the udp function. udp does not require the name of the remote host as an input argument. However, if you are using the object to communicate with a specific instrument, you should specify the remote host and the port number.

Note

Although UDP is a stateless connection, opening a UDP object with an invalid host name will generate an error.

As described in Configuring Properties During Object Creation, you can also configure property values during object creation, such as the LocalPort property if you will use the object to read data from the instrument.

For example, to create a UDP object associated with the remote host 127.0.0.1, remote port 4012, and local port 3533,

u = udp('127.0.0.1', 4012, 'LocalPort', 3533);

The UDP object u now exists in the MATLAB® workspace. You can display the class of u with the whos command.

whos u
  Name      Size                   Bytes  Class

  u         1x1                      632  udp object

Grand total is 12 elements using 632 bytes

When the UDP object is created, the following properties are assigned values based on the values provided to the upd function. These general purpose properties provide information about the UDP object.

UDP Descriptive Properties

Property Name

Description

Name

Specify a descriptive name for the UDP object.

RemoteHost

Specify the remote host.

RemotePort

Specify the remote host port for the connection.

Type

Indicate the object type.

LocalPort

Specify the local host port, if you are expecting to receive data from the instrument.

Note

To get a list of options you can use on a function, press the Tab key after entering a function on the MATLAB command line. The list expands, and you can scroll to choose a property or value. For information about using this advanced tab completion feature, see Using Tab Completion for Functions.

You can display the values of these properties for u with the get function.

u.Name
ans = 
    UDP-127.0.0.1
u.RemoteHost
ans = 
    127.0.0.1
u.RemotePort
ans = 
    4012
u.Type
ans = 
    udp
u.LocalPort'
ans = 
    3533

The UDP Object Display

The UDP object provides you with a convenient display that summarizes important configuration and state information. You can invoke the display summary these three ways:

  • Type the UDP object variable name at the command line.

  • Exclude the semicolon when creating a UDP object.

  • Exclude the semicolon when configuring properties using the dot notation.

You can also display summary information via the Workspace browser by right-clicking an instrument object and selecting Display Summary from the context menu.

The display summary for the UDP object u is given below.

UDP Object : UDP-127.0.0.1

Communication Settings 
   RemotePort:         4012
   RemoteHost:         127.0.0.1
   Terminator:         'LF'

Communication State 
   Status:             closed
   RecordStatus:       off

Read/Write State  
   TransferStatus:     idle
   BytesAvailable:     0
   ValuesReceived:     0
   ValuesSent:         0

Enable Port Sharing over UDP

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 LocalPort number, and in another application bind a UDP socket to that same local port number so both can receive UDP broadcast data.

This allows for the ability to listen to UDP broadcasts on the same local port number in both MATLAB and other applications. You can enable and disable this capability with a new property of the UDP object called EnablePortSharing.

The EnablePortSharing property allows you to control UDP port sharing, and the possible values are on and off. The default value is off.

EnablePortSharing ValuesResult
'on'

Allows other UDP sockets to bind to the UDP object’s LocalPort.

'off' (default)

Prevents other UDP sockets from binding to the UDP object’s LocalPort.

Note that you need to set this property before calling fopen on the UDP object, or you will get an error.

This example shows creating a UDP object, assigning the local port, enabling port sharing, then opening the connection.

u = udp();
u.LocalPort = 5000;
u.EnablePortSharing = 'on';
fopen(u) 

You can now do read and write operations, and other applications can access the port since port sharing is enabled.

Was this topic helpful?