CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2011
    Location
    Los Angeles, USA
    Posts
    5

    Reading floating point words

    I'm trying to read data from a spectrophotometer through RS-232. Manufacturer specs on communication: every 80 msec the following sequence generated

    1Ssimmmm 0mmmmmmm 0mmmmmmm 0mmmmmme 0eeeeeee

    S,s,i - some auxiliary bits; m - mantissa; e - exponent. MSB is to the left. Bit7 is set to 0, except for the first byte, to synchronize receiving. When the machine shows ~0.3534 on its little screen I read from the port (hex) 85 53 12 43 7F, which is, if I'm not mistaken,

    10000101 01010011 00010010 01000011 01111111

    Removing aux bits I get the following float point words
    01011010 01100100 10100001 11111111

    No matter how I try to convert this thing I do not get anything even close to 0.3. The format manufacturer describes is not quite IEEE 754 (MSB would be to the right). Can anybody help me to decipher what manufacturer meant?

    P.S. It's Knauer K2501 spectrophotometer, I contacted Knauer already, but not sure if they will get back to me.

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Reading floating point words

    I did not check your hex to bin or the decode... but the string that you put down gave me...
    0.353097916 E-1

    How have you been trying to convert it ????

    I used binary fractions.. (the method that fractions are stored in Binary)...

    first Byte..
    Code:
    Byte| Fraction | Final Decimal
    0   | 1/2      | 0
    1   | 1/4      | 0.25
    0   | 1/8      | 0
    1   | 1/16     | 0.0625
    1   | 1/32     | 0.03125
    0   | 1/64     | 0
    1   | 1/128    | 0.0078125
    0   | 1/256    | 0
    The first byte gives a value of 0.3515625, continue on with the other two and you get the ~ figure..

    Last byte ... Is a signed byte.. 00000000H = 0D , 01111111H = 127D BUT ..... 10000000H = -128D and 11111111H = -1D ...

    Easy ...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    Sep 2011
    Location
    Los Angeles, USA
    Posts
    5

    Unhappy Re: Reading floating point words

    Quote Originally Posted by GremlinSA View Post
    ]
    Last byte ... Is a signed byte.. 00000000H = 0D , 01111111H = 127D BUT ..... 10000000H = -128D and 11111111H = -1D ...
    Sorry, I'm new to all this thing, so I still don't get it.
    Do you mean that the exponent part has a designated sign bit?
    Which one is the sign bit for the fraction?
    In your conversion you are getting roughly 0.03, which is way better than anything I had, but still not 0.3 the machine showed...

    I was using this thing
    http://www.ajdesigner.com/fl_ieee_75...2_bit_word.php

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Reading floating point words

    Quote Originally Posted by artirm View Post
    Do you mean that the exponent part has a designated sign bit?
    Yes, that's how I interpret GeamlinSA's post. (I'm not sure, however, what you mean by "designated" here. It definitely does have a sign bit.

    Which one is the sign bit for the fraction?
    Perhaps one of the "auxiliary" bits we don't know anything about? There are two labeled "S" and "s" which could be good candidates. Or perhaps a sign bit wouldn't make sense at all in the context of the instrument the data comes from and so there is none?

    In your conversion you are getting roughly 0.03, which is way better than anything I had, but still not 0.3 the machine showed...
    Perhaps the value measured by your instrument "floats" a bit and your display readout and the binary data you got actually belong to different samples? Your display readout and GremlinSA's converted value just slightly over 0.5% apart.

    Nice tool to check a given bit pattern , for instance what you get out of your conversion routine (but then again, why not simply output it from your program?), but IMO for understanding the IEEE 754 floating point format this one is better: http://en.wikipedia.org/wiki/Single_...g-point_format

    I'm afraid you won't get away without some bit-juggling of your own, unless Knauer provide you a conversion routine (probably as source code, but perhaps not in the language you're using).
    Last edited by Eri523; September 15th, 2011 at 03:36 PM. Reason: Corrected my spec of the deviation between the two values
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Sep 2011
    Location
    Los Angeles, USA
    Posts
    5

    Re: Reading floating point words

    Perhaps one of the "auxiliary" bits we don't know anything about?
    S shows if the data belongs to wavelength scan; s indicates whether the data are from the initial moment of reading, i is set to 1 if the machine deems data invalid for some reason. Nothing about sign. The instrument measures observed light absorbance, which can be either positive or negative.

    01000011 11010011 11100010 00000001 is supposed to mean ~ +1.059
    10111111 11111000 00100101 00000000 is supposed to mean ~ -0.500

    btw, if they say exponent is the right most group of bits, then how I number bits in these words? Left most is number 0?
    Last edited by artirm; September 16th, 2011 at 12:59 AM.

  6. #6
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Reading floating point words

    Ahhh .. with a few more strings to work with ....

    I got it...

    the strings you posted work as follows..
    The mantisa (or significand) is signed, and the Exponent is signed. .. How ever the Exponent decides how you calculate the significand, and not applied afterwards..

    Exponent= 0 (Base line) (also third sample)
    0.0000000 00000000 00000000
    IE:..
    Code:
    00000000 00000000 00000000
     1111111 11111...ETC
    1/////// /////
     2481361 25124
        6242 51000
           8 62249
               486
    Exponent= -1 (first example)
    .00000000 00000000 00000000
    Code:
    00000000 00000000 00000000
    11111111 1111...ETC
    //////// ////
    24813612 5124
       62425 1000
          86 2249
              486
    Exponent= 1 (Second sample)
    00.000000 00000000 00000000
    Code:
    00000000 00000000 00000000
      111111 111111...ETC
    21////// //////
      248136 125124
         624 251000
             862249
                486
    Always remember that the first bit is also your signed bit...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  7. #7
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Reading floating point words

    Hmm.. forgot to add what i got from the last two strings...

    01000011 11010011 11100010 00000001 = 1.059807301
    10111111 11111000 00100101 00000000 = -0.50023973
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  8. #8
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Reading floating point words

    Because you've not really specified what language so i did a Excel Spreadsheet with the 3 samples you posted, that should calculate just about any binary you put into it... ( it works out the Exponent and all)

    Hope it helps you work out your own code to decode this...
    Attached Files Attached Files
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  9. #9
    Join Date
    Sep 2011
    Location
    Los Angeles, USA
    Posts
    5

    Smile Re: Reading floating point words

    Quote Originally Posted by GremlinSA View Post
    Hope it helps you work out your own code to decode this...
    Wow, it really did! Thanks a lot. I'm coding in C#, so it was not too difficult to write my own code based on your algorithm. Works like a charm and is so much better than reading data through analog output :-)
    So, it seems like both exponent and mantissa are biased, although I'm not sure if it is standard in IEEE 754. Where could I read more about these things?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured