Code covered by the BSD License  

Highlights from
MATLAB GUI Example for Agilent N8241A Arbitrary Waveform Generator

image thumbnail
agilentN8214A_simulator(visaResourceString)
% Creates a simulated arbitrary waveform generator device
function f = agilentN8214A_simulator(visaResourceString)

fprintf('Setting up simulated connection\n');

f.initialize =  @initializeArb;
f.setWaveform = @setWaveform;
f.startPlay = @startPlay;
f.stopPlay = @stopPlay;
f.close = @close;

% it is possible to configure a waveform while 
% another one is playing.
currentWaveform = [];
configuredWaveform = [];

handles = [];
timerObj = [];

%%
    function initializeArb
        if isempty(handles)
            disp('Initializing AWG');
            handles.figure = figure('name', 'Simulated output of AWG', 'toolbar','none', 'deleteFcn', @figCloseCallback);
            handles.axes = axes('parent', handles.figure);
            handles.line = plot(nan,nan, 'parent', handles.axes);
            title(handles.axes, 'STATUS: AWG INITIALIZED');
            timerObj = timer('Period', 0.1, 'executionMode', 'fixedrate', 'timerFcn', @timerCallback);
        end
    end

    function setWaveform(x)
        if isempty(handles)
            disp('AWG is not initialized!');
        else
            configuredWaveform = x;
            title(handles.axes, 'STATUS: CURRENT WAVEFORM CHANGED');
        end
    end

    function startPlay        
        if isempty(handles)
            disp('AWG is not initialized!');
        else 
            if ~strcmp(get(timerObj,'running'), 'on')
                disp('starting play ...');
                currentWaveform = configuredWaveform;
                set(handles.line, 'visible', 'on', ...
                    'xdata', 1:length(currentWaveform), 'ydata', currentWaveform);
                axis(handles.axes, 'tight');
                title(handles.axes, 'STATUS: GENERATING ...');
                start(timerObj);
            end
        end        
    end

    function stopPlay
        if isempty(handles)
            disp('AWG is not initialized!');
        else        
            disp('stopping play.');
            stop(timerObj);
            currentWaveform = [];
            set(handles.line, 'visible', 'off');
            title(handles.axes, 'STATUS: GENERATION STOPPED');
        end
    end

    function close
        if isempty(handles)
            disp('AWG is not initialized!');
        else        
            disp('closing connection to AWG');
            delete(handles.figure);
            handles = [];
        end
    end

%% ------------------------
    function timerCallback(hobj, eventdata)
        % do a circular shift
        if ~isempty(handles)
            shiftlen=max(1, round(length(currentWaveform)/20));
            currentWaveform = currentWaveform( [end-shiftlen:end 1:end-shiftlen-1] );
            set(handles.line, 'ydata', currentWaveform);
            % this next line forces all the waveforms to be displayed on a 
            % common time axes, so it is clear when the frequency is
            % changing (e.g., during a parameter scan).
            xlim(handles.axes, [1 1000]);
        end
    end

    function figCloseCallback(hobj, eventdata)
        handles = [];
        stop(timerObj);
        delete(timerObj);
        timerObj = [];
    end
end

Contact us at files@mathworks.com