Write and Read Data over TCP/IP Interface

Write Data

The write function synchronously writes data to the remote host connected to the tcpclient object. First specify the data, then write the data. The function waits until the specified number of values is written to the remote host.

In this example, a tcpclient object t already exists.

% Create a variable called data
data = 1:10;

% Write the data to the object t
write(t, data)

Note

For any read or write operation, the data type is converted to uint8 for the data transfer. It is then converted back to whatever data type you set if you specified another data type.

Read Data

The read function synchronously reads data from the remote host connected to the tcpclient object and returns the data. There are three read options:

  • Read all bytes available (no arguments)

  • Optionally specify the number of bytes to read

  • Optionally specify the data type

If you do not specify a size, the default read uses the BytesAvailable property value, which is equal to the numbers of bytes available in the input buffer.

In these examples, a tcpclient object t already exists.

% Read all bytes available.
read(t)

% Specify the number of bytes to read, 5 in this case.
read(t, 5)

% Specify the number of bytes to read, 10, and the data type, double.
read(t, 10, 'double')

Note

For any read or write operation, the data type is converted to uint8 for the data transfer. It is then converted back to whatever data type you set if you specified another data type.

Acquire Data from a Weather Station Server

One of the primary uses of TCP/IP communication is to acquire data from a server. This example shows how to acquire and plot data from a remote weather station.

Note

The IP address in this example is not a working IP address. The example shows how to connect to a remote server. You should substitute the address shown here with the IP address or host name of a server you want to communicate with.

  1. Create the tcpclient object using the Address shown here and Port of 1045.

    t = tcpclient('172.28.154.231', 1045)
    
    t = 
    
       tcpclient with properties:
    
              Address: '172.28.154.231'
                 Port: 1045
              Timeout: 10
       BytesAvailable: 0

    See the note above step 1 about using a valid address.

  2. Acquire data using the read function. Specify the number of bytes to read as 30, for 10 samples from 3 sensors (temperature, pressure, and humidity). Specify the data type as double.

    data = read(t, 30, 'double');
  3. Reshape the 1x30 data into 10x3 data to show one column each for temperature, pressure, and humidity.

    data = reshape(data, [3, 10]);
  4. Plot the temperature.

    subplot(311);
    plot(data(:, 1));
  5. Plot the pressure.

    subplot(312);
    plot(data(:, 2));
  6. Plot the humidity.

    subplot(313);
    plot(data(:, 3));
  7. Close the connection between the TCP/IP client object and the remote host by clearing the object.

    clear t

Read and Write uint8 Data

This example shows how to read and write uint8 data from an echo server.

  1. Create the tcpclient object using a local host at Port 7.

    t = tcpclient('localhost', 7)
    
    t = 
    
       tcpclient with properties:
    
              Address: 'localhost'
                 Port: 7
              Timeout: 10
       BytesAvailable: 0
  2. Assign 10 bytes of uint8 data to the variable data.

    data = uint8(1:10)
    
    data = 
    
      1    2    3    4    5    6    7    8    9    10
  3. Check the data.

    whos data
    
    Name     Size     Bytes     Class     Attributes
    
    data     1x10        10     uint8
  4. Write the data to the echoserver.

    write(t, data)
  5. Check that the data was written using the BytesAvailable property.

    t.BytesAvailable
    
    ans = 
    
        10
  6. Read the data from the server.

    read(t)
    
    ans = 
    
      1    2    3    4    5    6    7    8    9    10
  7. Close the connection by clearing the object.

    clear t