Code covered by the BSD License  

Highlights from
Agilent Spectrum Analyzer Seminar Examples

image thumbnail
from Agilent Spectrum Analyzer Seminar Examples by Richard Overdorf
Agilent MXA, PSA Seminar Examples

tuneAndListen(varargin)
function varargout = tuneAndListen(varargin)
% TUNEANDLISTEN M-file for tuneAndListen.fig
%      TUNEANDLISTEN, by itself, creates a new TUNEANDLISTEN or raises the existing
%      singleton*.
%
%      H = TUNEANDLISTEN returns the handle to a new TUNEANDLISTEN or the handle to
%      the existing singleton*.
%
%      TUNEANDLISTEN('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in TUNEANDLISTEN.M with the given input arguments.
%
%      TUNEANDLISTEN('Property','Value',...) creates a new TUNEANDLISTEN or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before tuneAndListen_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to tuneAndListen_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help tuneAndListen

% Last Modified by GUIDE v2.5 23-Apr-2007 21:33:57

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @tuneAndListen_OpeningFcn, ...
                   'gui_OutputFcn',  @tuneAndListen_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before tuneAndListen is made visible.
function tuneAndListen_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to tuneAndListen (see VARARGIN)

% Choose default command line output for tuneAndListen
handles.output = hObject;
handles.hAcqTimer = timer('Name','Acquisition Timer',...
    'ExecutionMode','fixedSpacing',...
    'Period', .2, ...
    'TimerFcn',@gooseAcquisition,...
    'StopFcn',@acquisitionStop, ...
    'UserData',hObject);

handles.QueryState = 'none';
handles.acqState = 'stopped';
handles.groupName = 'AgilentTechnologiesTuneAndListen';
handles.data = zeros(2,1);  % space for acquisition data
handles.samplePeriod = 1;
handles.preAmpAvail = false;
handles.params = struct('Frequency',getpref(handles.groupName,'Frequency',100.1e6),...
    'Bandwidth',getpref(handles.groupName,'Bandwidth',100e3),...
    'DemodulationType',getpref(handles.groupName,'DemodulationType','am'),...
    'DemodulationTime',getpref(handles.groupName,'DemodulationTime',1),...
    'MaxRange',getpref(handles.groupName,'MaxRange',-10),...
    'AutoRange',getpref(handles.groupName,'AutoRange','on'),...
    'HostName',getpref(handles.groupName,'HostName','localhost'));
updateGuiFromParams(handles);
handles.mxa = visa('agilent','tcpip0::localhost::instr');

set(hObject,'UserData','nothing');

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes tuneAndListen wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = tuneAndListen_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


function updateGuiFromParams(handles)
set(handles.frequencyText, 'String', sprintf('%f', handles.params.Frequency/1e6));
set(handles.bandwidthText, 'String', sprintf('%8.2f', handles.params.Bandwidth/1e3));
set(handles.timeText, 'String', sprintf('%8.0f', handles.params.DemodulationTime * 1000));
set(handles.powerText, 'String', sprintf('%8.0f', handles.params.MaxRange));

if strcmp(handles.params.DemodulationType,'fm'),
    hObject = handles.fmRadioButton;
else
    hObject = handles.amRadioButton;
end
set(handles.demodPanel,'SelectedObject', hObject);

if strcmp(handles.params.AutoRange,'on'),
    value = get(handles.autoRangeCheckBox,'Max');
    set(handles.powerText,'Enable','off');
else
    value = get(handles.autoRangeCheckBox,'Min');
    set(handles.powerText,'Enable','on');
end
set(handles.autoRangeCheckBox,'Value',value);

% --- Executes on button press in startButton.
function startButton_Callback(hObject, eventdata, handles)
% hObject    handle to startButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
buttonText = get(hObject, 'String');
if strcmp(buttonText, 'Start'),
    set(handles.startButton,'String','Stop');
    set(hObject,'UserData','acquire');
    enableParameterControls(handles,false);
    start(handles.hAcqTimer);
else
    updateStatus(handles.statusText, 'stopping acquisition...')
    set(handles.startButton,'String','Start');
    stop(handles.hAcqTimer);
end
% drawnow

% ---- Executes on figure close.
function CloseFigure_Callback(hObject,eventdata,handles)
disconnect(hObject,handles, true);
closereq

function gooseAcquisition(hTimer,varargin)
hGui = get(hTimer,'UserData');
handles = guidata(hGui);
updateStatus(handles.statusText, 'acquiring data...');

% setup attenuation, centerfrequency, bwTag, acq_time, and preamp
if strcmp(handles.params.AutoRange, 'on')
    range = -100;
else
    range = handles.params.MaxRange;
end
try
    saListen(handles.mxa, handles.params.Frequency, ...
        handles.params.Bandwidth, ...
        handles.params.DemodulationTime, ...
        range, ...
        handles.params.DemodulationType, ...
        1, 1, ...
        @updateStatus, handles.statusText);
catch
    updateStatus(handles.statusText, 'Error occured.');
    lerrInfo = lasterror;
    updateStatus(handles.statusText, lerrInfo.message);
    stop(handles.hAcqTimer);
end
    
% --- Called when the acquisition timer stops
function acquisitionStop(hTimer, varargin)
hObject = get(hTimer,'UserData');
handles = guidata(hObject);
enableParameterControls(handles,true)
updateStatus(handles.statusText, 'Acquisition stopped.');
set(handles.startButton, 'String', 'Start');
% updateStatus(handles.statusText, 'Acquisition stopping...');

function updateStatus(hStatus, str)
existing_str = get(hStatus,'String');
[m,n] = size(existing_str);
if m > 4,
    existing_str = existing_str(m-3:m,:);
end
new_str = strvcat(existing_str, str);
set(hStatus, 'String', new_str);
drawnow

% --- Executes on button press in closeButton.
function closeButton_Callback(hObject, eventdata, handles)
% hObject    handle to closeButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close(gcbf)

% --- displays an error dialog if an error occurs
function dispErrorDialog(errorMessage, dialogTitle, hStatus, statusText)
errordlg(errorMessage,dialogTitle);
updateStatus(hStatus, statusText);

% --- Executes on button press in connectButton.
function connectButton_Callback(hObject, eventdata, handles)
% hObject    handle to connectButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
stateText = get(hObject,'String');
    
if strcmp(stateText,'Connect'),
    answer = inputdlg({'Enter hostname of MXA or PSA:'},...
        'MXA Hostname',1, {handles.params.HostName});
    
    if isempty(answer),
        % the user pressed the cancel button so get out of here.
        return
    end
    fclose(handles.mxa);
    delete(handles.mxa);
    handles.params.HostName = answer{1};
    updateStatus(handles.statusText, sprintf('Connecting to %s',handles.params.HostName));
    errorOccurred=0;
    try
        handles.mxa = visa('agilent',['tcpip0::' handles.params.HostName '::instr']);
        % set(handles.mxa,'RemoteHost',handles.params.HostName);
        fopen(handles.mxa);
        fclose(handles.mxa);
    catch
        errorOccurred=1;
    end
    if ~errorOccurred,
        updateStatus(handles.statusText, 'Successfully connected.')
        setpref(handles.groupName,'HostName',handles.params.HostName)
        set(handles.startButton,'Enable','on');
        set(hObject, 'String', 'Disconnect');
    else
        err=lasterror();
        dispErrorDialog(err.message, 'Instrument Connect Error', ...
            handles.statusText, 'Instrument connect failed.');
        disconnect(hObject,handles,false);
    end
else
    disconnect(hObject, handles, false);
    set(hObject, 'String', 'Connect');
end    
guidata(handles.output, handles);

function disconnect(hObject, handles, deleteObjects)
running = get(handles.hAcqTimer,'Running');
if strcmp(running,'on'),
    stop(handles.hAcqTimer)
end
if strcmp(get(handles.mxa,'Status'),'open'),
    fclose(handles.mxa)
    set(handles.startButton, 'Enable', 'off');
end
if deleteObjects
    delete(handles.hAcqTimer);
    delete(handles.mxa)
end
% waitfor(hObject,'UserData','nothing');

function text_Callback(hObject, eventdata, handles)
% hObject    handle to Text object (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of bandwidthText as text
%        str2double(get(hObject,'String')) returns contents of bandwidthText as a double
tag = get(hObject,'Tag');
if strcmp(tag,'autoRangeCheckBox'),
    val=get(hObject,'Value');
    if val == 0,
        handles.params.AutoRange='off';
    else
        handles.params.AutoRange='on';
    end
else
    val=str2double(get(hObject,'String'));
    if ~isnan(val),
        switch tag
            case 'bandwidthText'
                if val<10,
                    val = 10;
                elseif val > 10e6,
                    val = 10e6;
                end
                handles.params.Bandwidth = val * 1e3;
                setpref(handles.groupName,'Bandwidth',handles.params.Bandwidth);
            case 'frequencyText'
                if val < 0,
                    val = 0;
                end
                handles.params.Frequency = val * 1e6;
                setpref(handles.groupName,'Frequency',handles.params.Frequency);
            case 'powerText'
                if val ~= round(val),
                    val = round(val);
                end
                handles.params.MaxRange = val;
                setpref(handles.groupName,'MaxRange',handles.params.MaxRange);
            case 'timeText'
                if val ~= round(val),
                    val = round(val);
                end
                if val < 250,
                    val = 250;
                end
                handles.params.DemodulationTime = val/1000;
                setpref(handles.groupName,'DemodulationTime',handles.params.DemodulationTime);
        end
    end
end
updateGuiFromParams(handles);
guidata(handles.output, handles);

% --- Executes during object creation, after setting all properties.
function text_CreateFcn(hObject, eventdata, handles)
% hObject    handle to bandwidthText (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --------------------------------------------------------------------
function demodPanel_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to demodPanel (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
tag=get(hObject,'Tag');
if strcmp(tag,'fmRadioButton'),
    val = 'fm';
else
    val = 'am';
end
handles.params.DemodulationType = val;
setpref(handles.groupName,'DemodulationType',val);
guidata(handles.output, handles);

function enableParameterControls(handles,enable)
if enable,
    str = 'on';
else
    str = 'off';
end
set(handles.frequencyText,'Enable',str);
set(handles.bandwidthText,'Enable',str);
set(handles.timeText,'Enable',str);
set(handles.frequencyText,'Enable',str);
set(handles.amRadioButton,'Enable',str);
set(handles.fmRadioButton,'Enable',str);
set(handles.powerText,'Enable',str);
set(handles.autoRangeCheckBox,'Enable',str);

Contact us