0
votes

I have the following code for listening at a serial port:

set timeout -1
log_user 0

set port [lindex $argv 0]

spawn /usr/bin/cu -l $port

proc receive { str } {
  set timeout 5
  expect
  {
    timeout { send_user "\nDone\n"; }
  }
  set timeout -1
}

expect {
  "XXXXXX\r" { receive $expect_out(0,string); exp_continue; }
}

Why does this give a

invalid command name "

error after the 5 second timeout elapses in the procedure? Are the nested expects OK?

1

1 Answers

3
votes

The problem is this:

  expect
  {
    timeout { send_user "\nDone\n"; }
  }

Newlines matter in Tcl scripts! When you use expect on its own, it just waits for the timeout (and processes any background expecting you've set up; none in this case). The next line, with what you're waiting for, is interpreted as a command all of its own with a very strange name (including newlines, spaces, etc.) which is not at all what you want.

What you actually want to do is this:

  expect {
    timeout { send_user "\nDone\n"; }
  }

By putting the brace on the same line as the expect, you'll get the behaviour that you (presumably) anticipate.