Code covered by the BSD License  

Highlights from
daqSelector

image thumbnail

daqSelector

by

 

08 Oct 2008 (Updated )

GUI selection tool for DAQ hardware

daqSelector
function [DAQAdaptor, DAQBoardNumber] = daqSelector
%DAQSELECTOR GUI selection tool for DAQ hardware
%   [DAQAdaptor, DAQBoardNumber] = DAQSELECTOR launches a GUI selection 
%   tool that automatically finds installed data acquisition boards and 
%   allows the  user to select a board from menus.  DAQAdaptor is the 
%   adaptor for the data acquisition board.  DAQBoardNumber is the board's 
%   identifier.  For example if there are two boards installed from the 
%   same vendor, one board might be listed at address 1 and the other would
%   be listed at address 2.
%
%   The output of the function can be used to create an
%   analoginput/analogutput/digitalio object.  
%
%   EXAMPLE:
%       [DAQAdaptor, DAQBoardNumber] = daqSelector
%       ai = analoginput(DAQAdaptor, DAQBoardNumber);
%
% 	Author: Isaac Noh
%   Copyright The MathWorks, Inc.
%   October 2008, December 2008
%   Version: 1.1

% Set default values as empty
DAQAdaptor = '';
DAQBoardNumber = '';

% Create GUI
h.f = figure(...
    'Units','characters',...
    'Color',[0.8 0.8 0.8],...
    'MenuBar','none',...
    'Name','daqSelector',...
    'NumberTitle','off',...
    'Position',[10.8 25.5 48.8 12],...
    'Resize','on',...
    'HandleVisibility','callback');

% Popup Menus
h.p1 = uicontrol(...
    'Parent',h.f,...
    'Units','characters',...
    'Position',[9.8 8.2 30.2 1.7],...
    'String',{  'Select Board' },...
    'Style','popupmenu',...
    'Value',1,...
    'BackgroundColor',[1 1 1],...
    'Tag','popupmenu1');

h.p2 = uicontrol(...
    'Parent',h.f,...
    'Units','characters',...
    'Callback','',...
    'Position',[9.8 5 30.2 1.7],...
    'String',{  'Select Board Number' },...
    'Style','popupmenu',...
    'Value',1,...
    'BackgroundColor',[1 1 1],...
    'Tag','popupmenu2');

% Push Button
uicontrol(...
    'Parent',h.f,...
    'Units','characters',...
    'Callback','',...
    'Position',[15.4 1.7 18.4 1.8],...
    'String','OK',...
    'Tag','OK',...
    'Callback',{@OK});

% Titles
uicontrol(...
    'Parent',h.f,...
    'Units','characters',...
    'Position',[8.3 9.9 13.5 1.3],...
    'String','Vendor',...
    'Style','text',...
    'Tag','text1',...
    'BackgroundColor',[0.8 0.8 0.8]);

uicontrol(...
    'Parent',h.f,...
    'Units','characters',...
    'Position',[9 6.7 18 1.15],...
    'String','Board Number',...
    'Style','text',...
    'Tag','text2',...
    'BackgroundColor',[0.8 0.8 0.8]);

loadBoards(h);
uiwait(h.f);

    function loadBoards(h)
        
        % Find boards
        InstalledBoards = findBoards;
        guidata(h.f,InstalledBoards);
        
        % Update function for "Board ID" popup menu
        set(h.p1,'Callback',{@setIDpopup,h.p2,InstalledBoards})
        
        % Initialize popup menus
        set(h.p1,'String',[InstalledBoards.name]);
        set(h.p1,'Value',1);
        setIDpopup(h.p1,[],h.p2,InstalledBoards);
        
    end %loadBoards ------------------------------------------------


% Function to find installed boards and their id numbers
    function InstalledBoards = findBoards
        hw = daqhwinfo;
        InstalledBoards = struct('adaptor',{},'name',{},'id',{});
        
        for ind = 1:length(hw.InstalledAdaptors)
            Adapt = daqhwinfo(hw.InstalledAdaptors{ind});
            BoardIds = Adapt.InstalledBoardIds;
            numBoards = length(BoardIds);
            if numBoards > 0
                if numBoards < 2
                    for ind2 = 1:numBoards
                        counted = length(InstalledBoards);
                        InstalledBoards(counted + 1).adaptor = hw.InstalledAdaptors{ind};
                        InstalledBoards(counted + 1).name = Adapt.BoardNames;
                        InstalledBoards(counted + 1).id = Adapt.InstalledBoardIds;
                    end
                else
                    for ind2 = 1:numBoards
                        counted = length(InstalledBoards);
                        InstalledBoards(counted + 1).adaptor = hw.InstalledAdaptors{ind};
                        InstalledBoards(counted + 1).name = Adapt.BoardNames(ind2);
                        InstalledBoards(counted + 1).id = Adapt.InstalledBoardIds(ind2);
                    end
                end
            end
        end
        
    end %findBoards ------------------------------------------------


% Function to update the "Board ID" popup menu so that the entries match
% the "Board" selection
    function setIDpopup(hObject,eventdata,p2,InstalledBoards) %#ok
        selection = get(hObject,'Value');
        str = get(hObject,'String');
        ind = strcmp(str{selection},[InstalledBoards.name]);
        set(p2,'String',[InstalledBoards(ind).id]);
        
    end %setIDpopup -------------------------------------------------


    function OK(hObject,eventdata)    %#ok
        
        % Extract response
        InstalledBoards = guidata(hObject);  %All installed boards data
        contents1 = get(h.p1,'String');  %Get all board names from GUI
        DAQBoard = contents1{get(h.p1,'Value')};  %Get selected board name
        ind = strcmp(DAQBoard,[InstalledBoards(:).name]);  %Get index for selected board
        DAQAdaptor = InstalledBoards(ind).adaptor;  %Get adaptor name for selected board
        
        contents2 = get(h.p2,'String');  %Get all installed board numbers
        DAQBoardNumber = str2double(contents2{get(h.p2,'Value')});  %Get selected board number
        
        % Check for string IDs (i.e. 'Dev1')
        if isempty(DAQBoardNumber)
            DAQBoardNumber = contents2{get(h.p2,'Value')};
        end
        
        close(h.f);
        
    end %OK ---------------------------------------------------------

end %SelectBoardGUI --------------------------------------------

Contact us