LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Detect data type within Variant

Hello,

 

I'd like to create a VI that has a variant control as an input, and I will need to pass different kinds of clusters in through this variant.  I will then need to pull out data from these clusters.  However, the Variant to Data VI wants a type, and it will not accept a generic (empty) cluster - it wants the exact kind of cluster that it should cast to.  But since my VI needs to accept different kinds of clusters, I cannot hardcode this.  I have looked into type descriptors, but it seems that the information on type descriptors is sparse and very poorly documented.  Is there an easy way to pass different kinds of clusters into a VI without hardcoding the cluster information within that VI?

 

Thanks,

 

Erik

 

0 Kudos
Message 1 of 20
(8,941 Views)
Hello Erik,
Are the clusters completely arbitrary or will you only be passing through a certain set of variant clusters? If there is only a few types of cluster that could be passed, you could implement the processing using a case structure; the varient data could all contain an encoded value that signifies what type of cluster data is contained and you could process it accordingly.
Does this help, or will the cluster data be completely arbitrary for each iteration?

 


Alex Thomas, University of Manchester School of EEE LabVIEW Ambassador (CLAD)

0 Kudos
Message 2 of 20
(8,937 Views)

Hi Alex - thanks for the response.

 

My goal is to pass an arbitrary cluster to the VI.  The only way I could think of to do this was to first cast these arbitrary clusters as variants, and then have the VI extract the cluster from the variant input.  What I discovered is that getting an arbirary cluster back out of a variant either required 1) knowing the exact kind of cluster so that it can be passed into the Type input on the Variant to Data VI (which defeats the purpose); or 2) understanding this complex array of hexadecimal type descriptors (and frankly, even if I could understand it, I'm not sure how I would use it to recreate the cluster and all of it's information).

 

Does this makes sense?

 

Erik

0 Kudos
Message 3 of 20
(8,863 Views)

Check out this handy-dandy VI from vi.lib

 

ContainsType.png

Message 4 of 20
(8,856 Views)

I do this all of the time with my Driver APIs.  Create Type Defs for all of your clusters then place the cluster type defs an the receiving VI and hide the controls.  The controls are only used to reconstruct the data type.  My command cluster has the Case to execute and the data in Variant form.  Each case will reconstruct it's data.

 

Variant.JPG

Visualize the Solution

CLA

LabVIEW, LabVIEW FPGA
Message 5 of 20
(8,848 Views)

Dave's posted approach will work but also requires the VI using the variant KNOW about all possible cluster types.

 

If that is a restriction on your part then you may want to look closely at LVOOP. In LVOOP all classes descend from a single parent so a single wire can carry any LVOOP class. A bonus that comes with LVOOP is rather than having to support code that decides "which version of cluster to use for the conversion" LV does that for you automatically behind the scenes.

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 6 of 20
(8,840 Views)

Darin - thank you for your post.  I have played around with that VI, but can't figure out yet how to use it to reconstruct an arbitrary cluster contained within the variant.  Have you done this before with that VI?

 

Thanks,

 

Erik

0 Kudos
Message 7 of 20
(8,834 Views)

Dave,

 

Thanks for your reply.  I'm plan to use my VI in an awful lot of places, so I was trying to avoid something like this, but it's good to see how one would implement it.

 

Thanks,

 

Erik

 

0 Kudos
Message 8 of 20
(8,831 Views)

@Erik the Red wrote:

Darin - thank you for your post.  I have played around with that VI, but can't figure out yet how to use it to reconstruct an arbitrary cluster contained within the variant.  Have you done this before with that VI?

 

Thanks,

 

Erik


Sorry, I read the title of your post more closely than the message.  In scripting there are many times when a value is passed as a variant and I want to know if a certain 'data type' is contained.  Getting the value is a bit trickier.  What exactly are trying to do with the arbitrary clusters?  Extract all of the values, all values of a certain type? 

0 Kudos
Message 9 of 20
(8,828 Views)

I have a bunch of large-ish clusters around my lvproj that contain configuration data for various DAQmx, NI-SCOPE, etc. devices.  I now need to write this data (and other kinds of data) to my TDMS file.  So I'm trying to create a class whose object I can pass around the project aggregating all this info into an array of strings.  In order to do this, I need a single "write" method that can convert these clusters to strings containing the label and value of each element in the cluster.  I want to avoid having to have a ginormous case statement and pre-populate it with all of the different kinds of clusters that could be passed, since that's a large number and I'd like to have a general purpose VI to do this on any project.

 

I've figured out how to programmatically pull out a label/value for each element in a cluster....but you need the cluster in the first place.  Hence trying to extract the cluster from a variant (the variant is being used so that the "write" method can accept any kind of cluster).  What's frustrating is that in testing all of this, the strings that are displayed in a variant's indicator are exactly what I want, but I can't figure out how to get it!  When I create a cluster of a DBL, string, and I32, the variant indicator shows: 

 

'Num1_DBL' -> 3.142E+0

'MyString' -> Hello

'Num2_I32' -> 23

 

...which are exactly the labels and values for the elements in the cluster....sigh...

 

0 Kudos
Message 10 of 20
(8,825 Views)