04-02-2016 11:50 AM
LV2103, Win7
Old hand at LabVIEW, brand new to DIAdem.
I need to convert a structure of arbitrary complexity into properties, suitable for writing to a TDMS file, in such a way as to be retrievable, and (hopefully) to be human-readable, such as whn the file is loaded into DIAdem.
I have most of it working just fine, but I have trouble with ENUMs.
An ENUM can be converted to a VARIANT just fine, but writing that variant to a file produces error -68007.
A simple test shows this:
The encoding process is fairly simple - given a generic control Ref, just look at its CLASS ID.
If the ID is one of the basic ones, just get its value as a variant, and append to the NAME and PROPERTY arrays:
If the item is a CLUSTER, then just get the controls within it, and do them one by one:
If the control is an ARRAY, then it's a bit more trouble. Since there is no way to get a reference to an arbitrary reference, you have to use the one reference to an element that you have. To use that, you have to be showing one and only one element. So this code does that, while restoring the original view:
HOWEVER
The ENUM, as I said earler, converts to VARIANT just fine, but TDMS will not accept it.
Here's one technique that works, sort of:
By converting the enum to an I32, you get the numeric value of its current setting, and can put that into a variant.
When reading, the numeric value will set the ENUM control correctly, so that's good.
The trouble is, in DIAdem, the property will look like:
Whatever_Engine_Cycle 2
While that is true, and will read back OK, it doesn't tell the user what kind of cycle it was.
The point of ENUMS was to put names on specific numbers.
I tried converting the variant to a STRING instead of an I32, but error 1057 (IIRC) happens: "cannot convert variant to specified type".
A similar issue exists for TEXT RING controls - although they will go through the process without error, what shows up is a number.
Any ideas for making DIAdem display the ENUM's string value, instead of a number?
The error says "not recognized by this version of LabVIEW" - is LV2015 any smarter about this?
Blog for (mostly LabVIEW) programmers: Tips And Tricks
Solved! Go to Solution.
04-02-2016 12:50 PM - edited 04-02-2016 12:53 PM
Nice clear post! let's try for a elegant solution. I think you are half-way there!
You know from the Class ID its an enum so cast the generic to enum
This may not work with type-defs I'll leave that to you. Rings and text rings can be handled simillar
Edit: Obviously cast string to variant
04-02-2016 01:03 PM
I take what I said about rings They will be tougher since you will also need to find the display format to wire to the Variant to Data function. (Yeah, Rings can be floating point so an I32 ins't always right)
04-02-2016 02:59 PM - edited 04-02-2016 03:02 PM
You know from the Class ID its an enum so cast the generic to enum
DUH ! ( slaps head )
I danced around with that earlier, but I was thinking that the strings wouldn't survive a downcast from ENUM ref to CONTROL ref.
Of course, that's not the case - It's just the REFERENCE that changes. If you convert it back to generic ENUM, then you can treat it like a generic ENUM.
Thanks, Jeff !
For completeness, here is my ENCODING code (enum ---> property):
On the other side, we do something similar:
Works like a charm.
Thanks again, Jeff!
Blog for (mostly LabVIEW) programmers: Tips And Tricks
04-02-2016 03:08 PM
For the record, the VALUE variant of an enum includes the possible strings, as evidenced in the original post above.
If you FLATTEN that into a STRING, you also get a DATA TYPE output (array of U16).
If you case THAT into [U8}, and then into STRING, you can see that all the possible strings are in there.
The DATA STRING out of the unflatten encodes the current value.
Still, using that would involve parsing out the TYPE STRING stuff, probably not a good plan. Unless that's documented by NI somewhere.
Blog for (mostly LabVIEW) programmers: Tips And Tricks
04-02-2016 03:34 PM
Just as an asside
This might help too
The hidden gems variant sub-pallate may have a few more tricks for you Steve.
04-02-2016 04:14 PM
Another tip:
Instead of casting to an ENUM, or to a RING, cast to a NAMED NUMERIC, which is a parent class of those two.
Then the same code can handle both ENUM and RING.
Blog for (mostly LabVIEW) programmers: Tips And Tricks
01-14-2020 09:32 AM - edited 01-14-2020 09:33 AM
Old thread I know, but I have a need to read and write Enums to TDMS Properties and was surprised to see that wasn't supported so I made an Idea Exchange post asking for native support. Over there I gave a basic example on how this can be done by setting/getting the string value. Go vote for it if you want this feature implemented.
Unofficial Forum Rules and Guidelines
Get going with G! - LabVIEW Wiki.
16 Part Blog on Automotive CAN bus. - Hooovahh - LabVIEW Overlord