01-14-2013 01:20 AM
I have a producer loop produce '1' and '0', then I try to transfer '0' or '1' produced to consumer loop. Initially I get '0' for the consumer, but once the '1' is tansfered to it, the '1' will always be in consumer loop, '0' will not be transfered, even the producer do produce a '0'. How come it's like this?
Solved! Go to Solution.
01-14-2013 01:31 AM
Are you hitting the "stop3" button after receiving a "1"? If not, you'll be stuck in the while loop (inside the 1 case) and your queue will not be handled until the loop terminates.
01-14-2013 01:43 AM - edited 01-14-2013 01:49 AM
I see you are reading the boolean value from a global variable and then you are converting that into an integer and then you are sending the value to the consumer loop and reading it and doing some operation.
My question is: Why don't you wire the Global variable directly to the 2nd loop case structure (consumer loop) and remove the producer loop, this is rubed code definitely. Also in your code you are running the Producer loop without time delay with a case structure that is also wired by a constant so you will eat the CPU by running 2 very fast loops without giving handle to the OS.
Why you always run into the 1 case always: Assuming that you do not have anything in the case 0 of the consumer loop Here is the story for you.
You are giving 0 to the consumer constantly and since the case 0 doesn't have anything the speed matches the Producer but once you enter into the 1 you have some code there you are calling the driver and certainly it takes time than the Producer. Since you are continuously running the producer all the 1s are queued up and that is the reason even after changing the value you are running into the same 1 case in the consumer.
Suggestion: Go for a design pattern that matches your requirement.
Edit: Oops I didn't see the while loop inside.
01-14-2013 07:24 AM
Is anything happening in the "0" case?
I would recommend initializing and closing your instrument outside of the consumer loop. Then remove the innermost while loop. Then whenever you get a 1, you send the command and check the queue again.