4 byte Hex conversion to single Float

For general discussion related FlowStone
Post Reply
clamprod
Posts: 7
Joined: Mon Aug 02, 2010 3:33 pm

4 byte Hex conversion to single Float

Post 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!
Attachments
ComPorts.jpg
ComPorts.jpg (65.13 KiB) Viewed 17959 times
Tronic
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Re: 4 byte Hex conversion to single Float

Post 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?
clamprod
Posts: 7
Joined: Mon Aug 02, 2010 3:33 pm

Re: 4 byte Hex conversion to single Float

Post 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
Tronic
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Re: 4 byte Hex conversion to single Float

Post 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?
User avatar
jjs
Posts: 142
Joined: Thu Jun 09, 2011 12:15 pm

Re: 4 byte Hex conversion to single Float

Post 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?
Tronic
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Re: 4 byte Hex conversion to single Float

Post 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?
User avatar
jjs
Posts: 142
Joined: Thu Jun 09, 2011 12:15 pm

Re: 4 byte Hex conversion to single Float

Post by jjs »

it can be but what is measured?
Ambient temperature or a component/motor/coil etc.

and it is fahrenheit i believe
clamprod
Posts: 7
Joined: Mon Aug 02, 2010 3:33 pm

Re: 4 byte Hex conversion to single Float

Post 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.'
Post Reply