使用TCL超时

Anj*_*ali 2 tcl

在TCL中有一种方法可以在超时块中包含一段代码吗?我的意思是即使执行没有完成,块也会在特定超时后退出.例如:-

timeout (interval) {
#wait for socket connection here

}
Run Code Online (Sandbox Code Playgroud)

如果在间隔时间内未建立连接,则块退出.

谢谢和问候,Anjali

Byr*_*ock 5

Anjali,你在找vwait.

下面是一个示例:等待五秒钟连接服务器套接字,否则关闭套接字并继续运行脚本:

# 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!"
   }
}
Run Code Online (Sandbox Code Playgroud)

编辑 您需要使用非阻塞I/O与UART设备进行通信.