Solved

HART Protocol Floating point handling

Posted on 2010-09-21
12
1,434 Views
Last Modified: 2013-11-17
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
Comment
Question by:farcuri
  • 6
  • 5
12 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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

by:farcuri
Comment Utility
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

by:jkr
Comment Utility
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

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> 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

by:farcuri
Comment Utility
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

by:jkr
Comment Utility
Just the same way
float f;;

unsigned int TXData; // read four bytes from device



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



printf("Value read is %f\n", f);

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:farcuri
Comment Utility
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

by:jkr
Comment Utility
You need the string 0x40 80 CC CD? Or just the binary representation?
0
 

Author Comment

by:farcuri
Comment Utility
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

by:
jkr earned 500 total points
Comment Utility
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);

Open in new window

0
 

Author Comment

by:farcuri
Comment Utility
Ok,
Thanks,
I will try it.
0
 

Author Closing Comment

by:farcuri
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now