Data conversion

dsongra
2020-11-11
2020-11-19
  • dsongra - 2020-11-11

    Hello,

    I am working on IOT kit using Hilscher NetIOT Connect Gateway, I have P&F Ultrasonic sensor (range 4000mm), i am getting process data 14 bit in two bytes (attachment Received data). I want to convert it into 16bit uint starting 2 bit offset of first byte, bit read start from bit 2 to bit 14. So value should be in mm.

    Thanks in advance.

    Br,
    Devendra

     

    Last edit: dsongra 2020-11-11
    • nothinrandom - 2020-11-12

      Let me see if I understand what you're saying here. You have two bytes, lower and upper. For the low byte, you are interested in bits 2-7; everything for the high byte.

      Assuming %IB10 is your low byte and %IB11 is your high byte

      uiOutput : UINT;

      // to always ignore the last two bits of the low byte
      // using 16#FC for masking since it is 11111100
      uiOutput := SHL(TO_UINT(%IB11), 8) OR (%IB10 AND 16#FC);
      
      // to shift right the 2 least significant bits
      // i.e. 11111111 becomes 00111111
      uiOutput := SHL(TO_UINT(%IB11), 8) OR SHR(%IB10, 2);
      
       
  • dsongra - 2020-11-18

    Hi,

    Yes, you are correct.

    I have tried both logics as above, result same for both.
    But value is different. Sensor place at 20 cm/0.2 meter distance between an object, received value screenshot attached, result should be like 20 cm/0.2 meter accordingly to the attached screenshot value (Conversion).

    according to attached conversion info screenshot.

    Br,
    Devendra

     

    Last edit: dsongra 2020-11-18
    • nothinrandom - 2020-11-19

      @dsongra,

      Thanks for the Conversion info.png. This makes more sense and it is probably why you are not seeing the result. It looks like we would need to generate the uint from the byte pairs and then shift. Try this instead:

      // generate uint from byte pair
      uiOutput := SHL(TO_UINT(%IB10), 8) OR %IB11;
      // shift right by 2
      uiOutput := SHR(uiOutput, 2);
      

      We also swapped %IB10 (previously low byte) and %IB11 (previously high byte) data starts with most significant bit (MSB) from data sheet. Doing rough math, 3 (16#03) 40 (16#28) yields (808) 16#0328. We take this and shift right by 2 bits, or effectively divide by 4; thus resulting in 202. My first guess would be 20.2 cm or 202 mm?

       
      πŸ‘
      1
      • dsongra - 2020-11-19

        Hello,

        Thank you so much for your support. Now I am getting correct value. please find below screenshot.

        Br,
        Devendra

         

        Last edit: dsongra 2020-11-19
        • nothinrandom - 2020-11-19

          Glad it works for you. With this endian, shift right is divide by 2 and left is multiply by 2 for each bit (i.e. shift right by 3 is the same as dividing by 222). I noticed that you also need to divide by 10 to remove decimal, so a one liner looks something like this:

          // shift right 2 (/4) and /10
          uiOutput := (SHL(TO_UINT(%IB10), 8) OR %IB11) / 40;
          
           

Log in to post a comment.