11-07-2008 09:23 AM
Hi All,
I've got a problem that i can only tell you a little about due to the fact that i've signed a non-disclosure agreement which prevents me from telling you all the information that i have. I know my way around LV reasonably well but have no experience of programming C or C++. I have a data capture device that saves files in a properitary binary format.
I've got partially there - i have a Read From Binary File.vi working to read in a string. I then typecast this to U8 and hey presto i get out most of the data that i am looking for. The problem is that there is a mix of ints, chars, and others and i don't have access to that data (which is what i want).
The manufacturer has given me a bit of code that explains the file format, but the problem is that i cannot quite understand it (nor can i post it). However, it goes something like this:
#define blah blah
struct blah blah
{
union
{
unsigned char blah;
struct
{
unsigned char blah :1;
unsigned char blah :3;
unsigned char blah :2;
} blah;
}blah;
unsigned char blah;
double blah;
int blah;
int blah;
};
Blah stand for various variables.
Could someone have a stab at explaining what is going on. I am guessing that if i use some sort of cluster (=struct??) then i might be able to get the info that i need.
Thanks.
Phil
11-07-2008 09:58 AM - edited 11-07-2008 10:00 AM
Easy part out of the way first: You do not need to read the file into a string and then typecast to a U8. You can simply read as a byte array by just doing this:

OK. Next part. What's probably stumbling you is the use of struct and union. Yes, a struct is basically a cluster. A structure is an ordered set of items. Each member is located in memory one after the other (I'm ignoring memory alignment here for the sake of illustration). A union, on the other hand describes an object in which all of the members start at the same memory location. However, only one member is actually present in terms of value. The amount of storage is the size of the largest member. In the example you provided what's being described is the following (again, ignoring memory alignment considerations here for the sake of illustration):
"something" that is 3 bytes long (since the struct member is longer than the single unsigned char)
a 1 byte unsigned char
a double (8 bytes - most likely)
an int (4 bytes - most likely)
an int (4 bytes - most likely)
The "something" is the union, and it can be represent either a single 1 byte value (with the other 2 bytes being meaningless), or a structure of 3 separate 1 byte values. Which one it is depends on the variable values that were written to file, so you need to know something about what was written.
Now I said that I was ignoring memory alignment issues for illustration. Well, you can't.
This just means that the amount of memory used will be larger than a simple addition of the size of each datatype. Let's take a simple example:
struct y {
char a;
short b;
char c;
}
How many bytes is y? The direct addition would tell you 4 (1 + 2 + 1). However, the more likely answer is 6 since memory alignment consideration dictate that you need to start on an "even" memory address. A C compiler will tell you the correct answer because this is platform dependent.
So what does all this mean to you? Well, my suggestion is that you write a wrapper DLL to convert the file since you will be able to cast the data you read from file directly into a variable of the type of the overall struct. Trying to deal with this in LabVIEW is probably not worth the effort.
Caveat: I'm no C expert, so I hope that I didn't say anything that was outrageously wrong. If I did, someone please correct me!