Home » Categories » Rockwell In-chassis » CompactLogix

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

The database addresses of each ProSoft card consist of 16 bit integers.  Therefore when dealing with 32 bit floating point or real values, they are stored as two consecutive 16bit integers.

Converting from Integers into Floats

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

COP  <SOURCE> <DESTINATION> <LENGTH>

Please keep in mind that the LENgth parameter is for the destination table or array.

In this example, a float value of '12.34' will be represented as '28836 and 16709' in the ProSoft card.  You will need to create a COP statement that copies both Integer values into a single Real tag/table. 
MCM.DATA.ReadData[x] is a (16bit) Int 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 data type of the Destination of 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 1 float.

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

Check the user manual for more information on swap codes.

Converting from Floats into Integers

When sending data out of the ProSoft card, the Float value will need to be copied into the ProSoft's memory (16 bit integer addresses).  The float value will be broken up into 2 integers, and need to be sent out as 2 integers.  Once the two integers arrive to its destination, the device on the other end will be responsible for displaying these 32 bits as a floating point number. 
 

The COP statement here will take the 1 floating point value "SendFloat[0]" and place the data in the.
MCM.DATA.WriteData[0] and [1] tags, 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.

Custom Fields
    Attachments (0) Attachments
    There are no attachments for this article.
    Comments Comments
    There are no comments for this article. Be the first to post a comment.