LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to make binary file with header

Hello !

I want to save acquisition data to binary file using Write to I16.vi .
I also want to add some header information in the beginning of the file.
There are good examples doing this to spreadsheet file but no for binary
files ?
I can build up (concatenate strings) the header and find the size of the
header but
then I have no idea how to put the header into the file. Should I use the
Write to I16.vi twice ?
one for header and secong for the data ? And how to reserve the space
(bytes) in the file for the header ?
And so on the other hand how to read such a file so that I can get the
header information into different indicators and the data (2D) into a graph.
Maybe there are good examples available ?

Hmmm, anyway hints availabl
e ?
0 Kudos
Message 1 of 8
(6,235 Views)
check out the "Cont Acq to File (binary).vi" in the DAQ/analogin/strmdisk.llb example. There is a read example there as well
Stu
0 Kudos
Message 2 of 8
(6,235 Views)
The basic thing to remember is that all files are binary. The only difference with the ones we arbitrarily define as "text" files is that the binary data in them was stored using a coding mechanism (called ASCII) that is readable by human beings.

You can certainly call Write to I16 twice, just be sure that the second call appends to the file, otherwise the data will overwrite the header you just created.

Reading the result gets a little trickier. You have to know ahead of time how any characters will be in the header. If this value isn't constant, make the size of the header (in bytes) the first value stored in the file. Assuming a non-constant header length and a 16-bit header length, you could create the file as follows:

1. Generate the header and count the number of bytes in it. If the header is an array of I16 values, the byte count is the number of elements in the array times 2.

2. Add an element to the beginning of the header array containing the header count.

3. Write the header to the file (using Write to I16.vi if you wish).

4. Write the data to the file with the write routine set to append the data to the file.

With this file structure, your read would proceed as follows:

1. Read the first two bytes of the file and convert to a U16. This is your header count.

2. Starting at the marker returned at the end of Step 1, read the number of bytes returned in Step 1. This is your header. Decode as necessary to convert the data into the form the rest of your program needs.

3. Starting at the marker returned at the end of Step 2, read the rest of the file and convert to an array of I16 values. This is your data. Decode as necessary to convert it into the form the rest of your program needs.

This same basic structure will work regardless of whether the data portion of the file is a 1D or 2D array, and regardless of the actual structure of the header data.

Hope this helps...

Mike...

PS: Right now I am without access to a development system, but I should have one again sometime next week. If you can't get this working give a shout and I'll put something together for you as soon as I'm up and running again...

Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 3 of 8
(6,235 Views)
Hi,

there is "String to Byte array" function that will help you. Take your header string, and transform it to Byte array. Add to end of array 0, it will be sign of end of header. Now you can write it to bynary file by Write to I16.vi. When you read this file, know that header will be until you read 0.
Example of vi attached.
Hope that will helps.
0 Kudos
Message 4 of 8
(6,235 Views)
That is certainly another approach, the only problem is that it complicates the code (you have to explicitly search for the end-of header) and has potential sideffects (i.e. what happens if one or more values in the header can be zero--or whatever other value you pick to denote the end-of-header?).

As long as you are adding a value to the header anyway, why not make it the number of bytes in the header so you know exactly how much you need to read. No searches, no worries about data content.

Mike...

Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 5 of 8
(6,235 Views)
1. First 0 will be in header only at the end, because it consist only ASCII codes.
2. I used 0 because if you want read file in text editor (notepad) you will not see it. But if you add number of bytes iat begining of header it will be seen as some symbol in editor.
0 Kudos
Message 6 of 8
(6,235 Views)
In the original post it was said that the entire file (including the header) will be binary. In other words, no ASCII codes.

Mike...

Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 7 of 8
(6,235 Views)
I know Mike.
All that i wanted say, that header in original was simple text. It means that can't be byte 0, because each byte in header is translation of some ASCII code.
0 Kudos
Message 8 of 8
(6,235 Views)