LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how to stop while loop

I can't figure out how to stop a while loop in my labview program. 
 
When the user presses the Run arrow in the toolbar I want my program to begin reading the serial port for GPS messages.  These messages should be displayed on the front panel.  Currently I have this read/display in a while loop.  The program is also waiting for an extrenal trigger.  When that trigger arrives, I want to grab the current string from the serial port and save it and continue reading and displaying the serial/gps string.  This trigger starts the other parts of the program- signal generation, recording, and saving data which need to run concurrently with the serial/gps reading/displaying.  Once the AO and AI have finished and the data have been written to disk, I want the program to stop.  The serial/gps messages should be updating this whole time.  Only when the data are written to disk should the whole program end.  This whole sequence of events should only be done once when the user preses the Run arrow. 
 
So far I'm unable to pluck the serial string when the trigger comes in if I'm watching the serial port all the time.  The program also doesn't stop when it finishes writing to disk because the read serial while loop is still running.  I don't want to use a front panel stop button.  The program should stop itself when the data havebeen written. 
 
I'm really stumped on this one but I'm new to LabVIEW so I'm sure there's an easy solution to this. 
 
Thanks for any and all help. 
 
 
 
0 Kudos
Message 1 of 9
(5,573 Views)
Basic dataflow.
 
FIrst: you wire a diagram constant to the iteration terminal, thus the termination condition is always false.
Second: the diagram constant is wired from outside the loop, so even if it were a control, the new value would not propagate inside the loop once the loop has started.
 
I am not sure what the purpose of that loop is since there is already an inner loop that stops on error. Can you explain? Maybe just remove that loop entirely?
 
0 Kudos
Message 2 of 9
(5,564 Views)
The first thing to do is to replace the for loop with a while loop so that the program runs continuously. Do NOT use the run continuous button on the toolbar. Then you can move the serial read while loop outside the main while loop and the two loops will run independently. Your main loop would have a stop button and the stop condition for the serial read loop would be OR'ed with the stop condition of the main loop. One way to do that is to read a local variable of the stop button in the serial loop. There are other ways than reading a local and you have to have the right mechanical action in order to use a local but this should get you past the major hump. There are a lot of other things you can change. You should move the DAQmx channel/start task/stop task/clear task functions to the outside of the main loop. These are pretty time consuming actions and you don't need to do these with every iteration of the loop.
0 Kudos
Message 3 of 9
(5,558 Views)
altenbach and Dennis-
 
Thanks for your prompt responses.  I'm trying to understand your suggestions. 
 
altenbach-  you are right about the outer while loop on the gps/serial read being unnecessary.  I've tried so many different approaches to this that my old mistakes are still haunting me.  I've removed the outer while loop.  But I still have to stop the inner loop. 
 
I don't understand what you mean by "you wire a diagram constant to the iteration terminal, thus the termination condition is always false".  The iteration terminal in the lower left of this while loop cannot be wired into.  It's an output.  You must mean a different terminal than what I'm thinking of.  Can you clarify this?  Are you referring to the conditional terminal? 
 
Dennis-  I removed the for loop that surrounds the entire program.  Since it was set to 1 it wasn't doing anything other than running the program once.  I was trying that as a way to get the program to stop, but since it was getting hung-up inside a while loop it was never getting to the end of the outer for loop.  I've removed that and it makes no difference- as one would expect.  I think I understand why you suggest a second while loop- one for the serial read stuff and one for all the other stuff.  That would allow the two process that I want to run in parallel ( which is what I want).  But because I don't want a stop button anywhere, I won't be able to stop either loop.  At least I don't see how.  Besides, what's the point of creating a second while loop if I only execute the stuff inside the loop (here I'm talking about the one for all things non-serial) exactly once?  Why use a loop at all?  
 
I appreciate your help.
0 Kudos
Message 4 of 9
(5,549 Views)


@psice wrote:
Are you referring to the conditional terminal? 

Yes, sorry for the confusion. 😉
0 Kudos
Message 5 of 9
(5,546 Views)

altenbach-

Thanks for clarifying that.  If I understand you correctly, there is no way to change the value of a variable in the while loop once it starts.  Which is what I was trying to do- set variable to false, run while false, set variable to true when data written successfully, use this to stop the loop.  Are you saying the value of variables in a while loop do not update when the loop moves to a new iteration?

I could by way off here.  I've got to be honest your first post confused me.  Were you giving instructions, or pointing out why I wasn't able to get out of the loop? 

I've attached the newest version below, but it suffers the same problem. 

Any ideas on how to grab a snapshot of the serial port when I get an external trigger?  I basically want to know the gps time,lat,lon, etc at the trigger. 

Thanks

0 Kudos
Message 6 of 9
(5,538 Views)
If you only want the main section of code to run once, then don't have any loops at all. Since you want the serial to run continuously, you would still need a while loop around it but you need to pass a value to the conditional terminal in order to stop it. Since you can't wire a value externally into a while loop to stop it, you have to use some other mechanism. As I said, setting a local variable is one way. Pick a point in the main section of code where you want to stop the loop. At that point, write to a local variable. If the read of the local is inside the while loop, the while loop will sttop. You could also use a queue or notifier.
0 Kudos
Message 7 of 9
(5,532 Views)


@psice wrote:
If I understand you correctly, there is no way to change the value of a variable in the while loop once it starts. 

Of course there is! If you want a control to be read during loop execution, place its terminal inside the loop.

In your particular case, the loop continues as long as an error occurs. If it never succeeds, it'll never stop. Just place a stop button terminal inside the loop and do some boolean logic so it also stops if the button is pressed.

Ponder the following figure 🙂

Message Edited by altenbach on 10-13-2006 02:08 PM

0 Kudos
Message 8 of 9
(5,529 Views)
Dennis and altenbach-  Thank you both for your patience. 
 
I was trying to do just what Dennis suggested-"As I said, setting a local variable is one way." even before posting to this forum, but I couldn't get my local variables to reflect changes made elsewhere in the program and I wasn't able to wire from them because they were writes.  The critical part I was missing was how to change a local variable from a write to a read.  It was staring me in the face the whole time- just right click.  When I finally found it, my problems were solved. 
 
altenbach- thank's for putting the figures together.  I do understand the logic and wiring there, but I was really trying to avoid stop buttons.  The program should be smart enough to figure out when to stop.  And using local variables turns out to be one way of solving this.  I still have some clean up to do, but I've included my current working version just so you can see how I implimented your suggestions.  There's still a lot of clean up to be done, but I'm delighted to be able to watch the serial/gps messages until I'm done reading in data.  At first I had this stop variable set in the final sequence frame.  That didn't work because I wasn't getting to the final frame because the loop wasn't finishing.  Once I placed the stop variable in the same frame as the while loop it began stopping when it should. 
 
If you have other comments/critiques about the wiring diagram I'm earger to hear them.  I'm considering the structure finished, however.  It still needs cleaning up and commenting, but I'm satisfied with the functionality. 
 
Thanks,
Peter
0 Kudos
Message 9 of 9
(5,517 Views)