-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget_owon_data.m
137 lines (137 loc) · 4.9 KB
/
get_owon_data.m
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
function [data, preamble] = get_owon_data(os_struct)
%% Function to get
% This is only as a guide. For multiple channels, modify the following code
%
os = os_struct.obj;
os_settings = os_struct.settings;
if strcmp(os.Status, 'closed')
fopen(os); fprintf(os, ':RUN');
end
%
fprintf(os, ':STOP');
[data.sample_rate, chs_disp, vertical]= get_srate_chs(os);
%%
% Counter and Preallocation
current_len = 0;
% OBS: Check always your InputBufferSize
step_len = 50000; % Test your step. The max data length that the device reads per time is 256k
if step_len > os.InputBufferSize/2-50
step_len = os.InputBufferSize/2-50;
end
total_len = 10e6; % Manual set from the DEPMEM query
data.points = nan(total_len,sum(chs_disp));
% flushinput(os);
%%
% fprintf(os, ':RUN');
if isequal(chs_disp,[1 0]) || isequal(chs_disp,[1 1])
str_command = ':WAV:BEG CH1';
else
str_command = ':WAV:BEG CH2';
end
fprintf(os, str_command);
% The read data by one time is #9000001024XXXX: among which, “9” indicates the bytes quantity,
% “000001024” describes the length of the waveform (input signal) data, say, 1024 bytes. The value of “N”
% calculated by introducing 2 functions: "partial string" and "decimal numeric string to numeric conversion".
fprintf(os, ':WAV:PRE?');
val = spoll(os);
%%
% Can't read it correctly. From the NI I/O Trace, I'm getting 1035 bytes
% binblockread works, but what's the correct format... int16, char?
preamble = fscanf(os, '%c');
val = spoll(os);
% out = binblockread(os, 'char');
%%
% Data loop
try
while current_len < total_len
str_range_command = sprintf(':WAV:RANG %d,%d',current_len, step_len);
fprintf(os, str_range_command);
fprintf(os, ':WAV:FETC?');
val = spoll(os);
% The read data consists of two parts - TMC header and data packet, like #900000ddddXXXX..., among
% which, “dddd” reflects the length of the valid data packet in the data stream, “XXXX...” indicates the data
% from the data packet, every 2 bytes forms one effective data, to be 16-bit signed integer data
out = binblockread(os, 'int16');
data.points(current_len+1:current_len+step_len,1) = out;
% DUAL channel status
if isequal(chs_disp,[1 1])
str_beg_command = ':WAV:BEG CH2';
fprintf(os, str_beg_command);
fprintf(os, str_range_command);
fprintf(os, ':WAV:FETC?');
val = spoll(os);
out = binblockread(os, 'int16');
data.points(current_len+1:current_len+step_len,2) = out;
str_beg_command = ':WAV:BEG CH1';
fprintf(os, str_beg_command);
end
current_len = current_len + step_len;
end
catch ME
% Sometimes there's no an effective data-packet read within the loop
fprintf(os, ':WAV:END');
fclose(os);
if isempty(out)
fprintf(2,'Empty data packet\n');
return
else
rethrow(ME.message)
end
% rethrow(ME);
end
%%
fprintf(os, ':WAV:END');
fclose(os);
%% Process data to waveform points
vscale = vertical.scale(logical(chs_disp));
voffset = vertical.offset(logical(chs_disp));
xfactor = os_settings.chs.probe(logical(chs_disp));
ximpedf = os_settings.chs.imped(logical(chs_disp));
offset_disp = 1; % To consider the offset or not
voffset = offset_disp*voffset;
for n = 1:sum(chs_disp)
data.points(:,n) = (data.points(:,n)/6400 - voffset(n))*vscale(n)*xfactor(n)/ximpedf(n);
end
end
%%
function [sample, chs_status, vertical] = get_srate_chs(os)
%% MAPs
map = get_config_map_owon();
%% Query instrument
% Ch Status
ch1stat = query(os, ':CH1:DISP?'); chs2stat = query(os, ':CH2:DISP?');
if strcmp(strcat(ch1stat), 'ON->') && strcmp(strcat(chs2stat), 'ON->')
CH_status = 'dual'; chs_status = [1 1];
elseif strcmp(strcat(ch1stat), 'OFF->') && strcmp(strcat(chs2stat), 'OFF->')
warning('All channels OFF... Turning ON CH1')
fprintf(os, ':CH1:DISP ON');
CH_status = 'single'; chs_status = [1 0];
elseif strcmp(strcat(ch1stat), 'ON->') && strcmp(strcat(chs2stat), 'OFF->')
CH_status = 'single'; chs_status = [1 0];
elseif strcmp(strcat(ch1stat), 'OFF->') && strcmp(strcat(chs2stat), 'ON->')
CH_status = 'single'; chs_status = [0 1];
end
vertical.offset = nan(2,1); vertical.scale = nan(2,1);
out = query(os, ':CH1:OFFS?');
vertical.offset(1) = str2num(out(1:end-3));
out = query(os, ':CH2:OFFS?');
vertical.offset(2) = str2num(out(1:end-3));
out = strcat(query(os, ':CH1:SCAL?'));
vertical.scale(1) = map.Vscale(out);
out = strcat(query(os, ':CH2:SCAL?'));
vertical.scale(2) = map.Vscale(out);
% Timebase
tbase = query(os, ':HORI:SCAL?');
% Depth mem
depmem = query(os, ':ACQ:DEPMEM?');
%% Sample struct output
maxRate = map.maxrate(CH_status);
samplePts = map.samplepts(strcat(depmem));
timebase = map.timebase(strcat(tbase));
% Sample rule
if maxRate > samplePts/timebase
sample = samplePts/timebase;
else
sample = maxRate;
end
end