LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Byte array to float

I would like to essentially convert the following python snippet to Labview.  It reads in a binary format and puts it into a mix of integer and floating point values.

 

I've spent half a day on this and keep getting out nonsense.

 

thanks

 

Eric

 

 

import sys
import struct

fn=sys.argv[1]
f=open(fn,'r')
ll=f.readlines()
f.close()

l=""
for l1 in ll:
l+=l1 
ll=""

 

i=0
psize = 21

print 'Converted from binary file', fn
print 'file size/particle size: ',len(l)/psize
print
print '{0:7}{1:7}{2:14}{3:9}{4:3}{5:9}{6:10}'.format("Col1","Col2","Col3","Col4","Col5","Col6","Col7")
print '--------------------------------------------------------'

while (i+1)*psize <=len(l):
try:
ty=struct.unpack('b',l[i*psize+0:i*psize+1])[0]
en=struct.unpack('f',l[i*psize+1:i*psize+5])[0]
x=struct.unpack('f',l[i*psize+5:i*psize+9])[0]
y=struct.unpack('f',l[i*psize+9:i*psize+13])[0]
px=struct.unpack('f',l[i*psize+13:i*psize+17])[0]
py=struct.unpack('f',l[i*psize+17:i*psize+21])[0]
i+=1
if (1):
print '{0:5g} {1:5g} {2:8.4f} {3:8.4f} {4:8.4f} {5:8.4f} {6:8.4f}'.format(i,ty,en,x,y,px,py)
except:
print "Error reading particle", i
break

0 Kudos
Message 1 of 10
(7,043 Views)

Sorry, I am not really familiar with Python, but if you could attach a small VI that contains a suitable typpical input string as well as the desired result, I could probably do it in a few minutes. 😄

0 Kudos
Message 2 of 10
(7,042 Views)

Try something like the following:

 

 

0 Kudos
Message 3 of 10
(7,029 Views)

Thanks for the suggestion.  I tried it, and I think it's close, but the floats I'm getting out don't match.  So per your request, I've attached the input and expected output along with the relevant header info. 

 

I should note, that with your code, I wasn't sure how to set the data types, so I set the blue one as I32, and the rest as SIG.

 

Anyway, thanks again for your time...this one has really stumped me.

 

Eric

 

(note, to be compatible with the forum, I had to add an acceptable extension for the binary, so I chose .tar)

 

Download All
0 Kudos
Message 4 of 10
(7,011 Views)

The blue one is I8 (I32 won't work) and the others SGL (look at the wire description I have included in the image). Also make sure the cluster order is correct. (I'll look at your attachmetns later...)

 

(I cannot read your tar file using 7zip. Maybe it is corrupt. Try to zip all your attachments instead. What is your OS?)

0 Kudos
Message 5 of 10
(7,002 Views)

I will try your suggestions.  As far  as the .tar is concerned, that file is actually the binary to be read, but NI wouldn't let me upload it with the native extension, so I chose .tar.  I tried to explain this in my last post, but perhaps I wasn't as clear as i could be. You can simply delete the .tar,

 

Eric

0 Kudos
Message 6 of 10
(6,993 Views)

Ah, sorry. I though you complressed the binary file as tar.

0 Kudos
Message 7 of 10
(6,989 Views)

OK. you can simply read it directly as that structure as follows (pythonParse2.vi).

 

 

 

If you have the string, my above suggestions works of course too (pythonparse.vi)

 

(VIs are in 8.2.)

Download All
0 Kudos
Message 8 of 10
(6,986 Views)

And here's how you could make a nice table. Modify as needed.

 

Download All
Message 9 of 10
(6,979 Views)

Thank you so much...this seems to have worked.  I tried everything today, except the cluster of elements...that's so simple, yet somehow non-obvious.  Again, thanks for helping me out with this.

 

Eric

0 Kudos
Message 10 of 10
(6,974 Views)