LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I add a timeout to a loop?

I quite often find myself creating a while loop that is interrogating an instrument, waiting for a specific event (e.g. a mask test on a scope). This kind of loop really needs some sort of timeout in order to avoid the possibility of spinning forever.

Is there any clever way of saying "run this loop until or "?
0 Kudos
Message 1 of 7
(3,857 Views)
I don't know how clever it is but all you have to do is outside the loop, get the current time (Get Date/Time in Seconds or Tick Count function), use the same function inside the loop and subtract the initial time value from the current time. Then check if its less than a timeout value that you pass to the VI. Combine this boolean output with the conditions that would normally stop the loop. I usually modify the output error cluster as well so that I can report either an instrument error or a timeout condition.
0 Kudos
Message 2 of 7
(3,857 Views)
That's the obvious solution, but it has one major flaw in that it's difficult to ensure that the initial time is set properly.

Let's see if I can explain this. Take the case where I have several time consuming steps, the third of which is the loop that I want to provide with a timeout. If I just wire up a TickCount function (to provide initial time) outside the loop then the initial time will get set to the point that the entire VI starts, not the time that the loop starts.

I suppose I could solve this with sequence structures but I *hate* sequence structures since they make it very difficult to read the code...
0 Kudos
Message 3 of 7
(3,857 Views)
I solved that by creating Tick Count and Get Date/Time VIs that have an error in and error out cluster. I agree with you about sequence structures.
0 Kudos
Message 4 of 7
(3,857 Views)
Ah...yes. In fact another engineer and I were just muttering "why didn't NI hook up error in and error out to their timer structures?"
0 Kudos
Message 6 of 7
(3,857 Views)
OK, in the grand tradition of answering my own questions, here's my current solution to this problem. The attached timeout VI is wired up to the loop indicator and outputs a true when the desired timeout has expired.

Note that it also handles the fact that the timer rolls over.
Message 5 of 7
(3,857 Views)
Thanks, I already had a similar VI but found this one better
0 Kudos
Message 7 of 7
(3,857 Views)