wdyjz 发表于 2009-6-16 22:43:52

Using Variants to Pass Multiple Data Types Through a Single Queue

http://decibel.ni.com/content/docs/DOC-4364

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

foolooo 发表于 2009-6-16 23:58:29

谢谢版主分享!另外我也查了些关于variant的说明,如果要传输的数据可能会有多种状态,或者要调用类似ActiveX和vi属性,那么variant是很好的选择。如果是大数据传输,而传输的数据类型又比较单一的话不建议使用。
页: [1]
查看完整版本: Using Variants to Pass Multiple Data Types Through a Single Queue