Example Code

Using Variants to Pass Multiple Data Types Through a Single Queue

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Overview

This VI uses variants to effectively pass multiple data types through a queue.

 

Description

Many times when using Queues with a Producer/Consumer type architecture (or another usage), it is necessary to be able to pass multiple data types through a queue.  However, due to the limitations of the Queue in LabVIEW, only a single data type can be passed through any particular queue.  That is, each queue must be explicitly defined.  If you need to pass different types of data depending on the situation, this presents a problem.  One way to solve this would be to use a cluster, and pass all possibly relevant information through on every iteration.  This would work, however it isn't very efficient.  A preferred way of accomplishing this is to use variants.

 

A variant is like a universal data type.  It is simply a binary representation of data that has no specific display format, such as Numeric, Boolean, String, etc.  You can convert any data type to a variant and then back from a variant and retrieve the original information.  This is exactly what we will do here.  In this example, we have one queue that is capable of passing Doubles, Strings, and Booleans.  To do this, I have made a cluster that contains an enumerated control that specifies what the data type is, and then a variant which contains the actual data.  This way, when the consumer loop is trying to read the data, it knows how to "decode" it.  In order to make the use of queues easier to understand, I have implemented an event-based producer consumer structure.  For more information on event-based producer consumer architectures, please refer to the example finder in LabVIEW.

 

Requirements

  • LabVIEW 2012 (or compatible)

 

Steps to Implement or Execute Code

  1. Run the Main.vi.
  2. Select a data type (Double Precision, String, or Boolean), and then set a value for that data type.
  3. Press Enqueue and then observe that the indicator of the data type that you chose displays the information that you entered, and the other indicators are disabled

  

Additional Information or References

 

Front Panel

2.png

 

Block Diagram

 

1.png

 

 

**This document has been updated to meet the current required format for the NI Code Exchange.**

Wes Pierce
Principal Engineer
Pierce Controls

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
Han16
Member
Member
on

The document is very usefull