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
8 posts
• Page 1 of 1
4 byte Hex conversion to single Float
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!
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 (65.13 KiB) Viewed 15276 times
- clamprod
- Posts: 7
- Joined: Mon Aug 02, 2010 3:33 pm
Re: 4 byte Hex conversion to single Float
this is the conversion from hex to float in ruby
but you should know how protocol sends data.
Any manual link of your Hardware sensor?
- 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
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
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
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?
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
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?
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?
-
jjs - Posts: 142
- Joined: Thu Jun 09, 2011 12:15 pm
Re: 4 byte Hex conversion to single Float
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
it can be but what is measured?
Ambient temperature or a component/motor/coil etc.
and it is fahrenheit i believe
Ambient temperature or a component/motor/coil etc.
and it is fahrenheit i believe
-
jjs - Posts: 142
- Joined: Thu Jun 09, 2011 12:15 pm
Re: 4 byte Hex conversion to single Float
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.'
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
8 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 53 guests