LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Event-driven programming...can anyone help with this code

Anthony,
Thanks for your reply. Was wondering if you had a chance to look into my code and advise on some suggestions.
0 Kudos
Message 11 of 21
(2,705 Views)

Anthony,

Can one have multiple producer/consumer design patterns in the same application...something like nested for/while loops. Assume I have a producer/consumer design pattern 'A'. Can the consumer 'A' be the producer for another producer/consumer design pattern, let's say 'B'? Also could you explain a little bit more about queue being enumerate and using a case structure in the loop.

0 Kudos
Message 12 of 21
(2,697 Views)
As far as I know, you can mix up producer and consumer loops as you like, as long as you keep everything under control 😉

I have a few applications where the SAME loop is both a producer AND consumer (Adding on extra tasks depending on the outcome of other tasks).

You can also have several producers for one consumer, although timing here can be somewhat problematic.....

If you need to daisy-chain producer/consumer schemes, go ahead and do it.  I'm not aware of anything to prevent this.

Hope this helps

Shane.
Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
0 Kudos
Message 13 of 21
(2,687 Views)
Can anyone help me in setting up multiple Producer/Consumer loops. I have two Producer/Consumer loops A and B. The Consumer of loop A will also be the Producer of loop B...sort of a daisy chain. I am little confused in setting up these loops, so would appreciate if someone can drop in a few suggestions on this issue. Thanks!
0 Kudos
Message 14 of 21
(2,676 Views)
Sorry for not replying earlier...  Have been a little busy...
 
 
Setting up multiple consumers/producers is easy.   Just make multiple loops.   Do NOT nest them.   Just put them parallel.    Each consumer should have it's own queue.      Telling a different consumer to do something, is simply a matter of putting data/instructions on the corresponding queue.
 
I've attached a little example.    It's the basic producer/consumer  structure using events,  and then expanded with two extra consumers.
 
I've used queues with enums, strings, and clusters, to show the different possibilities.  Notice that all consumers can become producers, by putting elements on any queue.   In this case, consumer C has two actions that create  data/instructions  for consumers A and B.
 
 
 
0 Kudos
Message 15 of 21
(2,661 Views)
Anthony,
Thanks for your reply. Can you take a look at the following code and suggest if there is anything wrong with it. The program basically generates an event each time the frequency of the function generator changes (data is read from a text file) and simultaneously triggers the digitizer card for data acquisition (data is acquired by the card continuously which is triggerred each time the frequency changes). This is the problem I am facing with the code: at times the code works fine, but most of the time it starts of working properly and then kinda of hangs up or goes into a suspended state after being triggerred by the first few frequency change events. I am thinking this is go to do with the triggering of the card, as this happens when the card is busy. So I need to send out a trigger (arm the card) from the event structure by checking if the card is busy or not. Again I am not sure if this is the source of the problem or it has something to do with the way I have set up producer/consumer loops. I have attached the code for your reference. The first program is the actual code which I am working on and the second one is the older version of the first one. Would appreciate your reply. Thanks!
0 Kudos
Message 16 of 21
(2,649 Views)

I've looked a bit at the code...    First question:  Why do you use a seperate producer A and producer B loop?   Seems to me that you could combine them into one loop.

Second:  From the description of the problem, and your code, it seems that you're creating a racing condition.    Your frequency event will be converted into a command on the consumer B queue..    So, it will wait untill that part is ready.

The question is wether you are allowed to change the frequency while the consumer is still busy.   If you aren't, then there's two obvious approaches.   One is to have the first loop wait untill the consumer is ready.   (using a indicator, semaphore or whatever)

More logical would be, to have the first loop continuing reading the file.  However, the actual changing of the frequency should be done in the consumer.  Thus, the loop that reads the frequencies will put a 'set frequency' command (and it's value) on the queue, followed by the arm command.   That way you can synchronize your actions very easily.  

 

0 Kudos
Message 17 of 21
(2,626 Views)

Anthony,

Here's the modified version of my application. It works perfectly fine right now, except that the following don't function as they are supposed to: when I connect probes or indicators to read from the local variables for distance and frequency, only the first value is read and then they remain constant, although the actual indicators (distance & frequency) update their respective values as their corresponding events are generated. I will need this information as part of a comment statement when I write data to file.

Also, I tried various options for implementing a STOP control for my program including stop value change events in the event structure, but none of them worked. The only way I can stop the program is by clicking the ABORT EXECUTE button, which is not recommended. Any ideas on this front will be great. Finally, if time permits can you look into my code and suggest ways of improving it (code efficiency) and any redundant code that can be eliminated.

Will look forward to your reply.

 

 

0 Kudos
Message 18 of 21
(2,613 Views)

Reading the local variables...    You must realize that a property node, local variable, or whatever, only produce data once.  If you want to read an indicator multiple times via a property node or local variable, then you need to put them multiple times on the diagram, in the corresponding places.   (That could also means that they are in a loop)

An 'update value (signaling)' property node will ONLY cause an 'value changed' event.  But it will not cause the 'value out' property nodes that 'belong' to the indicator to do anything.  If you want a local variable to be read after such an event, you should place a 'read value' property node within that event.      

Do I make sense?   Otherwise, try to do what you want in a small test program.   When I get stuck, I regulary make such small test program, to see if what I'm doing is at all possible.

As for ending the program.   If it doesn't end like it should, I suggest to 'step' through the program, and check wether your comparisons that control the ending of the loops are correct.   Probably, there's a +1 or -1 missing somewhere, causing a loop to run forever.  Ending the program shouldn't be a big problem.  

 

You might (should!) want to try to clean up the code a little bit, by making more use of sub-vi's.   That will also help you to better visualize what your program is doing, and where there are mistakes in the code.   

I see that you need many flat sequences to control data flow.  Seems that controlling the device is using subvi's without error lines.   You might consider encapsuling those vi's by your own, so to simply include an error line.      Very simple:  use the 'sub-vi with error handling template';  put the device vi into the no-error part;  make controls/indicator for the device vi input and output in your subvi.   That will clean up the diagram immensly.

0 Kudos
Message 19 of 21
(2,596 Views)

Anthony,

Can u look into the code now...when the program is run, I am prompting the user for saving data to a file (which is the first task of the program). When the user clicks 'yes', a file dialog box pops-up and the selected path is passed as a local variable to the 'write to measurement file vi'. The problem is this...when this is done another file dialog box pops-up. I am not sure how to resolve this problem.

Also, I could not figure out a way to STOP the program. Checked all the loops and they are working fine. And I did not quite understand the last part of ur previous reply. Will look forward to ur reply.

Thanks!

0 Kudos
Message 20 of 21
(2,564 Views)