Code covered by the BSD License  

Highlights from
Simple GUI for GPIB communication

image thumbnail

Simple GUI for GPIB communication

by

 

Set up a GPIB connection to send and receive messages.

GPIB_GUI(varargin)
function varargout = GPIB_GUI(varargin)
% A simple GUI that allows user to search for GPIB connections, connect
% to them, and send/receive commands.

%  	Author: Roger Yeh
%   Copyright 2010 MathWorks, Inc.
%   Version: 1.0  |  Date: 2010.02.04

% GPIB_GUI M-file for GPIB_GUI.fig
%      GPIB_GUI, by itself, creates a new GPIB_GUI or raises the existing
%      singleton*.
%
%      H = GPIB_GUI returns the handle to a new GPIB_GUI or the handle to
%      the existing singleton*.
%
%      GPIB_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GPIB_GUI.M with the given input arguments.
%
%      GPIB_GUI('Property','Value',...) creates a new GPIB_GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before GPIB_GUI_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to GPIB_GUI_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 GPIB_GUI

% Last Modified by GUIDE v2.5 04-Feb-2010 11:50:32

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @GPIB_GUI_OpeningFcn, ...
                   'gui_OutputFcn',  @GPIB_GUI_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 GPIB_GUI is made visible.
function GPIB_GUI_OpeningFcn(hObject, eventdata, handles, varargin)

gpibAdaptors = instrhwinfo('gpib');
nAdaptors = length(gpibAdaptors.InstalledAdaptors);

% Store the boards. 
handles.gpibBoards = cell(1,nAdaptors);

set(handles.adaptorList, 'String', ...
    [{'Select adaptor'} , gpibAdaptors.InstalledAdaptors ]);

set(handles.adaptorList, 'Value', 1);
set(handles.history_box, 'String', cell(1));

handles.gpibAdaptors = gpibAdaptors;
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GPIB_GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = GPIB_GUI_OutputFcn(hObject, eventdata, handles) 
varargout{1} = handles.output;


% --- Executes on selection change in adaptorList.
function adaptorList_Callback(hObject, eventdata, handles)
tempAdaptor = get(hObject, 'Value') - 1;

if tempAdaptor == 0
%     errordlg('Pick a valid adaptor.')
    set(handles.boardList, 'Value',1);
    set(handles.boardList, 'String', 'No boards, try scanning.');
else
    if isempty(handles.gpibBoards{tempAdaptor})
        set(handles.boardList, 'Value',1);
        set(handles.boardList, 'String', 'No boards, try scanning.');
    else
        set(handles.boardList, 'String', ...
            [{'Select board, primary, secondary address'} , ... 
            handles.gpibBoards{tempAdaptor}]);
        set(handles.boardList, 'Value',2);
    end
end

% --- Executes during object creation, after setting all properties.
function adaptorList_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function history_box_Callback(hObject, eventdata, handles)
% history_box_Callback(hObject, eventdata, handles) 
% only updates, can't be changed

% --- Executes during object creation, after setting all properties.
function history_box_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function Tx_send_Callback(hObject, eventdata, handles)
TxText = get(handles.Tx_send, 'String');
fprintf(handles.gpibConn, TxText);

currList = get(handles.history_box, 'String');

set(handles.history_box, 'String', ...
    [currList ; ['Sent @ ' datestr(now) ': ' TxText] ]);
set(handles.history_box, 'Value', length(currList) + 1 );

set(hObject, 'String', '');

% --- Executes during object creation, after setting all properties.
function Tx_send_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in rxButton.
function rxButton_Callback(hObject, eventdata, handles)
try 
    RxText = fscanf(handles.gpibConn);
    currList = get(handles.history_box, 'String');
    if length(RxText) < 1
        RxText = 'Timeout @ ';
        set(handles.history_box, 'String', ...
            [currList ; [RxText datestr(now)] ]);
    else
        set(handles.history_box, 'String', ...
            [currList ; ['Received @ ' datestr(now) ': ' RxText ] ]);
    end
    set(handles.history_box, 'Value', length(currList) + 1 );
catch e
    disp(e)
end


% --- Executes on button press in connectButton.
function connectButton_Callback(hObject, eventdata, handles)    
if strcmp(get(hObject,'String'),'Connect') % currently disconnected
    % check for valid selection
    adap_n  = get(handles.adaptorList, 'Value') - 1;
    board_n = get(handles.boardList, 'Value') - 1;
    
    if adap_n == 0 || board_n == 0
        errordlg('Select valid adaptor and/or board.');
    else
        gpibConn = eval(handles.gpibBoardsConstruct{adap_n}{board_n});
        
        try
            fopen(gpibConn);
            handles.gpibConn = gpibConn;
            
            % enable Tx text field and Rx button
            set(handles.Tx_send, 'Enable', 'On');
            set(handles.rxButton, 'Enable', 'On');
            
            set(handles.adaptorList, 'Enable', 'Off');
            set(handles.boardList, 'Enable', 'Off');
            
            
            set(hObject, 'String','Disconnect')
        catch e
            errordlg(e.message);
        end
        
    end
else % currently connected
    set(handles.Tx_send, 'Enable', 'Off');
    set(handles.rxButton, 'Enable', 'Off');

    set(handles.adaptorList, 'Enable', 'On');
    set(handles.boardList, 'Enable', 'On');
    
    set(hObject, 'String','Connect')
    fclose(handles.gpibConn);
end
guidata(hObject, handles);


% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isfield(handles, 'gpibConn')
    fclose(handles.gpibConn);
end
instrreset;
% Hint: delete(hObject) closes the figure
delete(hObject);


% --- Executes on selection change in boardList.
function boardList_Callback(hObject, eventdata, handles)
% hObject    handle to boardList (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns boardList contents as cell array
%        contents{get(hObject,'Value')} returns selected item from boardList


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

% Hint: listbox 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


% --- Executes on button press in scanButton.
function scanButton_Callback(hObject, eventdata, handles)
set(handles.figure1,'Pointer','watch');
drawnow;
% guidata(hObject, handles);

gpibAdaptors = handles.gpibAdaptors;
nAdaptors = length(gpibAdaptors.InstalledAdaptors);
gpibBoards = cell(1,nAdaptors);
gpibBoardsConstruct = cell(1,nAdaptors);

for n = 1:nAdaptors;
    tempBoards = instrhwinfo('gpib', gpibAdaptors.InstalledAdaptors{n});
    
    %% use following for debugging purposes
    % if n == 1
    %     tempBoards.ObjectConstructorName = {'gpib(''adv'', 1, 1);', 'gpib(''adv'', 2, 2);'};
    % else
    %     tempBoards.ObjectConstructorName = {'gpib(''agilent'', 1, 1);', 'gpib(''agilent'', 2, 2);'};
    % end
    gpibBoardsConstruct{n}  = tempBoards.ObjectConstructorName;
    gpibBoards{n} = regexprep(tempBoards.ObjectConstructorName, ...
        '.*''(\w+)''(,)\s+(\d+)(,)\s+(\d+).*', '$1$2$3$4$5');
end

% update the adaptor list
set(handles.adaptorList, 'String', ...
    [{'Select adaptor'} , gpibAdaptors.InstalledAdaptors ]);

for n = 1:nAdaptors
    if ~isempty(gpibBoards{n})
        set(handles.adaptorList,'Value',n+1);
        set(handles.boardList, 'String', ...
            [{'Select board, primary, secondary address'}, gpibBoards{n}]);
        set(handles.boardList,'Value',2);
        break;
    end
end

handles.gpibBoardsConstruct = gpibBoardsConstruct;
handles.gpibBoards = gpibBoards;

set(handles.figure1,'Pointer','arrow');
guidata(hObject, handles);

Contact us