# HART Protocol Floating point handling

I am working with HART protocol to send data to a device over serial com port.  The HART protocol requires that floats use IEEE-754 format.  For example to send the float 4.025 to the device I have to convert it to IEEE-754 format which is 0x40 80 CC CD and send this to the device.
Receiving data is the reverse--the device sends a float to me such as 0x40 80 CC CD and I have to convert it to decimal 4.025.
Are there any suggestions as to how to convert both ways: 1. decimal to binary and 2. binary to decimal?
I use C++ Borland
###### Who is Participating?

x

Commented:
Ah, OK, so you need it as a string. Then try
``````#include <memory.h>

float f = 4.025;
unsigned char buf[4]; // four bytes
unsigned int i0,i1,i2,i3;

memcpy(buf,&f,sizeof(float));

i0 = (unsigned int) buf[0];
i1 = (unsigned int) buf[1];
i2 = (unsigned int) buf[2];
i3 = (unsigned int) buf[3];

sprintf(m_ucTxBuffer,"\xff\xff\xff\xff\xff\x%2.2x\x%2.2x\x%2.2x\x%2.2x\x%2.2x",i0,i1,i2,i3);
``````
0

Commented:
The C/C++ data type 'float' (or 'double' for double3 precision) is already what IEEE-754 describes, so no additional modifications are necessary. See also http://en.wikipedia.org/wiki/IEEE_754-1985
0

Author Commented:
Ok,
I think I follow what you are saying but could you clarify maybe with my example.  For instance, if I declare
float value= 4.025 and I want to send this to my HART device I need to put it in a 32 bit word let's call it TXData.  How would I do that?  TXData is binary where the sign is the highest bit.  The next eight bits are the exponent, and the mantissa is the next 23 bits.
Thanks,
Fabio
0

Commented:
Well, like
``````#include <memory.h>

float f = 4.025;
unsigned int TXData; // four bytes

memcpy(&TXData,&f,sizeof(float));

// alternatively, just use the address of 'f' to send four bytes
``````
0

Commented:
>> The C/C++ data type 'float' (or 'double' for double3 precision) is already what IEEE-754 describes

Just as a side note, although this is generally true, there are some implementations that don't conform to that floating point standard. C Implementations that do, define __STDC_IEC_559__, so you can check for that.

No points for this post, please. Just mentioning it for completeness' sake.
0

Author Commented:
Hi JKR,
I like your answer.  I am going to test it.  I have a follow-up.  How do I do the reverse? Convert binary back to decimal.  Would it be a memcpy?  I would appreciate an example to if you don't mind.
Thanks,
Fabio
0

Commented:
Just the same way
``````float f;;
unsigned int TXData; // read four bytes from device

memcpy(&f,&TXData,sizeof(float));

``````
0

Author Commented:
Hi JKR,
Thanks to your help I am getting very close to doing what I need to do, however I have hit a mental block.
What I need is to convert the float into the IEEE-754 format (and the method you described above does that perfectly) however I need to put the 4 byte hex value into an array of unsigned char.  Could you please redo your  float to binary example above to go from float 4.025 to unsigned char 0x40 80 CC CD?
Thanks,
Fabio
0

Commented:
You need the string 0x40 80 CC CD? Or just the binary representation?
0

Author Commented:
Maybe the code below can answer the question.  I can send the representation below to my HART device OK.  I'd like to emulate that.

static unsigned char    m_ucTxBuffer[255];
memcpy(&m_ucTxBuffer[0], "\xff\xff\xff\xff\xff\x02\x80\x00\x00\x82",10);
0

Author Commented:
Ok,
Thanks,
I will try it.
0

Author Commented:
Hi jkr,
I tested the decimal to IEEE754 HEX representation in my program and it worked!  You  get the points.
If you have a minute though could you explain what the line memcpy(buf,&f,sizeof(f)) does exactly? I also noticed that the order of the HEX bytes get reversed.  To represent them in the order MSB to LSB I need to load m_ucTxBuffer[0]= i3
m_ucTxBuffer[1]= i2
m_ucTxBuffer[2]= i1
m_ucTxBuffer[3]= i0
Could you briefly explain that too?  My next test is to do the IEEE-754 to decimal conversion.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.