|On this page…|
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
Specify a descriptive name for the UDP object.
Specify the remote host.
Specify the remote host port for the connection.
Indicate the object type.
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 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
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:
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)
On host doejohn.dhpc, open u1:
On host doetom.dhpc, open u2:
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.
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