LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

converting Ascii byte array into 32-bit number

I have an instrument that sends the information in a 4-byte array that represents a 32-bit floating point number in little endian notation.  How do I join them into a single number?

0 Kudos
Message 1 of 9
(4,629 Views)

Use the Unflatten From String.  It has an input for the Endianness, make sure that is set to Little Endian.


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 2 of 9
(4,623 Views)

Thank you, but here is where I get lost.  The data from the instrument is sent in 5 bytes in ASCII and I use the string subset to drop the first letter, the remaining 4 characters represent a 32-bit floating point number in little endian.  It looks like I then need to convert the ASCII to binary before input into the unflatten from string.  Suggestions?

0 Kudos
Message 3 of 9
(4,540 Views)

@loman247 wrote:

It looks like I then need to convert the ASCII to binary before input into the unflatten from string.  Suggestions?


You don't need to do any conversion. Wire the 4-character string to the string input of Unflatten from String.

 

Unless your instrument is actually sending the values as text, but then the maximum value would be 9999 which would fit in 16-bits and endianness would be irrelevant, so that's probably not what's happening.

0 Kudos
Message 4 of 9
(4,531 Views)

You got the instrument rresponse from a VISA Read, I suppose, which returns string data, and you said you'd parsed off an initial character.

 

Unflatten From String takes a LabVIEW string datatype as input.  You also need to give it a typing wire so it knows what type the output needed is.  You'll want to wire a floating-point single-precision constant (SGL), value irrelevant, to do this.  MAKE SURE you set the constant's datatype correctly.

 

Apart from that, follow the earlier advice - wire in the other constants for little-endian byte ordering, and a false constant for "data includes... size".  You really should take the error out teminal too, and do some conditional processing based on error status!

 

Dave

David Boyd
Sr. Test Engineer
Abbott Labs
(lapsed) Certified LabVIEW Developer
0 Kudos
Message 5 of 9
(4,525 Views)

Thanks again for your assistance. I have attached a shot of my VI, should be seeing something closer to 0...hoping I am not embarrassing myself :/.unflatten2.png

0 Kudos
Message 6 of 9
(4,487 Views)

If you want to unflatten to a single-precision value, then why are you unflattening to an int? Change the numeric representation of the Type input to a SGL (right-click the numeric constant), and get rid of the conversion that follows.

 

Also, for byte order, it's easier to use the built-in enumeration than a numeric constant (delete the "2", right-click, create a constant).

 

The "to single-precision" conversion converts the numeric value, NOT the bits, which is why this is failing. If you start with an integer with the value of 5, you'll get a single-precision value of 5, not a single-precision value that has the same bits as the integer. What you're doing here is taking your bits, converting them to an integer, then asking for a single-precision representation of that number - where what you should be doing is taking those bits and interpreting them directly as a single-precision value.

0 Kudos
Message 7 of 9
(4,481 Views)

Like nathand said.  And I said it too... in all caps.  MAKE SURE you set the constant's datatype to SGL.  Again, the value is irrelevant, it's just a typing wire.

David Boyd
Sr. Test Engineer
Abbott Labs
(lapsed) Certified LabVIEW Developer
0 Kudos
Message 8 of 9
(4,474 Views)

Let's see, if I work backwards from your broken example...

 

I square your displayed value;

convert it to a U32   -guessing here, but that's what a dropped constant will assume unless you right-click and change representation [or type a fractional value in]);

flatten the U32 back to string (little-endian);

unflatten the string back to SGL (again, LE);

and take the sqrt() of that, I get 2.56584... is that a value you'd expect?

 

Dave

David Boyd
Sr. Test Engineer
Abbott Labs
(lapsed) Certified LabVIEW Developer
0 Kudos
Message 9 of 9
(4,469 Views)