LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

What is the "catch" in writing Variants to a Binary file?

I'm saving "event" data associated with an experiment that LabVIEW is "controlling".  The data includes such things as the State of several State Machines, Messages to Queued Message Handlers, digital data from hardware (such as Button Pushes), etc.  My "Event Type" is a cluster with three components -- Event Time (an I32 representing ticks of a 1KHz clock), Event ID (an Enum with values such as State, Message, Digital In), and Event Data, a Variant that is the State Enum or Message String or Digital U8 port value.

 

I wrote a test routine in which I generated some known data of Event Type, wrote it to disk using Binary Writes, then read it back in to an Array of Events doing a "Read to End-of-File" Binary Read.

 

Somewhat to my surprise, the array of Events looked exactly correct, including the Variant data.

 

What is the "catch" in storing data this way?  I can see one obvious problem -- if I change the Event ID Enum, I could mis-identify the Events.  Are there things I need to worry about in handling the Variant data?

 

[I'm now thinking about hare-brained schemes to save the Event ID data in the file, itself, perhaps as a header of some sort.  And before you ask, I'm not quite ready to think about using TDMS ...]

0 Kudos
Message 1 of 7
(2,825 Views)

the main considerations would be that you are able to parse the data when you load that (but you have already pointed that out) but also there is file size overhead to consider as the variant datatype contains information about the data as well as the data itself

David
www.controlsoftwaresolutions.com
0 Kudos
Message 2 of 7
(2,810 Views)

@Bob_Schor wrote:

...And before you ask, I'm not quite ready to think about using TDMS ...]


Out of curiosity, why not?

 

A major catch, as you already hinted, is that the Read fails if anything about the variant data structure changes.

0 Kudos
Message 3 of 7
(2,769 Views)

The only real catch I can think of for the Variant data is if NI decides to change how to save the data type.  I remember people running into problems saving and loading variant data saved to a file when working with multiple LabVIEW versions.  I think it was concluded that NI changed the binary representation of the data type.

 

As far as your clusters, if you change them at all you will not be able to recover old files.

 

The enum I will have to play with.  I think those are just saved as a number based on the representation you give it (a U16 will be 2 bytes).  So as long as you don't change the represenation, I don't think you will have problems with the enums as long as you only add items to the end.  Deleting and/or adding in the middle could mess things up.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 4 of 7
(2,745 Views)

@crossrulz wrote:

<snip>

The enum I will have to play with...  

<mini-snip>

Deleting and/or adding in the middle could mess things up.


Oh, yeah, I got a little burned with that one.  Good thing it was just something I was pracicing on.  Because changing stuff in the middle actually changes the values that the text is associated with, it can throw off all the the selections throughout your project!  I was dismayed to find all the typedef'd enum constants were changed to reflect the new text associated with the values.  For instance, if the value for "Init" was "1" and "Exit" was "2" and I added "Test" in between, "Test" would become "2" and "Exit" would move to "3" and all the contstants that had "Exit" selected before now showed "Test" as the selection!

 

So if you have to add something, add it to the end, no matter how unintuitive it makes the enum.  If you decide to delete something from the middle, don't delete it; replace it with something like <not used> to keep the values aligned with the text.  It may look funky on the enum, but it will save you lots of heartache.

 

Is there a better way to do this?

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 5 of 7
(2,719 Views)

In my experience, my "burn rate" on adding (or removing) Enums "in the middle" is maybe 25%.  Many times it just "works", shifting as appropriate, but trust me, I do check everywhere that Enum is used (thank goodness for Find All Instances").

0 Kudos
Message 6 of 7
(2,709 Views)

@Bob_Schor wrote:

In my experience, my "burn rate" on adding (or removing) Enums "in the middle" is maybe 25%.  Many times it just "works", shifting as appropriate, but trust me, I do check everywhere that Enum is used (thank goodness for Find All Instances").


Whew - 25% burn rate is about 25% too much for me.  😉

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 7 of 7
(2,706 Views)