Floating point to integer representation

PLBoudette

Member
Join Date
Jan 2006
Location
Albany, NY
Posts
41
I have a ML1400 Series B controller which is integrated with Wonderware via Modbus TCP. I have a flow totalizer in the ML as a floating point value which I'm trying to send via Modbus. The ML manual says only B (bits) & N (integers) files are accessed via Modbus. I'm wondering if there is an easy way to be able to convert this large floating point number as some type of integer representation for Modbus. I thought about setting up the totalizer in Wonderware but the system communicates over the internet which occasionally will dropout and thus, would compromise the totalizer value. I think the better idea is coding it in the ML which is backed up by a UPS so it will always hold the totalizer value even if the internet fails. I also thought of just copying the floating point number to 32 bit files for transmission and then break those down in Wonderware but it seems like a big hassle. Was wondering if anyone knew of any easier way? Maybe I'm missing an easier solution. Thanks in advance.
 
Use the COP instruction. COP makes a bit for bit copy of the IEEE754 floating point number bit pattern and stores it in two 16 bit integers which can be transmitted via modbus.

For example:
Take the float 35.67. The bit pattern for this float is 01000010000011101010111000010100

If that float is stored at F8:1 then

COP #F8:1 #N7:0 2 will put 0100001000001110 in N7:0 and 1010111000010100 in N7:1. The two integers now contain the exact bit pattern for the IEEE 754 float number and can be sent over modbus. If you were to look at the values of the integers you would see N7:0 = 16910 and N7:1 = -20972.

Reverse the instruction
COP #N7:0 #F8:1 1 to put the bit patterns of the two integers back into a float.

Note that the length on a COP instruction is the destination length; when copying a 32 bit float to two integers the length is 2. When copying two 16 bit bit patterns into a 32 bit float the length is 1.

Some devices will reverse the bit pattern. In that case you need to swap the values in N7:0 and N7:1, and sometimes you may need to swap the high and low bytes of the words.

A tool at http://babbage.cs.qc.edu/IEEE-754.old/Decimal.html can be used for you to decipher to bit pattern of the float.

Search the forum for IEEE-754 and for modbus for other programming examples - since this is a common topic you should be able to find many examples to help you.
 
Last edited:
I tried that solution but it produced an error. Then I tried copying at the bit level from the float value to integer files and it again gave me an error. Finally I settled on using the long integer function of the PLC, which I completely forgot because I almost never use that. Just moved the float to a long integer register then had to reverse the bytes and set up the Wonderware input to read a long integer and we were good to go. I also set up a reset bit for the totalizer at the end of the long integer range at ~2,147,000,000. But with the typical flow rate this system experiences, that should be about 8 years from now so not that big of a deal.
 
I tried that solution but it produced an error. Then I tried copying at the bit level from the float value to integer files and it again gave me an error.

What was the error? Can you post the program that gave you the error?
 
Can you skip using the float all together?

You will have float resolution problems long before you reach the limits of your long integer. While floats can represent very large numbers they cannot do it very well. This is a problem that affects all computers, not just PLCs. For example a float can no longer be incremented by 1 when it reaches 16.7 million (224-1). You may be better served by just using the long. You can use an implied decimal on the long, for example 1 count = 1/10 gallon so a value of 102 means 10.2 gallons and still have better precision than you would have with a float. Floats also have problems with fraction resolution. We've covered the reasons behind this in other threads so search for IEEE 754 float.
 
Attached are screenshots of the two errors I received doing both a copy from float to integer and float to bits. Thanks for the heads up for the float resolution issue. I will have to talk with the customer and explore a little further if those resolution issues could be a problem. You might be right, long integer might be the way to go here.

operand error.jpg bit error.jpg
 
This is one of those PITA differences between the Micrologix and a real SLC.

I saw something very similar with a ML1500 trying to use the same logic that works just fine in a 5/04.

Note that in your 2nd screen shot, the B3 address is specified to the bit level, and to work it would have to be addressed to the word level...which still won't work in a Micrologix...

What I ended up doing in my 1500 was to add a Long integer and used that for my purposes.

If you change the controller type to a SLC 5/03 (or 5/04 or 5/05) it will probably verify. I know this does you no good, just helps to illustrate this difference among these controllers.
 
I just tried CPW on a running ML1400. It works both direction, copying two integers to a float and copying a float to two integers.

There is one difference, the CPW length is specified in words and not destination elements, so the length has to be set at 2 for both directions.

I don't know why Rockwell felt the need to cripple the COP instruction on the ML to keep you from mixing operands of different types and then add the CPW to make up for it. o_O
 
Last edited:

Similar Topics

Hi eveyone. I need transfer signal from system 1 to DCS via modbus. System 1 only can send 32 bit floating point. DCS receive 16 bit integer. How...
Replies
20
Views
10,548
Does anyone know of any such PLCs? Usually I'm lazy and don't want to think about precision and overflows.
Replies
20
Views
6,963
I have noticed in one of our PLC5 programs there is a copy instruction that copies a integer into a floating file. The integer value being copied...
Replies
17
Views
9,134
In RS500 can I multiply a Floating point by 10 to get rid of the decimal? Ex: Source A(11.436) x Source B(10) = N7:0(114), or will I still get a...
Replies
2
Views
1,674
Hello, I'm working with an AD DL05, integer math only. I've got a C-more micro for an HMI. I need to input inch dimensions for a cut to...
Replies
14
Views
19,093
Back
Top Bottom