tags:

views:

151

answers:

1
+1  Q: 

timeout using TCL

Hi,

Is there a way in TCL to enclose a piece of code in a timeout block? What I mean by that is that the block will exit after a particular timeout even if the execution is not complete. For instance:-

timeout (interval) {
#wait for socket connection here

}

If no connection is established in interval time, the block exits.

Thanks and Regards, Anjali

+4  A: 

Anjali, You are looking for vwait.

Here is an example: Wait five seconds for a connection to a server socket, otherwise close the socket and continue running the script:

# Initialise the state
after 5000 set state timeout
set server [socket -server accept 12345]
proc accept {args} {
   global state connectionInfo
   set state accepted
   set connectionInfo $args
}

# Wait for something to happen
vwait state

# Clean up events that could have happened
close $server
after cancel set state timeout

# Do something based on how the vwait finished...
switch $state {
   timeout {
      puts "no connection on port 12345"
   }
   accepted {
      puts "connection: $connectionInfo"
      puts [lindex $connectionInfo 0] "Hello there!"
   }
}

Edit You will need to communicate with your UART device using non blocking I/O.

Byron Whitlock
Hi Byron, thanx for the response. I should have been more specific in the question. What I actually need to do is send some data over the UART interface to my device under test and sometimes the script hangs while doing this. I need to interrupt the script/block in that such a case. I thought a timeout block will help. Any ideas what else i can do?
Anjali
You should be able to adapt the above code to do that. Just add your code to do the UART connection between `after` and `vwait`. Have the results go into state. After the `vwait` close the UART connection. Take a closer look at the above code and it should be clear.
Byron Whitlock