-
Notifications
You must be signed in to change notification settings - Fork 1
/
getserial.m
277 lines (237 loc) · 5.77 KB
/
getserial.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
%======================================================================
% G E T S E R I A L . M
% doc: Wed Jan 7 16:30:11 2009
% dlm: Fri Mar 5 15:48:31 2010
% (c) 2009 A.M. Thurnherr
% uE-Info: 101 13 NIL 0 0 72 0 2 8 NIL ofnI
%======================================================================
% CHANGES BY ANT:
% Jan 7, 2009: - tightened use of exist()
function p=getadcpserial(f,p)
% try to read .LOG or RECOVE.LOG
% decode serial number
%
p=setdefv(p,'down_sn',nan);
p=setdefv(p,'up_sn',nan);
% check in local lookup table
if exist('adcpserial.tab','file')
disp(' found local lookup table for serial # to instid ')
load adcpserial.tab
if existf(p,'instid')
[dum(1),ii]=min(abs(adcpserial(:,1)-p.instid(1)));
if dum(1)==0
p.down_sn=adcpserial(ii,2);
end
if length(p.instid)>1
[dum(2),ii]=min(abs(adcpserial(:,1)-p.instid(2)));
if dum(2)==0
p.up_sn=adcpserial(ii,2);
end
end
end
end
% could not find instrument in lookup table, try log file
flog=findlogwh(f.ladcpdo);
if length(flog)>1
[p.down_sn,p.down_rawlog]=readserialwh(flog);
else
flog=findlogbb(f.ladcpdo);
if length(flog)>3
[p.down_sn,p.down_rawlog]=readserialbb(flog);
end
end
disp([' down looking serial number is : ',int2str(p.down_sn)])
% make sure to put serial number in data base
if length(flog)>1
if exist('adcpserial.tab','file')
load adcpserial.tab
[dum,ii]=min(abs(adcpserial(:,2)-p.down_sn));
else
adcpserial=[0 0];
dum=1;
end
if isfinite(p.down_sn)
if dum~=0
disp(' found new instrument serial number id pair save to ADCPSERIAL.TAB ')
adcpserial(end+1,:)=[p.instid(1) p.down_sn];
else
if p.instid(1)~=adcpserial(ii,1)
disp(['found new CPU board for ',int2str(p.down_sn),' add to ADCPSERIAL.TAB '])
end
end
fid = fopen('adcpserial.tab','w');
fprintf(fid,'%16.0f %5.0f\n',adcpserial');
fclose(fid);
else
disp(['found new CPU board id ',int2str(p.instid(1)),...
' add it to ADCPSERIAL.TAB with serial number'])
end
end
% up looker
flog=findlogwh(f.ladcpup);
if length(flog)>1
[p.up_sn,p.up_rawlog]=readserialwh(flog);
else
flog=findlogbb(f.ladcpup);
if length(flog)>1
[p.up_sn,p.up_rawlog]=readserialbb(flog);
end
end
disp([' up looking serial number is : ',int2str(p.up_sn)])
% make sure to put serial number in data base
if length(flog)>1
if exist('adcpserial.tab','file')
load adcpserial.tab
[dum,ii]=min(abs(adcpserial(:,2)-p.up_sn));
else
adcpserial=[0 0];
dum=1;
end
if isfinite(p.up_sn)
if dum~=0
disp(' found new instrument serial number id pair save to ADCPSERIAL.TAB ')
adcpserial(end+1,:)=[p.instid(2) p.up_sn];
else
if p.instid(2)~=adcpserial(ii,1)
disp(['found new CPU board for ',int2str(p.down_sn),' add to ADCPSERIAL.TAB '])
end
end
fid = fopen('adcpserial.tab','w');
fprintf(fid,'%16.0f %5.0f\n',adcpserial');
fclose(fid);
else
disp(['found new CPU board id ',int2str(p.instid(1)),...
' add it to ADCPSERIAL.TAB with serial number'])
end
end
%====================================
function flog=findlogwh(fin)
% look for logfile in data directory
% ignore empty file
flog=' ';
if length(fin)<2, return, end
disp([' FINDLOG: search near file: ',fin])
% check if .LOG exists
dum=find(fin=='.');
if dum>0
flog=[fin(1:max(dum)),'LOG'];
if exist(flog,'file')
return
end
flog=[fin(1:max(dum)),'log'];
if exist(flog,'file')
return
end
% check if 000.LOG exists
fin1=fin(1:(max(dum)-4));
flog=[fin1,'000.LOG'];
if exist(flog,'file')
return
end
flog=[fin1,'000.log'];
if exist(flog,'file')
return
end
end
% look in data directory for RECOVER.LOG
dum=find(fin=='/' | fin=='\') ;
if length(dum)>0
i1=1:dum(end);
fdir=fin(i1);
else
fdir='';
end
flog=[fdir,'RECOVER.LOG'];
if exist(flog,'file')
return
end
flog=[fdir,'recover.log'];
if exist(flog,'file')
return
end
disp([' FINDLOG: give up'])
flog='';
return
%====================================
function flog=findlogbb(fin)
% look for logfile in data directory
% ignore empty file
flog=' ';
if length(fin)<2, return, end
disp([' FINDLOG: search near file: ',fin])
% check if .LOG exists
dum=find(fin=='/' | fin=='\') ;
if length(dum)<1, return, end
if length(dum)>1, i1=dum(end-1)+1; else i1=1; end
fname=fin(i1:[dum(end)-1]);
flog=[fin(1:max(dum)),lower(fname)];
if exist(flog,'file')
return
end
flog=[fin(1:max(dum)),upper(fname)];
if exist(flog,'file')
return
end
disp([' FINDLOG: give up'])
flog='';
return
%=========================================
function [s,A]=readserialwh(f)
% open file ans parse for serial number
disp([' READSERIAL_WH: parse file ',f])
id=fopen(f);
A=fread(id);
fclose(id);
As=A;
ii=find(As<32); As(ii)=[];
ii=findstr(As',' Instrument S/N:');
if length(ii)>0
s=sscanf(char(As(ii(1)+17+[1:9])'),'%g')';
if (s>0 & s<9999)
% disp([' serial number is : ',int2str(s)])
else
s=nan;
end
else
disp([' Can not find RDI serial number '])
ii=findstr(A','Board Serial Number Data:');
if length(ii)>0
disp([' Check BOARD-ID: ',char(A(ii(1)+[26:208])')])
s=sum(A(ii(1)+[26:208]));
switch s
case 8600
s = 149;
case 8560;
s = 150.2;
case 8547;
s = 150.1;
case 8477
s = 299;
case 8521
s = 754;
otherwise
disp([' unknown BOARD-ID';])
disp([' madeup serial number is : ',int2str(s)])
end
else
s=nan;
end
end
A=char(A)';
%=========================================
function [s,A]=readserialbb(f)
% open file ans parse for serial number
disp([' READSERIAL_BB: parse file ',f])
id=fopen(f);
A=fread(id);
fclose(id);
As=A;
ii=find(As<32); As(ii)=[];
ii=findstr(As','Xducer Ser #:');
s=sscanf(char(As(ii+13+[1:9])'),'%g')';
if (s>0 & s<9999)
% disp([' serial number is : ',int2str(s)])
else
s=nan;
end
A=char(A)';