Knowledge Base / Rockwell In-chassis / CompactLogix

How do I convert an integer to floating point and vice versa in a Rockwell Controller?

Search KB

Categories

The database addresses of all ProSoft modules and gateways consist of 16 bit integers.  Therefore when dealing with 32 bit floating point data, or REAL values, they are stored as two consecutive 16 bit integers.
 

Converting from Integers into Floats

In all versions of RSLogix, the COP (COPy) function can be used to convert between integer and floating point data.  The syntax is as follows:

COP <SOURCE> <DESTINATION> <LENGTH>

Please keep in mind that the LENgth parameter is for the destination tag or array of tags.

In this example, a float value of '12.34' will be represented as '28836’ and ‘16709' in the ProSoft module.  You will need to create a `COP` statement that copies both 16-bit Integer values into a single Real tag. 

create_float.jpg

MCM.DATA.ReadData[x] is a (16-bit) INTEGER data type.

Specifically, MCM.DATA.ReadData[0] will contain the value 28836 and MCM.DATA.ReadData[1] will contain 16709.  FloatData[0] is a “REAL” data type.

It is the datatype of the destination tag specified by the COP statement that dictates the Length parameter.  A Length of '1' means the logic will use 2 consecutive integers starting at MCM.DATA.ReadData[0] to create a single floating point, or “REAL” data tag.

Not all pairs of integer values will comprise a valid float, so it is possible that the resulting float value is not valid.  There may be an issue with the order in which the bytes and/or words were placed when converting to a float. You may need to try a different ‘Swap Code’ parameter for each MCM Command that is reading a float via Modbus.  You can swap the two words, the bytes within the words, or both.

Check the user manual of your specific module for more information on swap codes.

Converting from Floats into Integers

When sending data out of the ProSoft module or gateway, the Float value will have been stored in the memory as two 16 bit integers, rather than a single float.  The float value will be sent out as two 16-bit integers.  Once the two integers arrive to the destination, it is the device on the other end that will be responsible for displaying these two 16-bit integers as a single 32-bit floating point number.

COP Block

The COP statement here will take the one floating point value "SendFloat[0]" and place the data in the MCM.DATA.WriteData[0] and MCM.DATA.WriteData[1] tags for example, since a length of 2 was specified in the COP statement.

 

Please see http://en.wikipedia.org/wiki/IEEE_754 for details of the IEEE 754 float format.