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

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

Communicating Between Two Hosts

This example illustrates how you can use UDP objects to communicate between two dedicated hosts. In this example, you know the names of both hosts and the ports they use for communication with each other. One host has the name doejohn.dhpc, using local port 8844; and the other host is doetom.dhpc, using local port 8866. Note that each host regards the other host's port as the RemotePort:

  1. Create interface objects — Create a UDP object on each host, referencing the other as the remote host.

    On host doejohn.dhpc, create u1. The object constructor specifies the name of the remote host, the remote port on that other host, and the local port to use on the machine where this object is created:

    u1 = udp('doetom.dhpc', 'RemotePort', 8866, 'LocalPort', 8844)

    On host doetom.dhpc, create u2:

    u2 = udp('doejohn.dhpc', 'RemotePort', 8844, 'LocalPort', 8866)
  2. Connect the objects — Open both UDP objects, so that each can communicate with the other host.

    On host doejohn.dhpc, open u1:

    fopen(u1)

    On host doetom.dhpc, open u2:

    fopen(u2)
  3. Write and read data — Communication between the two hosts is now a matter of sending and receiving data. Write a message from doejohn.dhpc to doetom.dhpc.

    On host doejohn.dhpc, write data to the remote host via u1:

    fprintf(u1, 'Ready for data transfer.')

    On host doetom.dhpc, read data coming in from the remote host via u2:

    fscanf(u2)
    ans =
    Ready for data transfer.
  4. Disconnect and clean up — When you no longer need u1 on host doejohn.dhpc, you should disconnect it and remove it from memory and from the MATLAB workspace.

    fclose(u1)
    delete(u1)
    clear u1

    When you no longer need u2, perform a similar cleanup on the host doetom.dhpc.

    fclose(u2)
    delete(u2)
    clear u2

Was this topic helpful?