LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to send multiple clusters of data to consumer queue?

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.

Download All
0 Kudos
Message 1 of 16
(5,538 Views)

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>   ---'


0 Kudos
Message 2 of 16
(5,531 Views)

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.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 3 of 16
(5,530 Views)

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

0 Kudos
Message 4 of 16
(5,492 Views)

@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?

0 Kudos
Message 5 of 16
(5,478 Views)

@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.

0 Kudos
Message 6 of 16
(5,475 Views)

@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.

Message 7 of 16
(5,450 Views)

@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.

  1. In the QMH, you don't have to have the Message Loop enqueue messages to itself -- it's just an option.  You could certainly make the loop the Consumer, and only put messages on in the Producer.
  2. Think of a non-queued State Machine, with the State in a Shift Register and set as the current State exits.  Now make this a QMH, where you enqueue the next Message just as the current "Message State" exits.  Seems to me it is the same thing, with the exception that the QMH could get messages enqueued at other places than the end of the State.  But you can look at the Queue wire to see where/if any other Enqueues are happening.  You can also use LabVIEW's Find command to look for the Enqueue Element function ...

Bob (Too Much Reading Can Be Bad For You) Schor

Message 8 of 16
(5,420 Views)

Shane posted a nuget some years ago on Function Overloading which I believe is the subject being discussed in this thread.

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 9 of 16
(5,405 Views)

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?

 

0 Kudos
Message 10 of 16
(5,326 Views)