%{
Daryl Spencer
GPIB Code for Agilent E4407B
Notes: FETCH command to pull current data; READ command to run a sweep then fetch data
%}
clear all;close all;instrreset
clc
tic
% file = 'Delay2_ESA_Agilent E8257D,13dBm,20GHz,FMRate1kHz,FMDev100Hz,DelayedLine,10dBATT,2p25Vmod,1p84Vlaser,2p4mApd';
file = 'MLL_passive_700mASOA_1p8Vmod_0p6mAPD_3MHzRBWVBW';
obj1 = gpib('NI', 0, 18);
% obj1.Timeout=15;
obj1.InputBufferSize=200000;
% obj1.OutputBufferSize=20000;
fopen(obj1);
% fprintf(obj1,'*IDN?')
%%% Query current trace data %%%
pts=str2num(query(obj1,'SENS:SWE:POIN?'));
f1=str2num(query(obj1, 'SENS:FREQ:START?'));
f2=str2num(query(obj1, 'SENS:FREQ:STOP?'));
% rbw=str2num(query(obj1, 'SENS:BAND|BWIDTH?'));
fprintf(obj1,'FORM:DATA: ASC');
pow=str2num(query(obj1,'TRAC:DATA? TRACE1'));
pks=str2num(query(obj1,':TRACe:MATH:PEAK?'));
%%% Set sweep properties and run a sweep %%%
% fprintf(obj1,'SENS:SWE:POIN 8192')); %8192 Max data points
% fprintf(obj1,'SENS:FREQ:CENT 13.25 GHZ'); Set center frequency
% fprintf(obj1,'SENS:FREQ:SPAN 26.5 GHZ'); %Set span; Max: 26.5 GHZ
% fprintf(obj1,'INIT:CONT 0'); %Trigger continuous sweep
% fprintf(obj1,'INIT:IMM;*WAI'); %Trigger single sweep and wait
% fprintf(obj1,'UNIT:POW DBM'); %dBm units
fclose(obj1);
delete(obj1);
%% Plot data
freq=linspace(f1,f2,pts);
H1=figure;plot(freq,pow,'b.');grid;
xlim([min(freq) max(freq)]);
xlabel('RF Frequency (Hz)');ylabel('RF Power (dBm)');
%% Save file and images
save([file 'data' datestr(now,30)])
saveas(H1,[file datestr(now,30) '.png'])
saveas(H1,[file datestr(now,30) '.fig'])
% ylim([-100 0]);
toc;