LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I read binary data?

Hello.

I have some binary data (it is a jpg) that is corrupted, but I know what I need to do to fix.  However, I am having trouble getting LabVIEW to cooperate.

If I read the data as text, I am able to process it to fix it the way I need to, but when I write it back to a file, LabVIEW adds a LF for every byte it thinks is a CR and a CR for every byte it thinks is a LF.  In other words, if a byte is 0x0d, it writes 0x0d 0x0a, and if a byte is 0x0a, it also writes 0x0d 0x0a.

This, of course, corrupts the jpeg data and makes it unusable.

So I thought I could just read the entire data and treat them as U8.  But when I try to use the "Read From Binary File" function, it apparently tries to find some sort of header with the size of the array, or something like that, and since the first few bytes of my file are 0x0, it returns an empty array.

Do anybody know of a way to make LabVIEW simply read a file and interpret the data as an array of U8 where the length of the array is simply the length of the file in bytes?

Thanks,

Alejandro
0 Kudos
Message 1 of 10
(7,614 Views)
Right-click on your text file IO functions and uncheck "convert EOL". See if this helps.
Message 2 of 10
(7,610 Views)
Hi.

Thanks a lot for your suggestion.  Simple, but effective.  It did work, and I was able to process the data correctly and get viewable images.

Just out of curiosity, though, is there a way to go the other route I was asking about?  Just making LabVIEW read in bytes and interpret them as an array of U8 (or pairs of bytes as U16, etc)?

Thanks,

Alejandro
0 Kudos
Message 3 of 10
(7,580 Views)
If you know what bytes to change and how to change them you should not have any trouble manipulating the file. I just read a .jpg file with the Read Text File function (LV 8.5), set a block of bytes (after what I guessed to be the header) to \00 using Replace Substring, and wrote the file to a new filename with Write Text File. When I opened the two .jpg files in an application which displays pictures, the modified file had blocks of various colors scattered all over the picture, but the underlying picture was still there. If you have a lot of manipulation to do converting the string to an array of U8 might be more convenient. The Text file functions deal with any kind of data just fine. It just gets interpreted as text at the input and output.

Lynn
0 Kudos
Message 4 of 10
(7,575 Views)
Hi Alejandro,
 

Just out of curiosity, though, is there a way to go the other route I was asking about?  Just making LabVIEW read in bytes and interpret them as an array of U8 (or pairs of bytes as U16, etc)?
 
Have you tried using the "Read JPEG File" and "Write JPEG File" functions. "Read JPEG File" returns an array of U8 for image data.
0 Kudos
Message 5 of 10
(7,552 Views)
Mike,

Use some caution with the Read and Write JPEG VIs. Due to the compression used for JPEG, reading the file, connecting the image data to the write, and writing the file results in two different files. The example I tried read a file of 320 KB and wrote a file of 252 KB.

Lynn
Message 6 of 10
(7,541 Views)
Thanks Lynn. Alejandro, what were you wiring to the "count" input of the "Read From Binary File" function? When you wire a -1 to "count" and a U8 to "data type", do you still receive an empty array?
0 Kudos
Message 7 of 10
(7,502 Views)
Hi.

Yeah, I have seen that phenomenom that Lynn describes where data written back to a jpeg file is re-compressed, and can result in a different bit stream alltogether.

Mike: I was trying it without anything connected to the "count" input.  However, I just tried it now with a -1 connected to the count input, and it results in a broken wire (the wire is not broken if you leave the input disconnected).  This is shown in the attached picture.

Were you able to read the binary data as an array of U8s?

Thanks

Alejandro
0 Kudos
Message 8 of 10
(7,485 Views)
Hi Alejandro,
 
You're getting a broken wire there because you are wiring an array of U8 as the data type input. An array should not be wired to the data type. The data type input corresponds to the data type of a single element (U8 in this case). When specifying the data type as an array of U8 and leaving count unwired (default is 1), you are asking for one data element of array U8. From the LabVIEW Help, "If count calls for an array of elements and the specified data type is an array, the function automatically returns a cluster of arrays or cluster array because LabVIEW does not allow arrays of arrays." Try giving this a shot. I hope that helps!
 


Message Edited by lion-o on 12-17-2007 12:03 PM
Message 9 of 10
(7,470 Views)
Hello, Mike.

Thanks for the tip!  It did work this time.  Although, I have already been able to do what I wanted to do by reading it as a string, it is good to know about this option, and how to get LV to interpret data as numbers.

Thanks a lot.

Alejandro
0 Kudos
Message 10 of 10
(7,449 Views)