On Mon, Aug 20, 2018 at 10:29:46AM +1000, luv-main wrote:
> Hi All,
>
> I'm having some trouble making expect work.
>
> I need it to talk to some vintage equipment over usb serial, and I think
> I'm getting hung up on opening the port.
>
> When the script runs, it just connects to the device and sits there. I can
> drive it interactively, but it doesn't attempt to automate anything.
>
> Any ideas? Thanks.
>
> My expect script looks like this:
>
> #!/usr/bin/expect -f
>
> # device
> set modem /dev/ttyUSB0
>
> # keep it open
> exec sh -c "sleep 3 < $modem" &
>
> # serial port parameters
> exec stty -F $modem 2400 raw -clocal -echo -istrip -hup
>
> # connect
> send_user "connecting to $modem, exit with ~,\n"
> spawn -open [open $modem w+]
> interact {
>     ~, exit
>     ~~ {send "\034"}
> }
>
> set force_conservative 1 ;# set to 1 to force conservative mode even if
>               ;# script wasn't run conservatively originally
> if {$force_conservative} {
>     set send_slow {1 .1}
>     proc send {ignore arg} {
>         sleep .1
>         exp_send -s -- $arg
>     }
> }
>
> set timeout -1
> match_max 100000
> send -- "\r"
> send -- "\r"
> expect  ">"
> send -- "p 7d91\r"
> expect  ">"
> send -- "p b2ff\r"
> expect  ">"
> send -- "h\r"
> expect  ">"
> send -- "td\r"
> expect "td\r
>   18 215  12  24  33\r
> >"
> send -- "gd 215\r"
> expect eof

What a mess. Could you tidy it a little, or a lot?

You don't need slow send with these short character sequences.

You can do the shell lines before you start expect (as I'll demonstrate).

I used to do a lot of (USB or not) serial stuff and never found I needed that
sleep 3 thing. Once you set serial line params, they stay that way until a
process changes them. Tcl open does not change them unless you tell it to.

Anyway, sleep 3 <$modem is likely sucking up response characters.

*exp_internal 1* is your friend when debugging expect scripts,

Cheers ... Duncan.

===================

#!/bin/sh

# expect sees the next line as a comment continuation \
# You can put shell lines here as long as you end them semicolon backslash \
modem=${modem:-/dev/ttyUSB0};\
export modem;\
stty -F $modem 2400 raw -clocal -echo -istrip -hup;\
\
# Really start expect \
exec expect -- "$0" "$@"

puts "connecting to $env(modem), exit with ~,"
spawn -open [open $env(modem) r+] ;# rw, no truncate

exp_internal 1 ;# Comment-out once working

interact {
    {~,} {exit}
    {~~} {send "\034"}
}

# and so on
_______________________________________________
luv-main mailing list
luv-main@luv.asn.au
https://lists.luv.asn.au/cgi-bin/mailman/listinfo/luv-main

Reply via email to