LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Interrupting code within an event structure

Solved!
Go to solution
Solution
Accepted by smallen

@smallen wrote:

Hi there,

 

Am I right in thinking that the case structures are being selected for by the processes/subVIs 1 and 2 completing? So the case is being determined by the iteration of the loop?

 

Also, will this not still freeze up the VI if the "2" box is just a wait? I think your suggestion will allow me to interrupt the write and read which are over serial and practically instantaneous compared to the exection time of the process on the instrument (which is variable depending on the command and could be any value from 20 ms to several minutes)

 

Thank you for your help, but I think if the wait is "2" in your example, I am looking for how I can interrupt "2" itself rather than between the three processes.


The case structure is selected by the loop iteration, yes.  I kept it simple for the example and just numbered them 0,1,2 but when I write code like this I will often make an enum with 3 values, naming them something short but informative (such as "Turn on", "Wait", "Shut down"), and then pass that into an Enum to Array of Enums node and iterate based off of that (this is sometimes known as a "Natt sequence").  Every time you want to have a point where you can interrupt, start a new case to split up the code more.  Anything that needs to always run, i.e. a safe shut down or data saving, leave that outside of the loop.

 

If the VI that is box 2 is just a wait, then you just have to find a way to split up the wait.  I showed one example where a "TCP read" with a timeout of 5 seconds could be replaced with a TCP read of 1 ms that ignores timeout errors and only stops the loop after getting all expected content or has looped enough time to elapse 5 seconds.  You can do similar things with the "wait" function:

Kyle97330_0-1721424695798.png

Since it's sounding like it's not an actual wait function, but something similar, post it and there's probably a workaround to interrupt that as well.  If it's a VISA read then changing the example I made earlier to use a VISA read with a short timeout written earlier with a property node will probably work, possibly with an additional check for a termination character.  Interrupts can work if it's in a separate VI as well, there are multiple options for proper signaling between VIs that you can look up or possibly make a new post about.

Message 11 of 15
(244 Views)

Hi McDuff,

 

How am I getting between the states? Can the end of one state executing trigger the start of another? If so, and it triggers straight away, why do they have to be seperate states? How do I get from the timeout state to the read state without user input?

 

I have tried with a parallel loop to no avail. The "stop" button that I want is just a stop button on the front panel of the VI. I am really surprised by how difficult it is to do this considering many laboratory applications will need to be able to interrupt timed sequences of input/output!

 

Thanks for your help.

 

Sam

Samuel Allen
University of St Andrews
0 Kudos
Message 12 of 15
(215 Views)

Hello Kyle,

 

Firstly thank you for your help.

 

I'm not sure if I explained my issue correctly. All I am looking for is for my code to recognise when a process on the instrument has finished (by calculating the time it should take and waiting until that time is elapsed) and then communicating with the instrument to receive values. The wait time is therefore just a wait - it does not need to do anything in this time, the instrument itself handles whatever the instruction is completely independently. I want to be able to interrupt at any time, for safety reasons, so I don't think making a new case to split up the code will work unless I have a sequence of hundreds of cases.

 

I implemented the while loop you provided and I can't interrupt it. The boolean is completely frozen out whilst the loop runs, so I cant switch its value. The full time always elapses. This is how I tried to implement it, with an example command.

 

smallen_0-1721641395537.png

 

Here, the command sent is "forward,x" which moves a motor forward x steps, with each step taking 20 ms. I added a small buffer time of 50 ms for safety. The case writes the command, calculates the time needed to wait... then it just waits. The boolean is useless - the program completely freezes it out. The visa name out and error then go into the code that reads back and interprets the data, which works fine. The writing of the command also works perfectly - it is just this variable, interruptable wait time that is causing a huge headache!

 

I hope this helps to explain my problem(s) more clearly. Thank you very much for your help.

 

Sam

Samuel Allen
University of St Andrews
0 Kudos
Message 13 of 15
(213 Views)
Solution
Accepted by smallen

@smallen wrote:

Hello Kyle,

[...]

I implemented the while loop you provided and I can't interrupt it. The boolean is completely frozen out whilst the loop runs, so I cant switch its value. The full time always elapses. This is how I tried to implement it, with an example command.

[...]

When you edit an event of a event structure there is a check box called 'Lock panel (defer processing of user actions) until the event case completes'. Did you uncheck that?

See: https://www.ni.com/docs/en-US/bundle/labview-api-ref/page/dialog-boxes/edit-events-dialog-box.html 

Message 14 of 15
(195 Views)

Thank you so much! As is often the case, the simplest things are often the cause of the problem!

 

The solution that works for me is the while loop as suggested by McDuff combined with unchecking the box you just suggested.

 

Thank you very much for your help!

 

Sam

Samuel Allen
University of St Andrews
0 Kudos
Message 15 of 15
(186 Views)