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.

Basic Workflow to Read and Write Data over TCP/IP

This example illustrates how to use text and binary read and write operations with a TCP/IP object connected to a remote instrument. In this example, you create a vector of waveform data in the MATLAB® workspace, upload the data to the instrument, and then read back the waveform.

The instrument is a Sony/Tektronix® AWG520 Arbitrary Waveform Generator (AWG). Its address is sonytekawg.yourdomain.com and its port is 4000. The AWG's host IP address is 192.168.1.10 and is user configurable in the instrument. The associated host name is given by your network administrator. The port number is fixed and is found in the instrument's documentation:

  1. Create an instrument object — Create a TCP/IP object associated with the AWG.

    t = tcpip('sonytekawg.yourdomain.com',4000);
  2. Connect to the instrument — Before establishing a connection, the OutputBufferSize must be large enough to hold the data being written. In this example, 2577 bytes are written to the instrument. Therefore, the OutputBufferSize is set to 3000.

    t.OutputBufferSize = 3000)

    You can now connect t to the instrument.

    fopen(t)
  3. Write and read data — Since the instrument's byte order is little-endian, configure the ByteOrder property to littleEndian.

    t.ByteOrder = 'littleEndian'

    Create the sine wave data.

    x = (0:499).*8*pi/500;
    data = sin(x);
    marker = zeros(length(data),1);
    marker(1) = 3;

    Instruct the instrument to write the file sin.wfm with Waveform File format, a total length of 2544 bytes, and a combined data and marker length of 2500 bytes.

    fprintf(t,'%s',['MMEMORY:DATA "sin.wfm",#42544MAGIC 1000' 13 10])
    fprintf(t,'%s','#42500')

    Write the sine wave to the instrument.

    for (i = 1:length(data)),
    	fwrite(t,data(i),'float32');
    	fwrite(t,marker(i));
    end

    Instruct the instrument to use a clock frequency of 100 MS/s for the waveform.

    fprintf(t,'%s',['CLOCK 1.0000000000e+008' 13 10 10])

    Read the waveform stored in the function generator's hard drive. The waveform contains 2000 bytes plus markers, header, and clock information. To store this data, close the connection and configure the input buffer to hold 3000 bytes.

    fclose(t)
    t.InputBufferSize = 3000)

    Reopen the connection to the instrument.

    fopen(t)

    Read the file sin.wfm from the function generator.

    fprintf(t,'MMEMORY:DATA? "sin.wfm" ')
    data = fread(t,t.BytesAvailable);

    The next set of commands reads the same waveform as a float32 array. To begin, write the waveform to the AWG.

    fprintf(t,'MMEMORY:DATA? "sin.wfm" ')

    Read the file header as ASCII characters.

    header1 = fscanf(t)
    header1 =
    #42544MAGIC 1000

    Read the next six bytes, which specify the length of data.

    header2 = fscanf(t,'%s',6)
    header2 =
    #42500

    Read the waveform using float32 precision and read the markers using uint8 precision. Note that one float32 value consists of four bytes. Therefore, the following commands read 2500 bytes.

    data = zeros(500,1);
    marker = zeros(500,1);
    for i = 1:500,
    	data(i) = fread(t,1,'float32');
    	marker(i) = fread(t,1,'uint8');
    end

    Read the remaining data, which consists of clock information and termination characters.

    clock = fscanf(t);
    cleanup = fread(t,2);
  4. Disconnect and clean up — When you no longer need t, you should disconnect it from the host, and remove it from memory and from the MATLAB workspace.

    fclose(t)
    delete(t)
    clear t
Was this topic helpful?