Page 1 of 1

4 byte Hex conversion to single Float

Posted: Tue Jul 30, 2013 5:14 am
by clamprod
OK, I'm really having trouble with this one; though I'd expect this to be simple, I'm finding little info on it.

I'm working through the ComPort to communicate with a YostEng sensor. The returned data must come back as a 4 byte Hex code and I need to convert it to single Float.

It would appear that I would .pack the data as Hex, then .unpack as single precision float, but the two do not seem to work together...pack is part of Array, and unpack is part of String?

For example, the following returns; The ASCII return shows 66.2 degreesF, the Hex return shows 0x428800


Any direction toward a proper conversion?

Thanks!

Re: 4 byte Hex conversion to single Float

Posted: Tue Jul 30, 2013 7:11 am
by Tronic
this is the conversion from hex to float in ruby

Code: Select all

hex_string = '42880000'
float = [hex_string.to_i(16)].pack('L').unpack('F')[0]
# => 68.0

float = 66.2
hex_string  = [float].pack('F').unpack('L')[0].to_s(16)
# => 42846666

but you should know how protocol sends data.
Any manual link of your Hardware sensor?

Re: 4 byte Hex conversion to single Float

Posted: Tue Jul 30, 2013 3:18 pm
by clamprod
Oh, Thanks, Tronic.

The sensor's web page is:
http://www.yeitechnology.com/productdis ... -down-case

The Sensor's manual is:
http://www.yeitechnology.com/sites/defa ... un2013.pdf

The sensor will actually be used in wireless mode (via the receiver dongle) vs. wired mode.

So, I believe what you'll be looking for is on page 24.

:-)
clamprod

Re: 4 byte Hex conversion to single Float

Posted: Tue Jul 30, 2013 6:52 pm
by Tronic
you are reading the temperature, you can try to run another command that returns a float value stable?
and see if it is always converted wrong?

Re: 4 byte Hex conversion to single Float

Posted: Sun Aug 04, 2013 10:35 am
by jjs
maybe this topic can help you alltough it is an other sensor reader.
viewtopic.php?f=72&t=471

are you reading out all measurements in one go or really fast after each other?

in the example above each measurement is read shortly after each other and therefore the multiplexer is switching according the type of measurement

start with one type of readout first and see what you get, so send one command at a time to your sensor and see what message is returned, i see that you now have a bunch of text wich makes it more difficult
also if you read a string blablabla"hexcode" like in your example then you could filter out to read the string at position x of the string for x positions and then convert them to decimal

i also see a 66.200000 readout in your example, what is it?

Re: 4 byte Hex conversion to single Float

Posted: Sun Aug 04, 2013 4:35 pm
by Tronic
jjs wrote:i also see a 66.200000 readout in your example, what is it?

is the response of request in ASCII mode ">,0,44\n"
temperature float 66.20000

the request in HEX is F72C2C
and the response is 42 88 00 00 in byte
but after converted in float it produce 68.0

perhaps the temperature was changed between the two requests?

Re: 4 byte Hex conversion to single Float

Posted: Sun Aug 04, 2013 4:51 pm
by jjs
it can be but what is measured?
Ambient temperature or a component/motor/coil etc.

and it is fahrenheit i believe

Re: 4 byte Hex conversion to single Float

Posted: Mon Oct 21, 2013 1:15 am
by clamprod
Thanks a lot for the help.

I've been out on the road for the past 10 weeks, am home for a couple days, then out again for another 3 weeks.

Then I want to get back into the solution for this.

Thanks again, 'I'll be back.'