LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Busy Waiting While Loop in SubVI?

I'm working with a motor drive that sets a busy waiting flag while an instruction is executing and clears the flag when an instruction has been completed. I need to poll the busy flag, so I was planning on creating a subVI that contains a while loop to poll the flag. But, I know that it is recommended that while loops should not be placed in subVIs. I was wondering if there's an alternative. Would it be ok to put my code in a subVI? I've posted the code as an attachment.

Thank You,
Molana

Message Edited by Molana on 08-21-2006 02:18 PM

0 Kudos
Message 1 of 8
(4,157 Views)

Nothing wrong with the way you are solving the problem or using a While Loop in a Sub-VI.  Just be sure to add a small delay like 20 milliseconds to prevent the loop from taking too much CPU.  While loops and Sub-VIs are fine.  Another tip maybe to add a timeout condition to the while loop.

 

Matthew Fitzsimons

Certified LabVIEW Architect
LabVIEW 6.1 ... 2013, LVOOP, GOOP, TestStand, DAQ, and Vison
0 Kudos
Message 2 of 8
(4,149 Views)
It is fine to put while loops in subvi's (in my opinion), as long as you can terminate it from the calling vi, either programmatically, or manually, and preferably both.

Use a queue or LV2 style global to pass a possible "abort" or "stop" button on the front panel of the calling vi, so that the subvi can be stopped for whatever reason.  References will also work, but have more overhead.
0 Kudos
Message 3 of 8
(4,144 Views)
I don't see any "flag" in your code. Is this just a boolean indicator on the calling VI that is lit while the subVI executes and reset once the call completes? Are you just setting the "busy" cursor?
 
You should make sure to place this in a paralell loop in the main VI so you're not blocking the UI code and other processes. Also, as others have mentioned, your while loop needs a small wait.
Message 4 of 8
(4,143 Views)
Would a semaphore work?

Lynn
0 Kudos
Message 5 of 8
(4,132 Views)
Well, the flag is ouputed through the "CR detect" Subvi. The string output of CR detect will either be 'B' for busy or 'R' for ready. The while loop will execute as long as the string output is 'B'.
0 Kudos
Message 6 of 8
(4,116 Views)
I'm not sure how to use a semaphore in this case.
0 Kudos
Message 7 of 8
(4,113 Views)
Molana,
 
I don't think a semaphore is what you are looking for.  You could possibly use an occurrance or a notifier though.  An occurrance will let you "wake up" your main thread when your subvi's while loop sees that a flag is set.  A notifier is like an occurrance except you can send back to your main loop once it is "woken up".  You can find examples of how to use these functions by looking in the example finder (Help->Find Examples).
 
I hope this helps,
Justin D.
Applications Engineer
National Instruments
0 Kudos
Message 8 of 8
(4,084 Views)