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
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:
Create an instrument object — Create a TCP/IP object associated with the AWG.
t = tcpip('sonytekawg.yourdomain.com',4000);
Connect to the instrument —
Before establishing a connection, the
be large enough to hold the data being written. In this example, 2577
bytes are written to the instrument. Therefore, the
t.OutputBufferSize = 3000
You can now connect
t to the instrument.
and read data — Since the instrument's byte order
is little-endian, configure the
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;
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.
Read the file
the function generator.
fprintf(t,'MMEMORY:DATA? "sin.wfm" ') data = fread(t,t.BytesAvailable);
The next set of commands reads the same waveform
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
the waveform using
float32 precision and read the
uint8 precision. Note that one
consists of four bytes. Therefore, the following commands read 2500
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);
and clean up — When you no longer need
you should disconnect it from the host, and remove it from memory
and from the MATLAB workspace.
fclose(t) delete(t) clear t