This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the 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 and its port is 4000. The AWG's host IP address is 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('',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.

  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])

    Write the sine wave to the instrument.

    for (i = 1:length(data)),

    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.

    t.InputBufferSize = 3000)

    Reopen the connection to the instrument.


    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 =

    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');

    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.

    clear t
Was this topic helpful?