Solved

HART Protocol Floating point handling

Posted on 2010-09-21
1,434 Views
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
0
Question by:farcuri
• 6
• 5

LVL 86

Expert Comment

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 Comment

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

LVL 86

Expert Comment

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

LVL 53

Expert Comment

>> 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 Comment

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

LVL 86

Expert Comment

Just the same way
``````float f;;

unsigned int TXData; // read four bytes from device

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

``````
0

Author Comment

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

LVL 86

Expert Comment

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

Author Comment

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

LVL 86

Accepted Solution

jkr earned 500 total points
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

Author Comment

Ok,
Thanks,
I will try it.
0

Author Closing Comment

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

Featured Post

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationâ€¦
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a â€¦
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.