Instrument Control Toolbox | ![]() ![]() |
Example: Writing and Reading Data with a TCP/IP Object
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 MATLAB, 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.mathworks.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.
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
.
ByteOrder
property to littleEndian
.
sin.wfm
with Waveform File format, a total length of 2544 bytes, and a combined data and marker length of 2500 bytes.
Write the sine wave to the instrument.
Instruct the instrument to use a clock frequency of 100 MS/s for the waveform.
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.
Reopen the connection to the instrument.
Read the file sin.wfm
from the function generator.
The next set of commands reads the same waveform as a float32
array. To begin, write the waveform to the AWG.
Read the file header as ASCII characters.
Read the next six bytes, which specify the length of data.
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.
t
, you should disconnect it from the host, and remove it from memory and from the MATLAB workspace.
![]() | Rules for Completing Write and Read Operations | Example: Writing and Reading Data with a UDP Object | ![]() |