08-18-2015 11:00 AM
Hi there, in my application I'm enqueing messages to a consumer loop and was wondering what the best way to do it is. The message can have a string/enum state and a variant data. When I send the variant data, I want to send (1) test parameters and (2) an initialized set of data to be manipulated. Is it best to enqueue two separate elements, or to bundle these together and send a single message? This consumer loop is the only place that can dequeue the elements.
Also, I want to read the data in my "Start Test" state, not in my "Idle" state where I wait for messages. Is it better to send the state and data together and store the data in a shift register, or to send one message with the state and another with the data that will be dequeued in the "Start Test" state? I can create a snippet if this second question doesn't make sense.
08-18-2015 11:04 AM
Your consumer loop should always utilize a single Dequeue node. If you start using multiple, you can end up with race conditions. Just bundle all the data you want to send in to one datatype and send it all at once. If you only want to send part of the data within the cluster, that works too, just check what data is there in your consumer loop to handle that possibility.
Cheers
--------, Unofficial Forum Rules and Guidelines ,--------
'--- >The shortest distance between two nodes is a straight wire> ---'
08-18-2015 11:04 AM
I would just use Variant Attributes to send all of your data in one message. And if you want to use the data in a later state, use a shift register.
08-18-2015 11:35 AM
It sounds like you want the Queued Message Handler, which you can find as a starting point for a New Project, along with a description/discussion of what it is and how to use it.
Basically, you generate Messages ("things to do"), bundle the Message along with its Data (which can be different for each Message) and put it into a Message Queue. It goes to a Message Handler Loop, which acts like a State Machine -- it pulls off the latest Message, goes to "Do" what the Message indicates, using the message-specific Data, and can either itself generate additional Messages (for itself) or go back to wait for the next Message from somewhere else.
It is really quite easy to use, and quite powerful. You can easily set up multiple loops running in parallel, passing messages back and forth to synchronize activity with each other.
Bob Schor
08-18-2015 11:50 AM
@crossrulz wrote:
I would just use Variant Attributes to send all of your data in one message. And if you want to use the data in a later state, use a shift register.
I looked up variant attributes because I had never heard of them. Can you explain in a sentence or two how they pertain to this case?
08-18-2015 11:52 AM
@Bob_Schor wrote:
It sounds like you want the Queued Message Handler
Bob Schor
I do know about the QMH, but I was reading that it is bad for readability and convolutes the idea of a state machine because once the loop starts enqueing messages to itself it is hard to trace the execution. This is why I wanted all messages to be sent from the producer to the consumer.
08-18-2015 12:24 PM
@Gregory wrote:
I looked up variant attributes because I had never heard of them. Can you explain in a sentence or two how they pertain to this case?
A variant attribute is like a look up table, where there is a key and value pair. The key is a string, the value can be anything including a variant, which it self can contain variant attributes.
Basically if your payload is a variant, you can put any and all things you would want in there. Of course errors can occur if you are trying to get data that doesn't exist, but that's to be expected.
There are lots of examples of using variant attributes but my favorite experimental way is with some XNodes I made and called a Variant Repository. If XNodes scare you a bit I posted a version which uses polymorphic VIs but isn't as slick.
Unofficial Forum Rules and Guidelines
Get going with G! - LabVIEW Wiki.
17 Part Blog on Automotive CAN bus. - Hooovahh - LabVIEW Overlord
08-18-2015 12:50 PM
@Gregory wrote:
I do know about the QMH, but I was reading that it is bad for readability and convolutes the idea of a state machine because once the loop starts enqueing messages to itself it is hard to trace the execution. This is why I wanted all messages to be sent from the producer to the consumer.
Two comments.
Bob (Too Much Reading Can Be Bad For You) Schor
08-18-2015 12:56 PM
Shane posted a nuget some years ago on Function Overloading which I believe is the subject being discussed in this thread.
Ben
08-19-2015 10:49 AM
Thank you all for your input, attached is the rough implementation that I have done so far. Also, I noticed if I wire one of my controls (like a double) into the variant node of the "bundle" function, then I get a coersion dot and it seems to work when I convert it back from a variant. Is this a sure thing, or should I always use the "to variant" function before wiring into a variant node?