Skip to content

Commit

Permalink
Get usb*.m functions working with VISA devices.
Browse files Browse the repository at this point in the history
  • Loading branch information
nsdecicco committed Sep 18, 2017
1 parent 037b044 commit ed50bee
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 160 deletions.
144 changes: 55 additions & 89 deletions usbgetscopedat.m
Original file line number Diff line number Diff line change
@@ -1,132 +1,98 @@
function [data, t] = getscopedat(GPIB, Channel)
function [data, t] = usbgetscopedat(deviceId, Channel)
%--------------------------------------------------------------------------
% MATLAB Driver for Inif(i)ium Scopes
% Last Update: 2.21.11
% Christopher Tilley, Philip Mease
% MATLAB script for obtaining data from Agilent oscilloscopes
% Based on code dated 2/21/11 by Christopher Tilley, Philip Mease
% Modified 9/15/17 by Nicholas DeCicco
%--------------------------------------------------------------------------
%
% This function will access the oscilloscope that is located at GPIB
% address 'GPIB(1,1)' and board index 'GPIB(1,2)' and plot the current wave
% form being displayed on channel 'Channel'. The vector of amplitudes will
% be saved to the variable 'data', and the time vector will be saved to
% This function will access the oscilloscope that is located at VISA address
% 'deviceId' and obtain data from channel 'Channel'. The vector of amplitudes
% will be saved to the variable 'data', and the time vector will be saved to
% 't'.
%
% GPIB can be sent in two forms. If 'GPIB' is a single value variable then
% the function will interpuret 'GPIB' as the GPIB address of the scope and
% use the default board index of 8. If the user wishes to designate the
% board index for the GPIB board in the computer they are using then 'GPIB'
% must be saved as a 1x2 vector, where 'GPIB(1,1)' is the GPIB address of
% the instrument and 'GPIB(1,2)' is the board index of the GPIB adapter in
% the computer. This board index can be found by opening the Agilent
% Control Panel in Windows.
%
% The following are the acceptable syntax for this function:
%
%
% [data, t] = getscopedat(GPIB)
% [data, t] = getscopedat(deviceId)
%
% This syntax uses the default channel 1 of the scope at GPIB
% address 'GPIB(1,1)' and board index 'GPIB(1,2)'
% eg (bi = 8, gpibaddY = 7): [d t] = getscopedat([7 8])
% This syntax uses the default channel 1 of the scope at VISA
% address 'deviceId'.
%
%
% [data, t] = getscopedat(GPIB, Channel)
%
% This syntax uses the channel 'Channel' of the scope at GPIB
% address 'GPIB(1,1)' and board index 'GPIB(1,2)'.
% This syntax uses the channel 'Channel' of the scope at VISA
% address 'deviceId'.
%
% Note that 'Channel' is a string such as 'CHAN1', 'CHAN2', etc.; this is to
% permit also accessing MATH functions on the oscilliscope. If, for example,
% MATH1 is displaying an FFT of a channel, you can pass 'FUNC1' for 'Channel'.
%--------------------------------------------------------------------------


%Determine number of inputs provided by user
if nargin > 2, error('Invalid Number of Arguments for viewscope(). Please type help getscopedat for for proper syntax!'); end
if nargin < 2, Channel = 1; end
if nargin < 1, error('Invalid Number of Arguments for viewscope(). Must enter the GPIB Address of the Scope!'); end

%Create a variable name for the scope object
if (length(GPIB) > 1)
board = num2str(GPIB(1,2));
name = ['obj', num2str(GPIB(1,1))];
obj_name = genvarname(name);
else
board = '8';
name = ['obj', num2str(GPIB)];
obj_name = genvarname(name);
open = instrfind('Status', 'open');
if ~isempty(open)
fclose(open);
end

% Create a GPIB object:
eval([obj_name, ' = instrfind(','''Type''', ',', '''gpib''', ',', '''BoardIndex''', ',', '''', board, '''', ',', '''PrimaryAddress''', ',', num2str(GPIB(1,1)), ',', '''Tag''', ',', '''''', ');'])
dev = visa('agilent', deviceId);
fopen(dev);

% Create the GPIB object if it does not exist
% otherwise use the object that was found.
if isempty(eval(obj_name))
eval([obj_name, ' = gpib(', '''AGILENT''', ', ', board, ', ', num2str(GPIB(1,1)), ');']);
else
fclose(eval(obj_name));
eval([obj_name, ' = ', obj_name, '(1)'])
%Determine number of inputs provided by user
if nargin > 2
error(['Invalid Number of Arguments for getscopedat(). ', ...
'Please type help getscopedat for for proper syntax!']);
elseif nargin < 2
Channel = 'CHAN1';
elseif nargin < 1
error(['Invalid Number of Arguments for getscopedat(). ', ...
'Must enter the GPIB Address of the Scope!']);
end

%Set the Input Buffer size:
% NOTE: this is arbitrary until we check. But you cannot set this
% while the object is open... can only be set with objs closed.
ibs = 2100100;
set(eval(obj_name), 'InputBufferSize', ibs);

% POINts sets req memory depth
% WAVeform:POINts?
% WAVeform:PREamble? query for
% :ACQuire:POINts? returns val of mem depth ctrl
% :ACQuire:SRATe? returns current acquisition sa rate

%Open Scope
fopen(eval(obj_name));

% Check memory:
memdepth = str2double(query(eval(obj_name),':ACQuire:POINts?'));
str = ['You are acquiring ', num2str(memdepth), ' points.']
% DSO1012A have a model id of 0x0588:
% USB0::0x0957::0x0588::CN50382775::0::INSTR
deviceIdParts = strsplit(deviceId,':');
if strcmp(deviceIdParts{3},'0x0588')
memdepth = str2double(query(dev,':WAVeform:POINts?'));
else
memdepth = str2double(query(dev,':ACQuire:POINts?'));
end
disp(['Acquiring ', num2str(memdepth), ' points.']);

if (memdepth >= ibs)
disp('READ MEMORY BUFFER TOO SMALL, WAIT WHILE WE INCREASE IT');
ibs = memdepth + 1; % update ibs to fit aquired data
open = instrfind('Status', 'open');
fclose(open);
set(eval(obj_name), 'InputBufferSize', ibs);
fopen(eval(obj_name)); % Reopen the object now buffer is set correctly
if (memdepth >= dev.InputBufferSize)
disp('Increasing insufficient input buffer size.');
fclose(dev);
dev.InputBufferSize = memdepth+1;
fopen(dev);
end

%Set the Source of the Scope
%fprintf(eval(obj_name), ':ACQ:SRAT 500 E+4');
cha = [':WAVEFORM:SOURCE CHAN', num2str(Channel)];
fprintf(eval(obj_name), cha);
fprintf(dev, [':WAVEFORM:SOURCE ', Channel]);

%Acquire X origin and X increment values
xorg = str2double(query(eval(obj_name),':WAVEFORM:XOR?'));
xinc = str2double(query(eval(obj_name),':WAVEFORM:XINC?'));
xorg = str2double(query(dev, ':WAVEFORM:XOR?'));
xinc = str2double(query(dev, ':WAVEFORM:XINC?'));

%Acquire Y origin and Y increment Values
yorg = str2double(query(eval(obj_name),':WAVEFORM:YOR?'));
yinc = str2double(query(eval(obj_name),':WAVEFORM:YINC?'));
yref = str2double(query(eval(obj_name),':WAVEFORM:YREF?'));
yorg = str2double(query(dev, ':WAVEFORM:YOR?'));
yinc = str2double(query(dev, ':WAVEFORM:YINC?'));
yref = str2double(query(dev, ':WAVEFORM:YREF?'));

%Setup the Scope for proper output format
fprintf(eval(obj_name), 'WAVEFORM:FORMAT BYTE');
fprintf(eval(obj_name), 'ACQUIRE:TYPE NORM');
fprintf(dev, 'WAVEFORM:FORMAT BYTE');
fprintf(dev, 'ACQUIRE:TYPE NORM');


%Instruct the scope to make current data available to acquire
fprintf(eval(obj_name), 'WAVEFORM:DATA?');
data1 = binblockread(eval(obj_name), 'uint8');
fprintf(dev, 'WAVEFORM:DATA?');
data1 = binblockread(dev, 'uint8');

%Create a time variable with the same lenght as "data"
t1 = 0:(length(data1)-1);

%Adjust data and time variables with x/y increments/origins
t = t1 * xinc + xorg;
data = (data1-yref) * yinc + yorg;

plot(t,data);

open = instrfind('Status', 'open');
fclose(open);

fclose(dev);
end

Loading

0 comments on commit ed50bee

Please sign in to comment.