Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

Convert FP2 CSi to Float


volcanelec Mar 21, 2022 11:09 PM

Hi,

I'm sending data from the serial port of my cr1000 to a lora node.

So, i must use serialOutblock to send data in binary format FP2.

I can't send data in 4 bytes (IEEE) because it's to large for the bandwith off  LoraWan communications.

The receiver need   to convert datas into float for storage them in database .

How can i convert FP2 CSI format to float ?

I have just this information 1 sign bit , 2 bit for exponent and 13 bit for mentissa from campbell user manuel.

I try the formula from IEEE 754  but no good result!!

Best regards


jtrauntvein Mar 23, 2022 03:02 PM

The FP2 format uses base 10 instead of base 2.  Here is a sample of my code that performs this conversion:

 

typedef unsigned char byte;
typedef unsigned short uint2;

float csiFs2ToFloat(void const *buff_)
{
   // we are going to cast the binary value into a two byte integer so that it is convenient to pick
   // out patterns and pick off parts of the structure.
   byte const *buff = static_cast(buff_);
   uint2 fs_word = (uint2(buff[0]) << 8) + buff[1];

   // we can now pick off the components of the FS2 structure
   static uint2 const pos_infinity = 0x1fff;
   static uint2 const neg_infinity = 0x9fff;
   static uint2 const not_a_number = 0x9ffe;
   bool is_negative = ((fs_word & 0x8000) != 0);
   uint2 mantissa = fs_word & 0x1FFF;
   uint2 exponent = (fs_word & 0x6000) >> 13;
   double rtn;

   if(fs_word == pos_infinity)
      rtn = std::numeric_limits::infinity();
   else if(fs_word == neg_infinity)
      rtn = -1.0f * std::numeric_limits::infinity();
   else if(fs_word == not_a_number)
      rtn = std::numeric_limits::quiet_NaN();
   else
   {
      rtn = static_cast(mantissa);

      for(uint2 i = 0; mantissa != 0 && i < exponent; ++i)
         rtn /= 10.0f;
      if(is_negative && mantissa != 0)
         rtn *= -1.0f;
   }
   return static_cast(rtn);
} // csiFs2ToFloat

 

Note that this code is in C++.


sophiaabigail Mar 26, 2022 11:21 AM

This post is under review.


JDavis Mar 28, 2022 06:47 PM

The FP2 binary format is described in the CR1000 manual. It is on page 585.

https://s.campbellsci.com/documents/us/manuals/cr1000.pdf 

Log in or register to post/reply in the forum.