#!/usr/bin/expect -- set helpTopics(default) {Current commands: getPOS - get the values associated with the hexapod FOO command help - this command - take an optional argument, for more detail abort - exit gracefully exit - exit gracefully } set helpCmds(foo) {foo} # Procedure: Help proc Help { {topic "default"} {x "0"} {y "0"}} { global helpTopics helpCmds if {$topic == ""} return while {[info exists helpCmds($topic)]} { set topic [eval $helpCmds($topic)] } if [info exists helpTopics($topic)] { set msg $helpTopics($topic) } else { set msg "Sorry, but no help is available for this topic" } puts $msg # mkDialog .help "-text {Information on $topic:\n\n$msg} -justify left -aspect 300" "OK {}" } proc docmd {out in} { puts "" puts "$out ->" exp_send $out\n expect $in } proc numberize {name} { upvar #0 $name n if {[catch {expr $n}]} { set rtn 0 puts "not a number - ++$n++" } else { set n [expr $n] set rtn 1 # puts "n=$n" } return $rtn } proc saveSlice {name} { global microridge set rtn {} set exp "${name}" foreach ndx [array names microridge] { if {[regexp $exp $ndx]} { set nm "microridge($ndx)" set val [set $nm] set this "set microridge($ndx) \{$val\};\n" set rtn "$rtn$this" } } return $rtn } proc outOfRange {goal val PMpercent} { set error [expr abs($goal - $val)] set errorPercent [expr $error/$goal] set rtn 0 if {$errorPercent > $PMpercent} { set rtn 1 } return $rtn } proc doChangesFromNew {} { global hexapod puts "doChangesFromNew: start" #set hexapod(asdf,newfoo) "noofew" #set hexapod(asdf,foo,set) "FOO34567890123456789012345678901234567890123456789012345678901234567 X" #set hexapod(asdf,newfoo2) "noofew2" #set hexapod(asdf,foo2,set) "FOO34567890123456789012345678901234567890123456789012345678901234567 Y" # somehow get all the variables that need changing # first pass - get all the *,new* variables # set exp {.*new.*} foreach ndx [array names hexapod] { if {[regexp $exp $ndx]} { set var "hexapod($ndx)" # construct a list of commands to do the update regsub {(.*,)new([^\)]*)\)} $var {\1\2)} ovar regsub {(.*,)new([^\)]*)\)} $var {\1\2,set)} svar if {[info exists $svar]} { # ok, hackery, glue together "asdf x" and "12" then split at space scan "[set $svar][set $var]" "%s %s" outcmd outarg set hexapod($outcmd,changeCommand) $outcmd lappend hexapod($outcmd,changeArgs) $outarg } unset $var } } # send the command lists out # foreach in hexapod(.*,changeCommand) set exp {.*changeCommand} foreach ndx [array names hexapod] { if {[regexp $exp $ndx]} { # we have a match, create a (couple) of commands set argname [set hexapod($ndx)] puts "[set hexapod($ndx)] [set hexapod($argname,changeArgs)]" # send each out docmd "[set hexapod($ndx)] [set hexapod($argname,changeArgs)]" "*" # poof dakines unset hexapod($ndx) hexapod($argname,changeArgs) } } } proc getPos {{point "1"}} { global microridge global val timestamp set error {} set val {not valid} exp_send "$microridge(R,$point)" expect { -re " *(\[^,]+),(\[^,]+),(\[^\r]*)\r" { set val $expect_out(buffer) set microridge(R,$point,data) $expect_out(1,string) set microridge(R,$point,units) $expect_out(2,string) set microridge(R,$point,channelId) $expect_out(3,string) # puts $expect_out(1,string) # puts $expect_out(2,string) # puts $expect_out(3,string) # last line - so bail } timeout {lappend error "timeout"; puts timeout} eof {lappend error "unexpected EOF"; puts eof} } if {[llength $error] == 0} { set timestamp [exp_timestamp] set microridge(R,$point,timestamp) $timestamp } else { set timestamp 0 foreach err $error { puts "getPos: $err" } } } proc getPOS {} { global microridge #setup channels set microridge(R,1) "> $val" } proc RA {} { excCmd "RA" ".*((mm)|(inch)).*" printRes "RA" } proc RB {} { excCmd "RB" ".*((mm)|(inch)).*" printRes "RB" } proc V {} { excCmd "V" "(MPX).*" printRes "V" } proc printPos {{channel "A"}} { global val time_format timestamp puts "" if { $timestamp == 0 } { puts "$timestamp = timestamp" puts "ERROR occured - possible invalid data follows" } puts "pos channel $channel: (current as of [exp_timestamp -format $time_format -seconds $timestamp])" puts "val is $val" } proc abort {{rtn "-1"}} { global kprompt # close kermit exp_send \034C expect { $kprompt {#puts "abort: got prompt"} timeout {puts "abort: timeout"} eof {puts "abort: eoft"} } exp_send quit # expect # wait exit $rtn } ######################################################## # Start of program ######################################################## # only want tk for send (now) #wm withdraw . set time_format "%c %Z" set timeout 3 set tcl_precision 10 log_user 0 # get in contact #spawn tip microridge #expect { # eof {puts "open failed"} # connected #} set kprompt microRidge-kermit> spawn kermit -l /dev/ttyS1 -b 9600 -C "set term byte 8, set term cr crlf, set prompt $kprompt" -Y expect { $kprompt {#puts "opened tty, got $kprompt"} timeout {puts timeout; abort} eof {puts eof; abort} } exp_send "c\n" expect { options. {#puts "connected"} timeout {puts timeout; abort} eof {puts eof; abort} } # ok, a little race condition - abort assumes we are here, but as soon as # kermit starts we need to exit it. exit -onexit { abort } # now we are talking to the microridge # clear cruft out exp_send "