1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
function [time, channel1, channel2, channel3, channel4] = DS1054z_take_data(scope_visa_name, data_save_flag, data_plot_flag)
%% This script reads data from Rigol DS1054z Digital Oscilloscope via the VISA interface and plots the four channels
savepath = 'Z:\gyro_data\data\';
%% Find and access VISA object.
% scope_visa_name = 'USB0::0x1AB1::0x04CE::DS1ZA170502787::0::INSTR';
% instrfind has different name convention
% instr_name_for_instrfind = strrep( scope_visa_name, '::INSTR', '::0::INSTR');
obj1 = instrfind('Type', 'visa-usb', 'RsrcName', scope_visa_name, 'Tag', '');
% Create the VISA object if it does not exist
% otherwise use the object that was found.
%if isempty(obj1)
%obj1 = visa('ni', scope_visa_name);
%else
%fclose(obj1);
%obj1 = obj1(1);
%end;
%% Configure instrument object, obj1
%set(obj1, 'InputBufferSize', 512000);
%set(obj1, 'OutputBufferSize', 512000);
% Connect to instrument object, obj1.
%fopen(obj1);
obj1=fopen('/dev/usbtmc0');
%% Initial adjustments on scope in preparation for aquizition
%fwrite(obj1,':STOP'); % stop aquiring new data on scope
%fwrite(obj1,':WAVEFORM:POINS:MODE RAW'); % about 8k points expected
%fwrite(obj1,':WAVEFORM:POINS:MODE NOR'); % 600 points expected
%% grab a channel function
function Vchan=grab_channel(chanName)
% Read a waveform from a given channel
fwrite(obj1, horzcat(':WAVEFORM:SOURCE ', chanName) );
fwrite(obj1,':WAVEFORM:FORMAT ASCII'); % ascii returns 1200 points
%fwrite(obj1,':WAVEFORM:FORMAT BYTE');
%fwrite(obj1, 'WAVeform:STOP 10000');
fwrite(obj1,':WAVEFORM:MODE NORMAL');
%fwrite(obj1,':WAVEFORM:MODE MAXIMUM');
%fwrite(obj1,':WAVEFORM:MODE RAW');
data1 = query(obj1, ':WAVEFORM:DATA?');
Vchan = sscanf(data1(12:end), '%f,');
end
%% grab channels
channel1 = grab_channel('CHANNEL1');
channel2 = grab_channel('CHANNEL2');
channel3 = grab_channel('CHANNEL3');
channel4 = grab_channel('CHANNEL4');
Npnts=length(channel1);
%% grab and prepare timing information
timescale_str = query(obj1, ':TIMEBASE:MAIN:SCALE?');
timescale = sscanf( timescale_str, '%f');
timeoffset_str = query(obj1, ':TIMEBASE:MAIN:OFFSET?');
timeoffset = sscanf( timeoffset_str, '%f');
time = linspace( -Npnts/2 , Npnts/2, Npnts) * timescale*12/Npnts;
time = time + timeoffset;
time = time';
%% which scope was used
deviceid = query(obj1, '*IDN?');
%% clean up of scope state
%fwrite(obj1,':RUN'); % switch scope into aquiring mode
%fwrite(obj1,':KEY:FORCE'); % return control to user
%% Disconnect from instrument object, obj1.
fclose(obj1);
%% Save data to a file
if (data_save_flag)
% first we need iteration number
run_number = get_runnum( savepath );
% generate data file name
data_file_base = horzcat( ...
savepath ...
, 'S' ...
, datestr(date,'yyyymmdd') ...
, '_', num2str(run_number,'%05.f') ...
);
save_to_file = horzcat( data_file_base, '.dat' );
% report to user the filename
disp(' ');
disp(horzcat('Saving data to ',save_to_file));
%% open data file
save_to_file_handle = fopen(save_to_file,'wt');
%% finally save data
% header first
comment_symbol='%';
str=horzcat(comment_symbol,' ', datestr(clock));
fprintf(save_to_file_handle,'%s', str);
fprintf(save_to_file_handle,'\n');
str=horzcat(comment_symbol,' ','Device:', ' ', deviceid);
fprintf(save_to_file_handle,'%s', str);
fprintf(save_to_file_handle,'\n');
% here comes the data
data = [time, channel1, channel2, channel3, channel4];
[nrows, ncols] = size(data);
fmtstr='%e';
for i=1:(ncols-1)
fmtstr=horzcat(fmtstr, '\t%e');
end
fmtstr=horzcat(fmtstr, '\n');
fprintf(save_to_file_handle, fmtstr,data'); % do not forget to transpone columniwise data !!!
% Close the file
fclose(save_to_file_handle);
end % end of data save if
%% Plot the scope data
if (data_plot_flag)
plot(time,channel1,'DisplayName','channel1','Color','black');
hold all;
plot(time,channel2,'DisplayName','channel2','Color','Cyan');
plot(time,channel3,'DisplayName','channel3','Color','Magenta');
plot(time,channel4,'DisplayName','channel4','Color','Blue');
grid on
hold off;
xlabel('Time (s)');
ylabel('Voltage (V)');
legend('show');
end % end of data plot if
if (data_save_flag && data_plot_flag)
fig_fname = horzcat( data_file_base, '.png' );
print( '-opengl','-r300', '-dpng', fig_fname);
fig_fname = horzcat( data_file_base, '.eps' );
print( '-painters','-r600','-depsc2', fig_fname);
end
disp('Done');
end
|