2
TCL中是否有方法将一段代码放在超时模块中?我的意思是,即使执行没有完成,块会在特定的超时后退出。 例如: -使用TCL的超时时间
timeout (interval) {
#wait for socket connection here
}
如果没有建立连接在间隔时间,块退出。
感谢和问候, 综合外电
TCL中是否有方法将一段代码放在超时模块中?我的意思是,即使执行没有完成,块会在特定的超时后退出。 例如: -使用TCL的超时时间
timeout (interval) {
#wait for socket connection here
}
如果没有建立连接在间隔时间,块退出。
感谢和问候, 综合外电
综合外电, 您正在寻找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!"
}
}
编辑 你需要与你的UART设备进行通信使用non blocking I/O。
嗨拜伦,thanx的回应。 我应该在问题中更具体一些。 我真正需要做的是通过UART接口发送一些数据给我的设备,有时脚本挂起。我需要在这种情况下中断脚本/块。我认为超时模块会有所帮助。任何想法我还能做什么? – Anjali 2010-03-05 20:18:03
你应该能够适应上面的代码来做到这一点。只需添加你的代码来做'后'和'vwait'之间的UART连接。让结果进入状态。在'vwait'关闭UART连接后。仔细看一下上面的代码,它应该很清楚。 – 2010-03-05 20:26:40
vwait +后不解决提出的通用问题。该解决方案仅适用于Anjali需要的特定情况,并且仅在使用Byron提及的非阻塞I/O时才适用。检查我的问题/答案,了解原因:http://stackoverflow.com/questions/23096240/tcl-vwait-and-after-not-as-expected 我仍然在寻找一种方法来实现这一点,如果有人知道... :) – msb 2014-04-16 01:19:01