Skip to content

Commit ed50bee

Browse files
committed
Get usb*.m functions working with VISA devices.
1 parent 037b044 commit ed50bee

File tree

2 files changed

+104
-160
lines changed

2 files changed

+104
-160
lines changed

usbgetscopedat.m

Lines changed: 55 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,98 @@
1-
function [data, t] = getscopedat(GPIB, Channel)
1+
function [data, t] = usbgetscopedat(deviceId, Channel)
22
%--------------------------------------------------------------------------
3-
% MATLAB Driver for Inif(i)ium Scopes
4-
% Last Update: 2.21.11
5-
% Christopher Tilley, Philip Mease
3+
% MATLAB script for obtaining data from Agilent oscilloscopes
4+
% Based on code dated 2/21/11 by Christopher Tilley, Philip Mease
5+
% Modified 9/15/17 by Nicholas DeCicco
66
%--------------------------------------------------------------------------
77
%
8-
% This function will access the oscilloscope that is located at GPIB
9-
% address 'GPIB(1,1)' and board index 'GPIB(1,2)' and plot the current wave
10-
% form being displayed on channel 'Channel'. The vector of amplitudes will
11-
% be saved to the variable 'data', and the time vector will be saved to
8+
% This function will access the oscilloscope that is located at VISA address
9+
% 'deviceId' and obtain data from channel 'Channel'. The vector of amplitudes
10+
% will be saved to the variable 'data', and the time vector will be saved to
1211
% 't'.
1312
%
14-
% GPIB can be sent in two forms. If 'GPIB' is a single value variable then
15-
% the function will interpuret 'GPIB' as the GPIB address of the scope and
16-
% use the default board index of 8. If the user wishes to designate the
17-
% board index for the GPIB board in the computer they are using then 'GPIB'
18-
% must be saved as a 1x2 vector, where 'GPIB(1,1)' is the GPIB address of
19-
% the instrument and 'GPIB(1,2)' is the board index of the GPIB adapter in
20-
% the computer. This board index can be found by opening the Agilent
21-
% Control Panel in Windows.
22-
%
2313
% The following are the acceptable syntax for this function:
2414
%
2515
%
26-
% [data, t] = getscopedat(GPIB)
16+
% [data, t] = getscopedat(deviceId)
2717
%
28-
% This syntax uses the default channel 1 of the scope at GPIB
29-
% address 'GPIB(1,1)' and board index 'GPIB(1,2)'
30-
% eg (bi = 8, gpibaddY = 7): [d t] = getscopedat([7 8])
18+
% This syntax uses the default channel 1 of the scope at VISA
19+
% address 'deviceId'.
3120
%
3221
%
3322
% [data, t] = getscopedat(GPIB, Channel)
3423
%
35-
% This syntax uses the channel 'Channel' of the scope at GPIB
36-
% address 'GPIB(1,1)' and board index 'GPIB(1,2)'.
24+
% This syntax uses the channel 'Channel' of the scope at VISA
25+
% address 'deviceId'.
26+
%
27+
% Note that 'Channel' is a string such as 'CHAN1', 'CHAN2', etc.; this is to
28+
% permit also accessing MATH functions on the oscilliscope. If, for example,
29+
% MATH1 is displaying an FFT of a channel, you can pass 'FUNC1' for 'Channel'.
3730
%--------------------------------------------------------------------------
3831

39-
40-
%Determine number of inputs provided by user
41-
if nargin > 2, error('Invalid Number of Arguments for viewscope(). Please type help getscopedat for for proper syntax!'); end
42-
if nargin < 2, Channel = 1; end
43-
if nargin < 1, error('Invalid Number of Arguments for viewscope(). Must enter the GPIB Address of the Scope!'); end
44-
45-
%Create a variable name for the scope object
46-
if (length(GPIB) > 1)
47-
board = num2str(GPIB(1,2));
48-
name = ['obj', num2str(GPIB(1,1))];
49-
obj_name = genvarname(name);
50-
else
51-
board = '8';
52-
name = ['obj', num2str(GPIB)];
53-
obj_name = genvarname(name);
32+
open = instrfind('Status', 'open');
33+
if ~isempty(open)
34+
fclose(open);
5435
end
5536

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

59-
% Create the GPIB object if it does not exist
60-
% otherwise use the object that was found.
61-
if isempty(eval(obj_name))
62-
eval([obj_name, ' = gpib(', '''AGILENT''', ', ', board, ', ', num2str(GPIB(1,1)), ');']);
63-
else
64-
fclose(eval(obj_name));
65-
eval([obj_name, ' = ', obj_name, '(1)'])
40+
%Determine number of inputs provided by user
41+
if nargin > 2
42+
error(['Invalid Number of Arguments for getscopedat(). ', ...
43+
'Please type help getscopedat for for proper syntax!']);
44+
elseif nargin < 2
45+
Channel = 'CHAN1';
46+
elseif nargin < 1
47+
error(['Invalid Number of Arguments for getscopedat(). ', ...
48+
'Must enter the GPIB Address of the Scope!']);
6649
end
6750

68-
%Set the Input Buffer size:
69-
% NOTE: this is arbitrary until we check. But you cannot set this
70-
% while the object is open... can only be set with objs closed.
71-
ibs = 2100100;
72-
set(eval(obj_name), 'InputBufferSize', ibs);
73-
74-
% POINts sets req memory depth
75-
% WAVeform:POINts?
76-
% WAVeform:PREamble? query for
77-
% :ACQuire:POINts? returns val of mem depth ctrl
78-
% :ACQuire:SRATe? returns current acquisition sa rate
79-
80-
%Open Scope
81-
fopen(eval(obj_name));
82-
8351
% Check memory:
84-
memdepth = str2double(query(eval(obj_name),':ACQuire:POINts?'));
85-
str = ['You are acquiring ', num2str(memdepth), ' points.']
52+
% DSO1012A have a model id of 0x0588:
53+
% USB0::0x0957::0x0588::CN50382775::0::INSTR
54+
deviceIdParts = strsplit(deviceId,':');
55+
if strcmp(deviceIdParts{3},'0x0588')
56+
memdepth = str2double(query(dev,':WAVeform:POINts?'));
57+
else
58+
memdepth = str2double(query(dev,':ACQuire:POINts?'));
59+
end
60+
disp(['Acquiring ', num2str(memdepth), ' points.']);
8661

87-
if (memdepth >= ibs)
88-
disp('READ MEMORY BUFFER TOO SMALL, WAIT WHILE WE INCREASE IT');
89-
ibs = memdepth + 1; % update ibs to fit aquired data
90-
open = instrfind('Status', 'open');
91-
fclose(open);
92-
set(eval(obj_name), 'InputBufferSize', ibs);
93-
fopen(eval(obj_name)); % Reopen the object now buffer is set correctly
62+
if (memdepth >= dev.InputBufferSize)
63+
disp('Increasing insufficient input buffer size.');
64+
fclose(dev);
65+
dev.InputBufferSize = memdepth+1;
66+
fopen(dev);
9467
end
9568

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

10172
%Acquire X origin and X increment values
102-
xorg = str2double(query(eval(obj_name),':WAVEFORM:XOR?'));
103-
xinc = str2double(query(eval(obj_name),':WAVEFORM:XINC?'));
73+
xorg = str2double(query(dev, ':WAVEFORM:XOR?'));
74+
xinc = str2double(query(dev, ':WAVEFORM:XINC?'));
10475

10576
%Acquire Y origin and Y increment Values
106-
yorg = str2double(query(eval(obj_name),':WAVEFORM:YOR?'));
107-
yinc = str2double(query(eval(obj_name),':WAVEFORM:YINC?'));
108-
yref = str2double(query(eval(obj_name),':WAVEFORM:YREF?'));
77+
yorg = str2double(query(dev, ':WAVEFORM:YOR?'));
78+
yinc = str2double(query(dev, ':WAVEFORM:YINC?'));
79+
yref = str2double(query(dev, ':WAVEFORM:YREF?'));
10980

11081
%Setup the Scope for proper output format
111-
fprintf(eval(obj_name), 'WAVEFORM:FORMAT BYTE');
112-
fprintf(eval(obj_name), 'ACQUIRE:TYPE NORM');
82+
fprintf(dev, 'WAVEFORM:FORMAT BYTE');
83+
fprintf(dev, 'ACQUIRE:TYPE NORM');
11384

114-
11585
%Instruct the scope to make current data available to acquire
116-
fprintf(eval(obj_name), 'WAVEFORM:DATA?');
117-
data1 = binblockread(eval(obj_name), 'uint8');
86+
fprintf(dev, 'WAVEFORM:DATA?');
87+
data1 = binblockread(dev, 'uint8');
11888

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

12292
%Adjust data and time variables with x/y increments/origins
12393
t = t1 * xinc + xorg;
12494
data = (data1-yref) * yinc + yorg;
125-
126-
plot(t,data);
12795

128-
open = instrfind('Status', 'open');
129-
fclose(open);
130-
96+
fclose(dev);
13197
end
13298

0 commit comments

Comments
 (0)