G_DeMark

Dint to Real

8 posts in this topic

Hello All,

Simple question I think.  What is the best way to take a DINT vale and convert it to a Real (floating point).  Here is what I did.  What do you think? is there a better way?  Thanks very much.

In this application I am obtaining a resistance value from a meter via Modbus RTU (using a ProSoft module) and wanting to display the final value on the PV 1000.

 

Rungs.jpg

Share this post


Link to post
Share on other sites

Can't you just divide the DINT by 100.0 and have the Destination as a REAL ? 

Share this post


Link to post
Share on other sites

The MOV instruction in Allen-Bradley controllers automatically converts between data types.
 

MOV
Source: MBTCP.DATA.ReadData[1]
Dest:   My_Resistance_Real

The DIV instruction also automatically converts between DINT and REAL.   I think in this case the divisor can be either 10 (DINT) or 10.0 (REAL).

Neither of these changes how single-precision floating-point values are stored and represented, or rounds to a specific number of decimal places.   For that, yes, a String method is sometimes required.

Share this post


Link to post
Share on other sites

I believe the proper solution is the DIV.  Of course, that requires one to have previously MUL by the same number of digits on the other side.

Edited by pcmccartney1

Share this post


Link to post
Share on other sites

DINT to REAL is easy; just about any math instruction will handle that.

REAL to DINT is an issue because you will get rounding or truncating errors. But there are only two "rules":

  1.  If source A & B are not REALs, then the fractional portion of the result truncates.
  2.  If source A or B are REALs, then the fractional portion of the result is rounded. But how it rounds seems to be random. For instance, if the result is -2.5, it rounds up to -2. If the result is 2.5, it rounds down to 2.

Share this post


Link to post
Share on other sites

ElectronGuru - I jsut read this in an old Modicon Concept Help File and it may apply here also.  "Rounding when done always rounds towards 0."  So 2.5 rounds to 2 and -2.5 to -2.  But 2.51 rounds to 3 and -2.51 rounds to -3.

Share this post


Link to post
Share on other sites
35 minutes ago, BobLfoot said:

ElectronGuru - I just read this in an old Modicon Concept Help File and it may apply here also.  "Rounding when done always rounds towards 0."  So 2.5 rounds to 2 and -2.5 to -2.  But 2.51 rounds to 3 and -2.51 rounds to -3.

so both 3 and -3 are further from zero than 2 or -2 so i would not consider Modicon Concept Help as a good resource to describe this.

 

Share this post


Link to post
Share on other sites

Round towards 0, round away from 0, round up, round to even...each can cause unexpected results for the x.5 case so you have to know what your platform does and act accordingly. The Logix 5000 PLCs I've used rounded to even; I don't remember what the 5s or 500s did. Or the S7-300s.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now