简体   繁体   中英

Periodically send command with Tcl

I am writing a Tcl/Tk interface to communicate with a temperature controller over Modbus. I would like to periodically (ie. every 5-10s or so) send a query to the temperature controller to get the current temperature.

Right now, I'm doing this by:

while 1 {
  get_current_temperature;
  after 5000;
  }

This works, but it prevents the user from interacting with the GUI for the duration of the after command, which is very undesirable. Could anyone suggest a workaround for this?

Thank you for your help!

You are looking for the every proc: https://wiki.tcl-lang.org/page/every

proc every {ms body} {
    after $ms [namespace code [info level 0]]
    uplevel #0 $body
}

every 5000 get_current_temperature

The every command reschedules itself after the specified number of milliseconds, then runs the command. This is the other way around from most of the versions on the wiki page. It has the advantage(?) that it isn't affected by the time the command takes to run. It also doesn't stop the next iteration if the command throws an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM