Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright

4 byte Hex conversion to single Float

For general discussion related FlowStone

4 byte Hex conversion to single Float

Postby clamprod » Tue Jul 30, 2013 5:14 am

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

Re: 4 byte Hex conversion to single Float

Postby Tronic » Tue Jul 30, 2013 7:11 am

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

Re: 4 byte Hex conversion to single Float

Postby clamprod » Tue Jul 30, 2013 3:18 pm

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

Re: 4 byte Hex conversion to single Float

Postby Tronic » Tue Jul 30, 2013 6:52 pm

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

Re: 4 byte Hex conversion to single Float

Postby jjs » Sun Aug 04, 2013 10:35 am

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

Re: 4 byte Hex conversion to single Float

Postby Tronic » Sun Aug 04, 2013 4:35 pm

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

Re: 4 byte Hex conversion to single Float

Postby jjs » Sun Aug 04, 2013 4:51 pm

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

and it is fahrenheit i believe
User avatar
jjs
 
Posts: 142
Joined: Thu Jun 09, 2011 12:15 pm

Re: 4 byte Hex conversion to single Float

Postby clamprod » Mon Oct 21, 2013 1:15 am

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


Return to General

Who is online

Users browsing this forum: No registered users and 70 guests