function zvl3_vna_lan_s11

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Takes S11 measurement (complex) using the ZVL3 VNA mode 
%Stores the Data files with Time Stamps on the VNA hard disk
%Hamdi Mani 08/29/2011 (hamdi.mani@asu.edu)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% User Settings 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% IP address of the ZVL3
% IP_address = '10.206.155.99'; % ASU Network IP Address 

%Set execution period in Seconds: this is the sampling period
execution_period= 60;  % = 1 minute

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Misc Settings 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc; % Clear command window
close all; % close all objects 
clearvars; %Clear all variable to avoid confusion
delete(timerfindall); %deltete all timers to avoid confusion

global zvl3 %Make the object zvl3 global to be accessed by other functions.


% Number of samples to be captured.
%NSamples = 1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Initiate  communication with the VNA
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Start LAN connection to the ZVL3
% [zvl3, Success] = rs_sa_connect('LAN', NSamples, IP_address);
% if ~Success
%     error('Could not open connection to the ZVL3! Please check connection, IP address...');
% end;

zvl3 = visa('ni', 'TCPIP::10.206.155.99::INSTR');
fopen(zvl3);

% Get ID string from ZVL3
rs_sa_getID(zvl3)

%Enable VNA Screen to update while in remote control mode
fprintf(zvl3,'SYSTem:DISPlay:UPDate on');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%VNA SETTING (SET the ZVL3 mode to VNA manually) 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Preset instrument to factory default (VNA Mode)
fprintf(zvl3,'*RST'); 

%Delete current Channel
fprintf(zvl3,'CONFigure:CHANnel OFF');

%Load VNA settings file (*zwa file which contains calibration)
%NEEDS TO BE RUN TWICE TO EXECUTE on the VNA (NOT SURE WHY !)
fprintf(zvl3,'MMEM:LOAD:NWAN 1,"C:\Edges_s11_08302011\1portcal_08302011.nwa"');
fprintf(zvl3,'MMEM:LOAD:NWAN 1,"C:\Edges_s11_08302011\1portcal_08302011.nwa"');

%Give a name for the Measurement Channel (not important!)
fprintf(zvl3,'CONFigure:CHANnel:name "EDGES ANT"');

%THE VNA SETTINGS FOR THIS MEAS:
%100-200MHz/ 1001 sweep points (100KHz freq step)
%Test port power: +20dBm (max power)
%IF bandwidth:100Hz
%Averaging: initially turned off 
%Ref pos:10 ; Ref value: 0dB ; 5dB /Div 
%Trace width:1 / Trace Color:Yellow on Black background

%The following lines of code are used to set the VNA using remote commands.
%not needed for our current measurement

%Set the start frequency
fprintf(zvl3,'FREQ:START %sMHz; WAI','10');

%Set the STOP frequency
fprintf(zvl3,'FREQ:STOP %sMHz; WAI','500');

%Set the frequency step size(define the number of freq points/sweep)
fprintf(zvl3,'SWE:STEP %sMHz; WAI','1');

%Set the sweep mode (Continuous/Single Sweep)
%fprintf(zvl3,'INIT:CONT %s; WAI','OFF')%Single sweep mode
fprintf(zvl3,'INIT:CONT %s; WAI','ON');

%Set the marker ON/OFF
fprintf(zvl3,'CALC:MARK %s; WAI','ON');
%fprintf(zvl3,'CALC:MARK:X %sMHZ; WAI','1000')
%fprintf(zvl3,'CALC:MARK:MAX')
%fprintf(zvl3,'CALC:MARK:MIN')
%fprintf(zvl3,'CALC:MARK:TRACE  %s; WAI','1')

%Set the trigger mode 
fprintf(zvl3,'TRIG:MODE %s; WAI','IMM');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%START taking data periodically and save it on the Instrument
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

while (true)
    
%Define filename and store location
k=datestr(now);% Output should be like this: 15-Aug-2011 23:50:55
%replace the ':' by '-' to be able to save.
for i=1:length(k)
    if k(i)==':'
        k(i)='-';
    end
end

filename=strcat('C:\S11_',k,'.s1p');

%Turn OFF Averaging    
fprintf(zvl3,'AVER OFF ');

%Set Averaging Factor
fprintf(zvl3,'AVER:COUN 10');% average 10 sweeps

%Turn ON averaging
fprintf(zvl3,'AVER ON');

pause(30);

%Save the Files
fprintf(zvl3,'MMEM:STOR:TRAC ''TRC1'', ''%s''',filename );

% Make sure that the averaging is done before storing data
% the most reliable way to do is to check the sweep time SWT  with your settings
% and pause for SWT + 2 seconds for safety on line #130
pause(execution_period);
end


end







