pack and unpack ?

Discussion in 'Perl Misc' started by Steve, Jan 9, 2005.

  1. Steve

    Steve Guest

    Sorry to be thick, but I have googled this and ended-up more confused
    than ever. So I hope you don't mind if I come back on this question.

    I am reading a series of bytes from a com port. Having looked at the
    data and the way it changes, I *believe* that some or all of the bytes

    123 101 60 117

    represent a signed floating-point with the value of approximately +24
    (+/- 1)

    Could someone kindly explain how I get to the value from the raw
    bytes?

    Thanks
    Steve, Jan 9, 2005
    #1
    1. Advertising

  2. Steve

    Michael Fuhr Guest

    (Steve) writes:

    > I am reading a series of bytes from a com port. Having looked at the
    > data and the way it changes, I *believe* that some or all of the bytes
    >
    > 123 101 60 117
    >
    > represent a signed floating-point with the value of approximately +24
    > (+/- 1)


    Why do you believe that? What evidence leads you to that conclusion?

    > Could someone kindly explain how I get to the value from the raw
    > bytes?


    Find out for certain what kind of data is represented, how it's
    represented, and that you're looking at the right portion of the
    data. Beware of looking for a solution before you fully understand
    the problem.

    Here's one way to create a string of those four bytes and then
    unpack that string as a floating-point number:

    my $s = pack("C*", 123, 101, 60, 117); # assumes the numbers are decimal
    my $f = unpack("f", $s);

    On my i386 machine this yields 2.38821e+32; on my sparc it yields
    1.19026e+36 (differences due to endianness). If the bytes do indeed
    represent a floating-point number around 24 then this method of
    unpacking is wrong.

    --
    Michael Fuhr
    http://www.fuhr.org/~mfuhr/
    Michael Fuhr, Jan 9, 2005
    #2
    1. Advertising

  3. Steve

    Guest

    Thanks, that helps and I will play further along those lines. I
    suspect that 2.38821e+32 is very close to 24 if I ignore some high end
    bits.

    >What evidence leads you to that conclusion?


    I am reading temperature data from a device which receives data
    wirelessly from many transmitters and then sends it via serial to the
    PC.

    I see a series of characters every time the device receives input from
    a sensor, including the identity of the device in plain text and the
    time in a series of decomals, so I know I have the comm protocol
    right. If I change the temp, only those 4 columns of numbers change, I
    know the temperature at the probe, I know the devices own software
    shows the temp to 3 decimal places and has a range of -50 to +150 C.

    Of course there could be some unique coding in here, but they are very
    profligate with data, (sending some 30 characters of text), so I doubt
    that they will have bothered to pack it in a cunning way.

    The obvious question is 'what does the manufacturer say', the answer is
    they don't respond to questions!
    , Jan 10, 2005
    #3
  4. Steve

    Michael Fuhr Guest

    writes:

    > I see a series of characters every time the device receives input from
    > a sensor, including the identity of the device in plain text and the
    > time in a series of decomals, so I know I have the comm protocol
    > right. If I change the temp, only those 4 columns of numbers change, I
    > know the temperature at the probe, I know the devices own software
    > shows the temp to 3 decimal places and has a range of -50 to +150 C.


    Could you post several samples, showing the data you receive and
    the corresponding temperature at the probe? If the numbers do
    indeed represent the temperature then maybe we can find the pattern.

    --
    Michael Fuhr
    http://www.fuhr.org/~mfuhr/
    Michael Fuhr, Jan 10, 2005
    #4
  5. Steve

    Guest

    OK, here is the data I see. The numbers are all generated by
    ord(character)

    Note that 2 temeratures are being sent, an internal and an external
    (probe) temp.
    All degrees Centigrade. All were taken when the system was stable
    EXCEPT the two
    marked as approx.

    internal 27.33 external 26.330
    corresponding data looks like this:
    3 10 80 72 113 88 126 12 72 123 109 53 117

    internal 24* approx external 0
    3 10 80 72 113 88 127 12 72 123 47 61 117

    internal 16.5* approx external 0
    3 10 80 72 113 89 127 12 72 123 47 86 117

    internal 27.33 external -24
    3 10 80 72 113 88 127 12 72 123 14 53 117

    internal 27.33 external -50
    3 10 80 72 113 88 127 12 72 123 4 53 117




    Thanks
    , Jan 10, 2005
    #5
  6. Steve

    Guest

    Here's more. I should have done this sytematically before.
    It may be relevant that the scale of the external reading is -50 to
    +100
    and of the internal reading -30 to +50.


    6.5 l 0
    3 10 80 72 113 90 127 12 72 123 47 121 117

    5.25 0
    3 10 80 72 113 91 127 12 72 123 47 125 117

    21.67 50
    3 10 80 72 113 88 126 12 72 123 33 68 117

    27.33 88
    3 10 80 72 113 88 127 12 72 123 74 53 117
    27.67 96
    3 10 80 72 113 88 127 12 72 123 78 52 117
    , Jan 10, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. joe shoemaker

    Python pack and unpack question

    joe shoemaker, Jul 15, 2008, in forum: Python
    Replies:
    0
    Views:
    305
    joe shoemaker
    Jul 15, 2008
  2. Bill Atkins
    Replies:
    6
    Views:
    117
    Daniel Berger
    Jun 14, 2004
  3. Anton Hörnquist

    Array#pack and String#unpack query

    Anton Hörnquist, Sep 11, 2008, in forum: Ruby
    Replies:
    2
    Views:
    101
    Jan Dvorak
    Sep 11, 2008
  4. Tulan W. Hu

    pack and unpack question for perl 5.8.0

    Tulan W. Hu, Jul 28, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    99
    Tulan W. Hu
    Jul 28, 2003
  5. Kim Schulz

    pack and unpack integer data

    Kim Schulz, Nov 1, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    124
    Kim Schulz
    Nov 2, 2005
Loading...

Share This Page