From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

MASTER/SLAVE PATTERN: how to stop slave loop without stop main loop

Hi All, I am studying the master/slave loop and saw an examples are like this (only master block shows)

master.gif

From this case, as I understand, if we click "set" button, slave loop will run. If we click this button again to mak it "Flase", the slave loop will be stopped as well as the main loop will be stopped as well.

 

Now if I have a multi-function main loop, I just want to start or stop slave loop when click "set" button, how can I do it? Thank you very much! 

 

 

0 Kudos
Message 1 of 16
(3,170 Views)

Your picture doesn't show how the master loop is stopped.

 

The Set Value event is just putting the string "Set Pressed" into the notifier.  It doesn't matter whether the set button is being pressed to make it true, or pressed to make it false.  Either way, a False constant is being sent to this loop's stop terminal.  There must be some other event in that event structure (possibly a Stop button Value event?) to stop the master loop.

0 Kudos
Message 2 of 16
(3,164 Views)

Hi RavensFan, could you share with me any examples you have for master/slave structure? Yesterday you advised me to look for producer/consumer pattern, it is very useful. And I found that master/slave structure is the most suitable one to solve my problem.

But I cannot find any vi file to show how this pattern works, do you have that? The picture is from an internet example, but it is only a picture, not the real vi.

 

0 Kudos
Message 3 of 16
(3,160 Views)

Hi RavensFan, I want to decribe the code and what I want in more detail and hope you can help me.

I have a main program, which has a main while loop. In this while loop, I can trigger some events to read out some data.

Now I want to add a another feature to this program, it is another while loop. In this while loop, some data can be read out on a real time basis. As this loop runs, the data keeps changing.

What I want is to use the 1st loop as master loop, and 2nd loop as slaver loop. And use a boolean button (we can call it "real time scanning") in front panel to trigger the notifier. When we press this button "real time scanning" , the second loop begins to run, and I can see transient data.

When we disable the slaver loop, we can still use other features of the main program, to trigger other events.

Could you advise how can I implement it?

Thank you very much! 

0 Kudos
Message 4 of 16
(3,156 Views)

@bhl3302 wrote:
From this case, as I understand, if we click "set" button, slave loop will run. If we click this button again to mak it "Flase", the slave loop will be stopped as well as the main loop will be stopped as well.

Your understanding is completely wrong.  There is nothing in the image you show that would stop either loop.  In this situation the "set" button would normally have a latching mechanical action, meaning that when it is pushed, it will stay true until it is read once, at which point it will return to false.  Placing the control terminal inside the event case causes the terminal to be read and resets the button to false.  With a latching mechanical action, there will be only one event generated even though the boolean will change value twice (from false to true, and from true to false).  You'll never have a situation where you push the button once to set it true, and again to set it false.  However, even if the mechanical action is switching, not latching, it still won't make a difference here - the event case does the same thing whenever the value changes, regardless of whether it's true or false.

0 Kudos
Message 5 of 16
(3,152 Views)

I would not stop the slave loop.  Once you stop it, there is no way to restart it until you stop all of your loops and restart your program.

 

What you should do is have a case structure where if your real-time mode is true, then it puts data into the queue to send to that slave loop.  If it is false, then it doesn't enqueue data for the slave loop.

0 Kudos
Message 6 of 16
(3,140 Views)

Hi RavensFan, thank you for your advise! To realize what you told me, is master/slave pattern still the best choice? Or I should use producer/consumer pattern?

0 Kudos
Message 7 of 16
(3,118 Views)

I don't know what the distinction is between master/slave versus producer/consumer.  I consider them all producer/consumer where one loop produces items to put into a queue, and the other consumes the items in the queue and acts on them.

0 Kudos
Message 8 of 16
(3,114 Views)

OK, thank you very much! I will let you know how it works.

0 Kudos
Message 9 of 16
(3,112 Views)

Hi RavensFan, I use an example to wirte a code by myself. Attached is its simplified version. As you can see, when we start the program, consumer loop is always running, no matter how the data sent from producer loop. Could you advise how can use the button "start/stop consumer loop" to control the consumer loop?

I think when it works properly, if we click the button to switch the boolean value to be true, the light will be ON. When we click it to make it false, the light will be OFF. 

0 Kudos
Message 10 of 16
(3,092 Views)