%% Binary Data Read from the 8753/8720 Network Analyzers
%
% This is the machine generated representation of an instrument control
% session. The instrument control session comprises all the steps you are
% likely to take when communicating with your instrument. These steps are:
%
% 1. Create an instrument object
% 2. Connect to the instrument
% 3. Configure properties
% 4. Write and read data
% 5. Disconnect from the instrument
%
% To run the instrument control session, type the name of the M-file,
% ag8753_get_form5_binary_data, at the MATLAB command prompt.
%
% The M-file, AG8753_GET_FORM5_BINARY_DATA.M must be on your MATLAB PATH. For additional information
% on setting your MATLAB PATH, type 'help addpath' at the MATLAB command
% prompt.
%
% Example:
% ag8753_get_form5_binary_data;
%
% See also SERIAL, GPIB, TCPIP, UDP, VISA.
%
% ojd - 1/12/2009
%
%% Create a VISA-GPIB object.
%Disconnect and delete all current instrument objects
instrreset;
visa_addr='GPIB5::16::INSTR';
obj1 = instrfind('Type', 'visa', 'RsrcName', visa_addr, 'Tag', '');
% Create the VISA-GPIB object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
obj1 = visa('agilent', visa_addr);
else
fclose(obj1);
obj1 = obj1(1);
end
% Max out buffer size for largest possible read of 1601 points in FORM5
set(obj1,'InputBufferSize', 13000);
% Connect to instrument object, obj1.
fopen(obj1);
%% Communicating with instrument object, obj1.
% Get and display ID String
data1 = query(obj1, 'OUTPIDEN');
fprintf(1, 'Connected to: %s\n', data1);
% Set data return format and trigger a single measurement
fwrite(obj1, 'FORM5; OPC?; SING');
opc_comp=fscanf(obj1);
%% Read data back from analyzer
%Ask for data
fwrite(obj1, 'OUTPDATA');
%Read out #A from binblock
temp=fread(obj1, 2, 'char');
%Read out block size
temp=fread(obj1, 1, 'uint16');
%Read out trace data
data1=fread(obj1, temp/4, 'float32');
points=temp/(4*2);
%Flush the buffer
clrdevice(obj1);
%Allocate space for data array
data=zeros(points,2);
%Reshape output array into two columns
for i=1:1:points
data(i,1)=data1((2*i)-1);
data(i,2)=data1(2*i);
end
%% Calculate Amplitude and Plot
%Allocate space for magnitude array
mag=zeros(points);
%Calculate Magnitude
for i=1:1:points
mag(i)=20*log10(sqrt(data(i,1)^2+data(i,2)^2));
end
plot(mag);
%% Clean-up
% Disconnect all objects.
fclose(obj1);
% Clean up all objects.
delete(obj1);