|
1 #!/usr/bin/expect -f |
|
2 #====================================================================== |
|
3 # L I B R D I . E X P E C T |
|
4 # doc: Mon Mar 8 02:57:35 2004 |
|
5 # dlm: Thu Aug 26 00:23:09 2010 |
|
6 # (c) 2004 A.M. Thurnherr |
|
7 # uE-Info: 42 0 NIL 0 0 72 2 2 8 NIL ofnI |
|
8 #====================================================================== |
|
9 |
|
10 # HISTORY: |
|
11 # Mar 8, 2004: - incepted (NBP0402) |
|
12 # Mar 15, 2004: - released |
|
13 # Mar 18, 2004: - improved download-error handling |
|
14 # - BUG: set correct color in toggle_instrument |
|
15 # - new reset_two_instruments |
|
16 # Mar 20, 2004: - increased instrument-toggle timeout to 2s |
|
17 # - cleaned-up get_deployment_name |
|
18 # - added save_hardware_config to upload_file (just before CS) |
|
19 # - renamed continue_logging to resume_logging |
|
20 # Mar 21, 2004: - debugged user-interaction routines |
|
21 # - generally increased timeouts |
|
22 # Apr 4, 2004: - added comments |
|
23 # Jun 15, 2004: - changed \n to \r for BB150 (CLIVAR P02) |
|
24 # - started adaptation to BB150 |
|
25 # Jun 18, 2004: - BUG: could not handle station 008 (illegal octal digit) |
|
26 # Jun 27, 2004: - doubled wait_for_startup timeouts |
|
27 # Sep 28, 2006: - added sleep 1 to wait_for_startup (marked AMARYLIS) |
|
28 # Oct 30, 2006: - improved error messages |
|
29 # Nov 6, 2006: - added ensure_sleep |
|
30 # - BUG: input_int had interpreted 010 as octal |
|
31 # Nov 15, 2006: - added next_speed |
|
32 # Nov 17, 2006: - added get_serial_number |
|
33 # - added save_parameters |
|
34 # Oct 22, 2007: - removed erroneous line of code (that did nothing) |
|
35 # - changed expect target in next_speed() |
|
36 # Oct 23, 2007: - cosmetics |
|
37 # Nov 19, 2008: - added load_factory_defaults() |
|
38 # - added print_version() |
|
39 # Oct 12, 2009: - BUG: load_factory_defaults errmesg used non-existent variable |
|
40 # Nov 19, 2009: - get_deployment_name needed a delay (sleep 1) for the |
|
41 # instruments sent to the DIMES cruise |
|
42 # Aug 25, 2010: - libBB.expect -> libRDI.expect |
|
43 |
|
44 |
|
45 # NOTE: after having written this, I am not much of a fan of TCL and and |
|
46 # expect(1) syntax any more... |
|
47 |
|
48 # PS0 ON BB150 NOTE: |
|
49 # I AM NOT SURE ABOUT THE FOLLOWING ANY MORE. THE CURRENT CODE |
|
50 # MAY THEREFORE NOT RUN WITH BB150s!!! |
|
51 # PS0 does not produce any output on the BB150 unless the factory |
|
52 # defaults are in effect. (I have not checked exactly which user defaults |
|
53 # cause this behaviour.) Therefore, every time before PS0 is sent |
|
54 # to the instrument, the current values are stored, the factory |
|
55 # defaults restored, PS0 is sent, and then the user values |
|
56 # are restored. For safety, I removed PS0 from save_hardware_config |
|
57 # code for the BB150; because it would be really bad if the factory |
|
58 # defaults would be restored immediately before pinging starts... |
|
59 # The behaviour was detected on CPU firmware 5.52. |
|
60 |
|
61 set NEXT_SPEED \002; # ^B # bbabble commands |
|
62 set BREAK \003; # ^C |
|
63 set HELP \010; # ^H |
|
64 set TOGGLE \024; # ^T |
|
65 set DNLOAD \030; # ^X |
|
66 |
|
67 set cid -1; # current instrument |
|
68 |
|
69 proc print_version {} { |
|
70 send_user "acquire V1.4beta\n" |
|
71 } |
|
72 |
|
73 proc set_color {{cid -1}} { # set instrument color |
|
74 if {$cid == 0} { |
|
75 send_user [exec tput setaf 1]; |
|
76 } elseif {$cid == 1} { |
|
77 send_user [exec tput setaf 4]; |
|
78 } else { |
|
79 send_user [exec tput setaf 0]; |
|
80 } |
|
81 } |
|
82 |
|
83 proc error {msg} { # print error & exit |
|
84 global cid; |
|
85 set_color $cid; |
|
86 send_error "\nError: $msg\n"; |
|
87 set_color; |
|
88 exit 1; |
|
89 } |
|
90 |
|
91 proc gobble_prompts {} { # gobble old prompts |
|
92 set continue_flushing 1; |
|
93 while {$continue_flushing > 0} { |
|
94 expect { |
|
95 timeout {set continue_flushing 0;} |
|
96 -timeout 0 ">"; |
|
97 } |
|
98 } |
|
99 } |
|
100 |
|
101 proc BREAK {} { # send BREAK |
|
102 global BREAK; |
|
103 |
|
104 gobble_prompts; |
|
105 send $BREAK; # Wakeup Instrument |
|
106 expect { |
|
107 timeout {error "Can't wake Instrument"} |
|
108 -timeout 8 "RD Instruments (c)" |
|
109 } |
|
110 expect { # gobble prompt as well |
|
111 timeout {error "Can't get wakeup prompt"} |
|
112 -timeout 3 ">" |
|
113 } |
|
114 } |
|
115 |
|
116 proc no_error_BREAK {} { # send BREAK without errcheck |
|
117 global BREAK; |
|
118 |
|
119 gobble_prompts; |
|
120 send $BREAK; |
|
121 expect -timeout 8 "RD Instruments (c)" |
|
122 expect -timeout 3 ">" |
|
123 } |
|
124 |
|
125 proc next_speed {} { # select next baudrate |
|
126 global NEXT_SPEED; |
|
127 |
|
128 send $NEXT_SPEED; |
|
129 expect { |
|
130 timeout {error "Cannot change speed"} |
|
131 -timeout 5 ": end" |
|
132 } |
|
133 sleep 1 |
|
134 } |
|
135 |
|
136 proc toggle_instrument {} { # select other instrument |
|
137 global cid TOGGLE; |
|
138 send $TOGGLE; # switch |
|
139 expect { |
|
140 timeout {error "Can't switch instruments"} |
|
141 -timeout 3 "Instrument" |
|
142 } |
|
143 if {$cid == 0} {set cid 1} else {set cid 0} |
|
144 } |
|
145 |
|
146 proc select_master {{toggle_first 1}} { # select master & check S/N |
|
147 global HELP cid master_sn slave_sn; |
|
148 |
|
149 if {$toggle_first} toggle_instrument; |
|
150 gobble_prompts; |
|
151 send "\r"; wait_for_prompt; # save current value |
|
152 send "PS0\r"; |
|
153 expect { |
|
154 timeout {error "Can't determine serial number"} |
|
155 -timeout 8 -re "Instrument S/N: $master_sn.*>|Xducer Ser #: $master_sn.*>|PS0...>" {} |
|
156 -timeout 8 -re "Instrument S/N: $slave_sn.*>" { |
|
157 toggle_instrument; |
|
158 send "PS0\r"; |
|
159 expect { |
|
160 timeout {error "Can't determine serial number"} |
|
161 -timeout 8 -re "Instrument S/N: $master_sn.*>|Xducer Ser #: $master_sn.*>|PS0...>" {} |
|
162 -timeout 8 -re "Instrument S/N: $slave_sn.*>" { |
|
163 error "Can't switch to master" |
|
164 } |
|
165 } |
|
166 } |
|
167 } |
|
168 set cid 0; |
|
169 } |
|
170 |
|
171 proc select_slave {{toggle_first 1}} { # select slave & check S/N |
|
172 global HELP cid master_sn slave_sn; |
|
173 |
|
174 if {$toggle_first} toggle_instrument; |
|
175 gobble_prompts; |
|
176 send "\r"; wait_for_prompt; # save current value |
|
177 send "PS0\r"; |
|
178 expect { |
|
179 timeout {error "Can't determine master/slave"} |
|
180 -timeout 8 -re "Instrument S/N: $slave_sn.*>" {} |
|
181 -timeout 8 -re "Instrument S/N: $master_sn.*>|Xducer Ser #: $master_sn.*>|PS0...>" { |
|
182 toggle_instrument; |
|
183 send "PS0\r"; |
|
184 expect { |
|
185 timeout {error "Can't determine master/slave"} |
|
186 -timeout 8 -re "Instrument S/N: $slave_sn.*>" {} |
|
187 -timeout 8 -re "Instrument S/N: $master_sn.*>|Xducer Ser #: $master_sn.*>|PS0...>" { |
|
188 error "Can't switch to slave" |
|
189 } |
|
190 } |
|
191 } |
|
192 } |
|
193 set cid 1; |
|
194 } |
|
195 |
|
196 proc wait_for_startup {} { # wait for bbabble startup |
|
197 global cid; |
|
198 expect { |
|
199 timeout {error "bbabble failed to start up"} |
|
200 -timeout 10 "device" { |
|
201 error "No such device: cables plugged in???" |
|
202 } |
|
203 -timeout 10 "Instrument 0" {} |
|
204 } |
|
205 set cid 0; |
|
206 sleep 1; # AMARYLIS |
|
207 } |
|
208 |
|
209 proc reset_instrument {} { # reset regardless of state |
|
210 no_error_BREAK; |
|
211 send "\r"; # send NOP command |
|
212 expect { |
|
213 timeout { |
|
214 send_error "."; |
|
215 BREAK; # try again |
|
216 } |
|
217 -timeout 5 ">"; # instrument reacted |
|
218 } |
|
219 } |
|
220 |
|
221 proc reset_two_instruments {} { # reset regardless of state |
|
222 set ok 0; |
|
223 ### toggle_instrument; |
|
224 while {$ok < 2} { |
|
225 no_error_BREAK; |
|
226 send "\r"; expect { |
|
227 timeout { |
|
228 set ok 0; |
|
229 send_error "!"; |
|
230 } |
|
231 -timeout 5 ">" {incr ok} |
|
232 } |
|
233 toggle_instrument; |
|
234 } |
|
235 select_master; |
|
236 } |
|
237 |
|
238 proc wait_for_prompt {{tout 5}} { # wait for prompt |
|
239 expect { |
|
240 timeout {error "Can't get prompt"} |
|
241 -timeout $tout "ERR" { |
|
242 error "Instrument returned error $expect_out(buffer)"; |
|
243 } |
|
244 -timeout $tout ">" |
|
245 } |
|
246 } |
|
247 |
|
248 proc put_to_sleep {} { # send instrument to sleep |
|
249 gobble_prompts; |
|
250 send "\r"; wait_for_prompt; |
|
251 send "CZ\r"; |
|
252 expect { |
|
253 timeout {error "Can't put Instrument to sleep"} |
|
254 -timeout 5 -re "Powering Down|POWERING DOWN" |
|
255 } |
|
256 } |
|
257 |
|
258 proc save_parameters {} { # save parameters |
|
259 gobble_prompts; |
|
260 send "\r"; wait_for_prompt; |
|
261 send "CK\r"; |
|
262 expect { |
|
263 timeout {error "Can't put Instrument to sleep"} |
|
264 -timeout 5 -re "USER defaults" |
|
265 } |
|
266 } |
|
267 |
|
268 proc ensure_sleep {} { # send instrument to sleep... |
|
269 gobble_prompts; # unless already sleeping |
|
270 send "CZ\r"; |
|
271 expect { |
|
272 timeout {error "Can't put Instrument to sleep"} |
|
273 -timeout 5 -re "Powering Down|POWERING DOWN" |
|
274 } |
|
275 } |
|
276 |
|
277 proc start_download {} { # start downloading |
|
278 global DNLOAD; |
|
279 |
|
280 gobble_prompts; |
|
281 send "\r"; wait_for_prompt; |
|
282 send $DNLOAD; |
|
283 expect { |
|
284 timeout {error "Can't start download (memory empty?)"} |
|
285 -timeout 10 "Receiving:" |
|
286 } |
|
287 } |
|
288 |
|
289 proc wait_for_download_finish {stn tout} { # wait for timeout to finish |
|
290 global BREAK TOGGLE; |
|
291 expect { |
|
292 timeout {error "Download did not finish ok"} |
|
293 -timeout $tout "Transfer complete" {} |
|
294 -timeout $tout "ABNORMALLY" { |
|
295 send $BREAK; send $TOGGLE; send $BREAK; |
|
296 error "Download did not finish ok"; |
|
297 } |
|
298 } |
|
299 expect -timeout 5 ">"; |
|
300 |
|
301 global cid master_download_filename slave_download_filename; |
|
302 global master_deployment_name_fmt slave_deployment_name_fmt; |
|
303 if {$cid == 0} { |
|
304 rename_master_download_file [format $master_deployment_name_fmt $stn]000.000; |
|
305 } else { |
|
306 rename_slave_download_file [format $slave_deployment_name_fmt $stn]000.000; |
|
307 } |
|
308 } |
|
309 |
|
310 proc list_dir {} { # list directory |
|
311 global cid; |
|
312 gobble_prompts; |
|
313 send "\r"; wait_for_prompt; |
|
314 send "RR\r"; |
|
315 expect { |
|
316 -timeout 1 "ERR" { # BB150 |
|
317 wait_for_prompt; |
|
318 send "RS\r"; |
|
319 log_user 1; |
|
320 } |
|
321 -timeout 5 "Recorder Directory:"; |
|
322 } |
|
323 set_color $cid; log_user 1; |
|
324 wait_for_prompt; |
|
325 log_user 0; set_color; |
|
326 } |
|
327 |
|
328 proc erase_memory {} { # erase memory |
|
329 gobble_prompts; |
|
330 send "\r"; wait_for_prompt; |
|
331 send "RE ErAsE\r"; |
|
332 expect { |
|
333 timeout {error "Can't erase recorder"} |
|
334 -timeout 5 "Erasing..." { # BB150 |
|
335 sleep 3; |
|
336 wait_for_prompt; |
|
337 return; |
|
338 } |
|
339 -timeout 5 "Recorder erased." # Workhorse |
|
340 } |
|
341 wait_for_prompt; |
|
342 } |
|
343 |
|
344 proc save_hardware_config {filename} { # NB: logging continues in ... |
|
345 gobble_prompts; # ... file given as parameter |
|
346 send "\r"; wait_for_prompt; |
|
347 send "TT?\r"; # BB150 |
|
348 expect -timeout 1 "ERR" { |
|
349 wait_for_prompt; |
|
350 set time [exec date +%y/%m/%d,%H:%M:%S]; |
|
351 set pipe [open "| tr -d \\\r \ |
|
352 | sed -e s@Time\\ Set.*@LOGGING\\ COMPUTER:\\ $time@ \ |
|
353 -e /^>/d \ |
|
354 > $filename" w]; |
|
355 log_file; log_file -a -open $pipe; |
|
356 send "T?\r"; wait_for_prompt; |
|
357 send "RA?\r"; wait_for_prompt; |
|
358 send "RS?\r"; wait_for_prompt; |
|
359 send "W?\r"; wait_for_prompt; |
|
360 send "B?\r"; wait_for_prompt; |
|
361 send "E?\r"; wait_for_prompt; |
|
362 send "CG?\r"; wait_for_prompt; |
|
363 send "CL?\r"; wait_for_prompt; |
|
364 send "CP?\r"; wait_for_prompt; |
|
365 send "CQ?\r"; wait_for_prompt; |
|
366 send "CT?\r"; wait_for_prompt; |
|
367 send "CX?\r"; wait_for_prompt; |
|
368 send "PS3\r"; wait_for_prompt; |
|
369 return; |
|
370 } |
|
371 set time [exec date +%Y/%m/%d,%H:%M:%S]; |
|
372 set pipe [open "| tr -d \\\r \ |
|
373 | sed -e s/^Press\\ any\\ key\\ to\\ continue\\ *// \ |
|
374 -e s@Time\\ Set.*@LOGGING\\ COMPUTER:\\ $time@ \ |
|
375 > $filename" w]; |
|
376 log_file; |
|
377 log_file -a -open $pipe; |
|
378 send "TT?\r"; wait_for_prompt; |
|
379 send "RF\r"; wait_for_prompt; |
|
380 send "RR\r"; wait_for_prompt; |
|
381 send "PS0\r"; wait_for_prompt; |
|
382 send "PS3\r"; wait_for_prompt; |
|
383 send "AC\r"; wait_for_prompt; |
|
384 send "RN?\r"; wait_for_prompt; |
|
385 send "DEPLOY?\r"; |
|
386 expect { |
|
387 timeout {error "Can't get DEPLOY? output"} |
|
388 -timeout 10 "Press any key to continue" |
|
389 } |
|
390 send " "; wait_for_prompt; |
|
391 } |
|
392 |
|
393 proc start_logging {{filename "bbabble.log"}} { # write UNIX conformant log file |
|
394 set pipe [open "| tr -d \\\r \ |
|
395 > $filename" w]; |
|
396 log_file -a -open $pipe; |
|
397 } |
|
398 |
|
399 proc resume_logging {{filename "bbabble.log"}} {# append to UNIX conf. log file |
|
400 set pipe [open "| tr -d \\\r \ |
|
401 >> $filename" w]; |
|
402 log_file; |
|
403 log_file -a -open $pipe; |
|
404 } |
|
405 |
|
406 proc memory_empty {} { # check whether memory is empty |
|
407 gobble_prompts; |
|
408 send "\r"; wait_for_prompt; |
|
409 send "RA\r"; |
|
410 expect { |
|
411 timeout {error "Can't get number of deployments"}; |
|
412 -timeout 3 -re {[1-9]} { |
|
413 wait_for_prompt; |
|
414 return 0; |
|
415 } |
|
416 -timeout 3 ">" { |
|
417 return 1; |
|
418 } |
|
419 } |
|
420 } |
|
421 |
|
422 proc set_instrument_clock {} { # set instrument clock |
|
423 gobble_prompts; |
|
424 send "\r"; wait_for_prompt; |
|
425 send "TT?\r"; # check for BB150 |
|
426 expect { |
|
427 -timeout 1 "ERR" { |
|
428 gobble_prompts; |
|
429 set time [exec date +%y/%m/%d,%H:%M:%S]; |
|
430 send "TS$time\r"; |
|
431 wait_for_prompt; |
|
432 return; |
|
433 } |
|
434 } |
|
435 set time [exec date +%Y/%m/%d,%H:%M:%S]; |
|
436 send "TT$time\r"; |
|
437 wait_for_prompt; |
|
438 } |
|
439 |
|
440 proc upload_file {cfn lfn hwfn} { # upload command file |
|
441 gobble_prompts; # logging stops in this routine |
|
442 send "\r"; wait_for_prompt; |
|
443 set cf [open $cfn "r"]; |
|
444 set lf [open $lfn "w"]; |
|
445 while {[gets $cf line] >= 0} { |
|
446 regsub {[ ]*;.*} $line {} line; |
|
447 if {![regexp {^[ ]*$} $line]} { |
|
448 puts -nonewline $lf "$line ... "; |
|
449 if {$line == "CS" || $line == "cs"} { |
|
450 send_error "!"; |
|
451 save_hardware_config $hwfn; |
|
452 send "$line\r"; |
|
453 expect { |
|
454 timeout {}; |
|
455 -timeout 3 expect ">" { |
|
456 error "Got prompt after CS command"; |
|
457 } |
|
458 } |
|
459 puts $lf "INSTRUMENT PINGING"; |
|
460 close $cf; close $lf; |
|
461 log_file; |
|
462 return; |
|
463 } |
|
464 send "$line\r"; |
|
465 send_error "."; |
|
466 expect { |
|
467 timeout {error "Can't get prompt after sending $line"}; |
|
468 -timeout 5 "ERR" { |
|
469 error "Sending command $line returned $expect_out(buffer)"; |
|
470 } |
|
471 -timeout 5 ">" { |
|
472 puts $lf "OK"; |
|
473 } |
|
474 } |
|
475 } |
|
476 } |
|
477 close $cf; close $lf; |
|
478 } |
|
479 |
|
480 proc load_factory_defaults {} { # set default params |
|
481 gobble_prompts; |
|
482 send "\r"; wait_for_prompt; |
|
483 send "CR1\r"; |
|
484 expect { |
|
485 timeout {error "Can't get prompt"}; |
|
486 -timeout 5 "ERR" { |
|
487 error "command CR1 returned $expect_out(buffer)"; |
|
488 } |
|
489 -timeout 5 ">" |
|
490 } |
|
491 } |
|
492 |
|
493 proc set_deployment_name {{dn UNSET}} { # set deployment name |
|
494 gobble_prompts; |
|
495 send "\r"; wait_for_prompt; |
|
496 send "RN$dn\r"; |
|
497 expect -timeout 1 "ERR"; # BB150 |
|
498 wait_for_prompt; |
|
499 } |
|
500 |
|
501 proc get_deployment_name {} { # get deployment name |
|
502 global stn_format master_deployment_name_fmt; |
|
503 gobble_prompts; |
|
504 send "\r"; wait_for_prompt; |
|
505 send "RN?\r"; sleep 1; |
|
506 expect { |
|
507 timeout {error "Can't get deployment name";} |
|
508 -timeout 1 "ERR" { # BB150 |
|
509 if {![file isfile .last_stn]} { |
|
510 error "Can't find .last_stn"; |
|
511 } |
|
512 set stn [exec cat .last_stn]; |
|
513 set dn [format $master_deployment_name_fmt $stn]; |
|
514 wait_for_prompt; |
|
515 return $dn; |
|
516 } |
|
517 -timeout 5 -re {Current deployment name = ([^\r]*)} |
|
518 } |
|
519 set dn $expect_out(1,string); |
|
520 wait_for_prompt; |
|
521 return $dn; |
|
522 } |
|
523 |
|
524 proc get_serial_number {} { # get serial number |
|
525 gobble_prompts; |
|
526 send "\r"; wait_for_prompt; |
|
527 send "PS0\r"; |
|
528 expect { |
|
529 timeout {error "Can't get serial number";} |
|
530 -timeout 5 -re {Instrument S/N: *([0-9]*)} |
|
531 } |
|
532 set sn $expect_out(1,string); |
|
533 wait_for_prompt; |
|
534 return $sn; |
|
535 } |
|
536 |
|
537 #====================================================================== |
|
538 # User-Interation Routines |
|
539 #====================================================================== |
|
540 |
|
541 proc affirm {prompt {to -1} {timeout_return 0}} { # yes/no question with timeout |
|
542 send_user $prompt; # and default answer |
|
543 while {1} { |
|
544 expect_user { |
|
545 timeout { |
|
546 if {$timeout_return == 0} { |
|
547 send_user "N\n"; |
|
548 } else { |
|
549 send_user "Y\n"; |
|
550 } |
|
551 return $timeout_return; |
|
552 } |
|
553 -timeout $to -re "^y.*|^Y.*" {return 1;} |
|
554 -timeout $to -re "^n.*|^N.*" {return 0;} |
|
555 -re {..*} {send_user "Please answer Y or N..."} |
|
556 } |
|
557 } |
|
558 } |
|
559 |
|
560 proc input_int {prompt} { # input an integer |
|
561 while {1} { |
|
562 send_user $prompt; |
|
563 expect_user { |
|
564 -timeout -1 -re {^[0-9]+\n} { |
|
565 regsub {^0*} $expect_out(0,string) {} decimal_val; |
|
566 return $decimal_val; |
|
567 } |
|
568 -timeout -1 -re {.+} {send_user "only integers, please!\n"} |
|
569 } |
|
570 } |
|
571 } |