Labview - Using Queues to Control Program Flow & Provide Structure - Owlcation - Education
Updated date:

Labview - Using Queues to Control Program Flow & Provide Structure

Author:

Automation Software Engineer with 10+ years experience in automation, instrumentation and software programming.

labview-using-queues-to-control-program-flow-provide-structure

Drag the above image into a LabVIEW VI to drop the contents on to the VI. (LabVIEW 2018+)

What Are Queues?

Queues are exactly what they sound like, Queues. They can be used for multiple purposes, but one that we will explore in this article is using them to enforce program structure and flow. We will be using Queues to handle an action request and execution.

Queues are essentially bits of information (of a known type) placed in the queue structure and then processed when required or needed. The queue is typically read as "First In First Out", but can be written to from either end. Other actions, such as flushing the queue back to empty can also be performed.


Initialising Queues

labview-using-queues-to-control-program-flow-provide-structure

In the example above, a new Queue is being initialised called "Main". It has a data type of a "Cluster" (at this point it is not important to know the contents, we'll come back to this later).

The TRUE constant is wired to the "Create if not found" input, this will create the Queue "Main" if it does not already exist.

The FALSE constant is wired to local variables to ensure that the VI can start without a stop command from a previous run being present

Enqueuing An Element

labview-using-queues-to-control-program-flow-provide-structure

As seen in the Initialisation structure, the data type that the Queue is using is made up of a Cluster containing 3 elements. A String, A Variant and a TimeStamp.

A While loop is used as a "Message Generator". This loop is monitoring button presses, when a button is pressed, a message is enqueued in to the Queue.

Passing an element to the queue is as simple as using the "Enqueue Element Vi" from the Data Communications > Queue Operations palette.

The example above shows the logic for the event "Button 1" being pressed.

  1. Button 1 is pressed
  2. The Case Structure becomes TRUE
  3. The Selects pass a value of 1 to the inner Case Structure
  4. The inner Case Structure passes "Button 1 Pressed" to the Data part of the cluster for the Queue (Note that this is implicitly converted to a Variant)
  5. "Button" is passed to the Type part of the cluster
  6. The cluster, now populated with data, is passed to the Enqueue Element VI

Once the above has been completed, the "Main" Queue will now have a cluster containing the data stored as an element, ready to be de-enqueued and read.

Dequeue Element

labview-using-queues-to-control-program-flow-provide-structure

In the image above, the Dequeue Element VI (the one with the Hourglass in it) is obtaining the element that is at the FRONT of the queue (in this example, the oldest element).

The cluster data is then examined and the "Type" field checked. Because our button handler set the Type as "Button", the Case Structure uses the "Button" case.

Inside the "Button" case, the Variant Data is converted to a String (as we know button will always be a string type).

The inner Case Structure does not contain a case for "Button 1 Pressed", so default is passed, which passes a FALSE to the stop command.

On the front panel, you'll notice that when Button 1 is pressed, the "Processed Message" indicator will show the information passed by Button 1

Using A Common Queue For Different Data Types

The example we're using can process 3 different events

  • Button 1 Pressed - No Action
  • Stop Button Pressed - Stop the VI
  • Run Time Counter Update - Increment the Seconds Run Counter

These are all handled by the "Message Hander".


Stop Button Pressed - Sends a stop to all loops

Stop Button Pressed - Sends a stop to all loops

Seconds Run Update. Sends the new number to the indicator on the front panel. Notice the "Variant To Data" VI is converting to a number in this case

Seconds Run Update. Sends the new number to the indicator on the front panel. Notice the "Variant To Data" VI is converting to a number in this case

Common Message Handling

In most cases there will be something that you want to do to all messages that are recieved. Everything outside of the case structure in the message handler is executed on every message recieved.

The message handler loop waits at the Dequeue Element VI until an element is in the queue. A timestamp is added and the entire element cluster is passed to the "Processed Message" indicator. Then the message is unbundled and processed in accordance to its type.

Every single message that is recieved has a timestamp added and updates the indicator, regardless of type or data. The nice thing about using a Variant too is that the raw data is displayed in the front panel cluster no matter what type it is

Stopping The VI

Remember to stop all loops correctly. In the example we're using local variables, but you could also destory the queue reference and stop all message generators on an error. Be sure that no matter what the state of your loops are, they are always ready to accept a stop command.

© 2019 Liam