function Data=getOSAtrace(varargin)
% getOSATrace()
% Opens connection to Ando OSA AQ6317B or Anritsu (HP) MS9030B, 86140(A
% and maybe B) at specified GPIB address and retrieves wavelength and level
% data from specified trace
%
% input:
% getOSATrace() - retrives trace A from GPIB address 1
% getOSATrace(GPIB,'Trace') - retrives trace 'Trace'('A','B' or 'C')
% from GPIB address GPIB (1,2,...) if 'Trace' is a number it gets it from
% the internal memory with the same number (for ANDO OSA only).
% -------------------------------------------------------------------------
% cc Micke Malmstrm
% Version 2012-02-14
% Set default values
if nargin==0
GPIB=1;
ABC='A';
else
GPIB=varargin{1};
ABC=varargin{2};
end
obj1 = instrfind('Type', 'gpib', 'BoardIndex', 0, 'PrimaryAddress', GPIB, 'Tag', '');
if isempty(obj1)
g1=gpib('ni',0,GPIB);
else
fclose(obj1);
g1 = obj1(1);
end
set(g1,'InputBufferSize',8*20001); % Standard is 512 ASCII signs that transfers
set(g1,'Timeout',30);
fopen(g1); % Open connection to OSA
status=get(g1,'status'); % Check if connection successful
if strcmp(status,'closed')
disp(['Connection to GPIB' GPIB ' failed'])
end
idn=query(g1,'*IDN?');
if strfind(idn,'MS9030B') % Check what typ of OSA it is
level=str2num(query(g1,['DM' ABC '?'])); % Acquire level data
sta=(query(g1,['STA?']));
sta=str2num(sta(4:end))*1e-6;
sto=(query(g1,['STO?']));
sto=str2num(sto(4:end))*1e-6;
wavelength=linspace(sta,sto,length(level))';
elseif strfind(idn,'86140') %Its a HP (Anritzu) 86140 (A or B)
% Acquire level data
level=str2num(query(g1,['TRACe:DATA:Y? TR' ABC ]))';
% Acquire wavelength data
sta=str2num(query(g1,['TRACe:DATA:X:STARt? TR' ABC]));
sto=str2num(query(g1,['TRACe:DATA:X:STOP? TR' ABC]));
wavelength=linspace(sta,sto,length(level))';
else % Asume it's an ANDO OSA
if isstr(ABC) % if string it is trace A,B or C
% Acquire wavelength data
wave=str2num(query(g1,['WDAT' ABC ]));
% Acquire level data
value=str2num(query(g1,['LDAT' ABC ]));
else % it is in memory
% Acquire wavelength data
wave=str2num(query(g1,['WMEM' num2str(ABC)]));
% Acquire level data
value=str2num(query(g1,['LMEM' num2str(ABC)]));
end
% Set output, first entry is only number of data points
wavelength=wave(2:end)';
level=value(2:end)';
end
fclose(g1) % Close connection to GPIB
set(g1,'InputBufferSize',512); % Reset InputBufferSize to standard
Data=[wavelength level];
if nargin==0
try plotOSA(Data); end
end