Instrument Control Toolbox    

Example: Writing and Reading Data with a UDP Object

This example illustrates how to use text read and write operations with a UDP object connected to a remote instrument.

The instrument used is an echo server on a Linux-based PC. An echo server is a service available from the operating system that returns (echoes) received data to the sender. The host name is daqlab11 and the port number is 7. The host name is assigned by your network administrator.

  1. Create an instrument object -- Create a UDP object associated with daqlab11.
  2. Connect to the instrument -- Connect u to the echo server.
  3. Write and read data -- You use the fprintf function to write text data to the instrument. For example, write the following string to the echo server.
  1. UDP sends and receives data in blocks that are called datagrams. Each time you write or read data with a UDP object, you are writing or reading a datagram. For example, the string sent to the echo server constitutes a datagram with 13 bytes -- 12 ASCII bytes plus the line feed terminator.

    You use the fscanf function to read text data from the echo server.

    The DatagramTerminateMode property indicates whether a read operation terminates when a datagram is received. By default, DatagramTerminateMode is on and a read operation terminates when a datagram is received. To return multiple datagrams in one read operation, set DatagramTerminateMode to off. The following commands write two datagrams. Note that only the second datagram sends the terminator character.

    Since DatagramTerminateMode is off, fscanf reads across datagram boundaries until the terminator character is received.

  1. Disconnect and clean up -- When you no longer need u, you should disconnect it from the host, and remove it from memory and from the MATLAB workspace.

  Example: Writing and Reading Data with a TCP/IP Object Events and Callbacks