Labview - Using Queues to Control Program Flow & Provide Structure

Updated on December 31, 2019
LiamBee profile image

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

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

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

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

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


    0 of 8192 characters used
    Post Comment

    No comments yet.


    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at:

    Show Details
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the or domains, for performance and efficiency reasons. (Privacy Policy)
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
    ClickscoThis is a data management platform studying reader behavior (Privacy Policy)