AlexPLC

Converting REAL to STRING, with defined precision

5 posts in this topic

Hi gurus,

I am running Studio 5000 and am building a comma delimited data string to send out via ASCII with the 5069-SERIAL card (CompactLogix).

I am more familiar with Siemens on this, and there is a function called VAL_STRG in which you can convert a real to a string and define the precision or how many decimal places it will have (attached).

I am unable to find something similar and am scratching my head at how to write something robust enough to accommodate a real number that can have one or two decimal places and convert it to a string that will HOLD that precision throughout the range of the number. I guess for a number with one decimal place I could multiply by 100, convert to STRING, and then insert the decimal place, but it sounds like maybe overkill....? Currently if I bring the real even to a user-defined STRING of say 5 characters, the decimal "moves" as the number grows or shrinks.

Any help or hint in the right direction would be greatly appreciated.

 

Siemens_Val_String.PNG

AB_RTOS_too_many_decimal_places.PNG

Share this post


Link to post
Share on other sites

Assuming that you always want the decimal in the same place, like you're displaying money for example.  I believe the following would work.

RTOS RealValue AsciiStringValue

FIND AsciiStringValue "." 1 Find_Pos

COP AsciiStringValue AsciiDisplayValue 1

ADD Find_Pos 2 AsciiDisplayValue.Len

Not as concise or elegant as the siemens but might just work.

Share this post


Link to post
Share on other sites

okay so with the FIND, I see how this can help me keep track of the decimal as I change from X.X, XX.X, XXX.X..., but from the third and fourth line is where I am still confused

This is what I have

ab_logic.PNG.fd92d0fe27b84147c466b21863f

There is something that I am not understanding about the COP, at this point for a number of X.X, all I want to copy is the X.X and throw away all of the other characters in the decimal, but the length of the "AsciiStringValue" (stringstest[11]) is still 10.

 

For the fourth statement, what exactly is going on with adding the position of the decimal to the length of the Ascii Display Value?

Really appreciate the help so far...

 

ab_logic.PNG

Share this post


Link to post
Share on other sites

The COP Instruction length tells you the number of strings to copy.  in your case if stringtest[13].LEN evaluates to 10 you've copied [11] to [13] , [12] to [14] , [13] to [15] all the way up to and including  [20] to [22].  

The length in your COP instruction should be one (1).  You're copying One String.

The Add instruction takes the position of the decimal and adds 2 places to it and makes it the length of stringtest[13].

 

 

StringtestExample.PNG

1 person likes this

Share this post


Link to post
Share on other sites

ahhhhhhhhhh ok! I am liking this now, so essentially the "Source B" integer is acting like a "precision parameter". This is definitely going to work for me.

I think I'll package this logic into a block and re-use them on all the real values I have and then use INSERT and CONCAT to put the commas in and at the end will go the EOL characters.

This was the key I needed to make this work. BobLfoot, Thank you so much for taking the time to explain this!

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