Step 7 Convert REAL to INT

Hal9000

Member
Join Date
Jun 2010
Location
London
Posts
140
Hi !

I want to convert a Real value (say 94.2 PSI) into two separate Integers so that +94 goes into MW210 and +2 goes into MW212. My Modbus slave device will only accept this format.

This may sound simple, but I'm finding it difficult to get the result I want!

Any advice would be warmly welcomed.

Cheers.
 
Only one decimal after the comma ?

i.e. "12.34" becomes "12" & 3" and not "12" & "34"

If so,


Code:
L "rNumber"
TRUNC
T "iRealpart"
 
L "iRealpart"
ITD
DTR
L "rNumber"
TAK
-R
L 10.0
*R
RND
T "iFractionpart"
 
Hi,

well done JesperMP, you can erase the instruction:
L "iRealpart" after T "iRealpart"

Code:
L "rNumber"
TRUNC
T "iRealpart"
ITD
DTR
L "rNumber"
TAK
-R
L 10.0
*R
RND
T "iFractionpart"
 
I've sliced some more instructions out:

Code:
      L     #rNumber
      TRUNC 
      T     #iRealPart
      DTR   
      -R    
      L     1.000000e+001
      *R    
      RND   
      T     #iFractionPart
 
Arrrh.. don't touch me code.

You sliced too much, methinks.

edit: If you absolutely must superoptimise everything, then be sure that it works:
Code:
      L     "rNumber"
      TRUNC 
      T     "iRealPart"
      DTR   
      L     "rNumber"
      -R    
      L     -1.000000e+001
      *R    
      RND   
      T     "iFractionPart"

N.B.: It only works for positive numbers !
 
Last edited:
JesperMP,

Your first example of code worked fine. Your second example kept on returning -1 in the iFractionPart.

Thanks for your help!

Kind regards,

Hal9000
 
You mean the code from post #6 ?
I tested it with PLCSIM and it worked fine by me. Except for that fractionpart would be negative, if the real number was negative.

Try to test this exactly as it is.
Code:
      L     MD    10                    //"rNumber"
      TRUNC 
      T     MW    14                    //"iRealPart"
      DTR   
      L     MD    10                    // "rNumber"
      -R    
      L     -1.000000e+001
      *R    
      RND   
      T     MW    16                    // "iFractionPart"
 
I see what you mean.
The .99 part of 101.99 rounds up to 1.0.
And the greatest fraction number that can be represented by the special scheme is .9

It will have to be solved like this.

Code:
      L     MD    10                    //"rNumber"
      TRUNC 
      T     MW    14                    //"iRealPart"
      DTR   
      L     MD    10                    // "rNumber"
      -R    
      L     -1.000000e+001
      *R    
      RND   
      T     MW    16                    // "iFractionPart"

      L     MW    16
      L     9
      >I    
      JNB   _001

      L     0
      T     MW    16
      L     MW    14
      L     1
      +I    
      T     MW    14
_001: NOP   0
 
Good stuff.... interested in how it could be done with a s7-1200 and no stl.....
 
Alternative, should work for all real numbers limited to +-3276.7 (pretty high (diff)pressure).

L MD 10;
L 1.000000e+001;
*R ;
RND ; // Evt RND+
L 10;
/I ; // The remainder is in accu1-h
T MD 20; // MW20 = Dec, MW22 = Int

// And if you don't want to see negative decimals
A M 20.7;
JCN DIT;
L MW 20;
NEGI ;
T MW 20;
DIT: CLR ;

Good night:sleep:
Kalle
 
Thanks eveyone for all your help!!

I now have another puzzle to solve...Converting back the other way. i.e. 2x INT's to a Real.

Assuming

DB143 DBW80 is the Integer
and
DB143 DBW82 is the fraction part

I need the result to show the result (as a real) to 1dp only.

The problem is that some fraction parts contain 3 numbers and other 4 numbers. I must always use the value on the left side.

See DB143 below as a reference.

DB143.DBW80 INT 83 MOTOR POWER (INTEGER)
DB143.DBW82 INT 9764 MOTOR POWER (FRACTION)

The real result should equal 83.9


DB143.DBW84 INT 102 MOTOR TEMPERATURE (INTEGER)
DB143.DBW86 INT 883 MOTOR TEMPERATURE (FRACTION)

The real result should equal 102.8

I have over 200 values to map, ideally I need a function where I need to only add 3 x parameters to the FC. i.e. integer(in), Fraction(in) and real(out)
 

Similar Topics

Hi, How can I easy convetr Real temperature to integer in step 7 thanks BarP
Replies
1
Views
4,441
Helo I have a FB-Libary in Step 7 v5.4 i want to use in TIA-Portal V11 Sp2. How can i convert this?
Replies
5
Views
5,159
I am just beginning to learn PLC i have this ladder http://imageshack.us/f/233/ladderw.jpg/ i take me about 30sek to write it down and it...
Replies
6
Views
2,082
Hi All, I'm currently busy on a project where i must take a number of char type objects and convert them to a decimal number for comparison...
Replies
4
Views
6,781
I have a plc w/ a 24VDC pulse output and want to connect it to a stepper drive that takes +/- 5V Step+ and Step- inputs. In the manual it says...
Replies
3
Views
2,258
Back
Top Bottom