Step 7 SCL real number rounding

CharlesM

Member
Join Date
Aug 2005
Location
Arkansas
Posts
1,129
I am logging real numbers into arrays in SCL. I am using these for buffer bit trends in WinCC Flexible. I need to cut some digits out. Some of the numbers are like 3.0124987 and I need to trim them down to 3.0125 The only way I see to do it is to multiply by 1000 round convert to DINT then divide by 1000. Is there another command like TRUNC or ROUND where you can specify the number of digits after?
 
No. You may have to build your own function, such as how you describe already.(*)
Is the trimming because the significant digits have to fit into an INT ? In that case there may be other ways.

*: if you worry about exceeding the cycle time because you have to loop through a lot of data, it may be slightly more efficient to avoid an external function and just write it directly in SCL:

rValTrimmed := DINT_TO_REAL(REAL_TO_DINT(rVal * 1000.0)) / 1000.0 ;

edit: Code typo.
 
Last edited:
In stl it will be much simpler.

OPN DB999

L #Current_position
SLW 3
LAR1 P#DBX 0.0
+AR1

L #data_real
RND
T DBW [AR1,P#0.0]

L #Current_position
+ 2
T #Current_position
 
Is the trimming because the significant digits have to fit into an INT ?
It just makes the trends a little hard to read. As you move the slider back and forth the number of digits change. It will show 3.0 then 3.000012345 Its just not very clean looking.
 
Did that rounding trick work for you?

I tried it on our controller. The problem is that not all numbers can be presented exactly with 3 decimal places. Most of the time it worked but some times it didn't. I tried mulitplying by 0.001 instead of dividing by 1000 but it made no difference.
 
I worked on it for about 45 minutes this morning without any luck. I could round to 1 but could not get 1.0000 I kept getting 1.00000234 or something similar. I may work on it some more tomorrow if I have time.

Its not a big problem because it only does this with my trends. Everything else on the HMI I can specify the number of digits.
 
If you don't mind hacking something together, there are some tricks you could try.

You could convert the value to a string and pass it to the HMI that way after formatting it the way you need it.

Or, maybe you could split the whole number from the decimal portion, convert the decimal portion to an int, truncate it, then pass it to the HMI as two tags and knit them together.

But, this is exactly why I will never use a Siemens HMI. It is suitable most of the time, but then you run into stuff like this that should be a no brainer. I mean, I can't set the number of display digits of a float? No one on the WinCC development team thought that would be useful?
 
I have quickly put this together and tested it:
Code:
	  L	 MD	20
	  L	 1.000000e+003
	  *R	
	  RND   
	  T	 MD	24
	  L	 MD	24
	  DTR   
	  L	 1.000000e+003
	  /R	
	  T	 MD	28
It works OK, on PLCSIM that is.
MD20=1.012345, MD24=1012, MD28=1.01200
But I remember there was a recent thread where the problem was that S7 made strange rounding of REAL numbers.
Dont remember which thread. But I think it was on this forum :)
 
Charles,

I havent looked at WinCC Flex lately, can you not just tell the tag to use 999.9 for example?

I know in WinCC Ive used this. I need to look at the trends in Flex though.
 
JRW said:
Charles,

I havent looked at WinCC Flex lately, can you not just tell the tag to use 999.9 for example?

I know in WinCC Ive used this. I need to look at the trends in Flex though.

I'm wrong Charles, I see 6 decimal places on a real tag too. If I use an INT, its a whole number.
 
This has to be done in the PLC. These are being stored in an array of 500. I am using WinCC Flexible buffer bit trends to pull these arrays up into a trend view. With the change of a bit it will load up 8 arrays of 500. There is no formating for the data in the buffer bit trends. If I was just reading one of the values into flexible I could format it any way I want.
 
I am going to investigate this today

I can see why the PLC will have a problem with this. There is not indication that the PLC has any guard bits. Our products have either 8 guard bits or the 32 bit numbers get converted to 64 and then stored back as 32 bit numbers. The only thing I can suspect right now is that it is impossible to represent some numbers in a 32 bit format or even 64 bit numbers. I tried the SCL ( ST ) code above. I didn't get the desired results when trying to truncate 23064/1000 or 23071/1000 to 3 decimal places. Most of the time the code above works but I tried many numbers.
 
yes, but you still get 6 decimal places on a real number (in a Wincc Flex Trend), I thought that is what you were trying to fix.
There is no way in Flex to display..say 2 decimal places in a trend.

Again, I may be missing what you are trying to see in WinCC Flex
 
Here is what it looks like on the trend. I know I can't format it in WinCC Flexible thats why I say its a PLC problem. Its not a big problem because before I had the real numbers I just had ints without a decimal point. I can go back to that if needed because I could * 1000 and round. Still I don't like it when thinks don't work like they should.
trend.GIF
 
I had a little time this week to get back to this rounding problem. What I had looked like it should work but it didn't. So I put up a question on the Siemens Fourm

http://www.automation.siemens.com/W...ow.aspx?PageIndex=1&PostID=125594&Language=en

After a little work I found out that what I had was working in Step 7 however the resulting trend in WinCC Flexible was not allowing it to be shown correctly. I don't know that I can do anything about that but I thought it was a good idea to put my findings here so they might help someone else.

Thanks for all the help.
 

Similar Topics

Hi, Can someone translate this for me please, or at least help to explain what the instructions mean? SET SAVE =...
Replies
17
Views
4,626
Hello, what is wrong with the program like below to write 100 to DB81.DW24? %DB81.DW(IDX :=24) := 100; Or how should I write the program...
Replies
16
Views
5,077
I have made a small FB in SCL, in this blok i have to transfer to an array in a certan position, but I can't get it to work. It goes as follows...
Replies
6
Views
6,545
Help!! From reading previous threads, I know that the best way to index an array is using SCL. I have the SCL editor, I'm just not sure where to...
Replies
3
Views
4,250
Hi guys, i was trying to make a program in simatic step 7 with which i could with the useing of a switch choose between automatic, manual and step...
Replies
0
Views
4,644
Back
Top Bottom