Siemens String to Int conversion

DLMUK

Member
Join Date
Jun 2013
Location
Southampton
Posts
311
hi,

I am trying to convert a string value to int but it is not working and I cannot figure out why.

I have a string which is constantly changing.

-012.33
-012.55
-012.69
-013.11

and so on...

I have placed this into an S_CONV block but the block returns an Int value of 4098 and this value never changes even when the string value changes.

I have my S_CONV in my OB1 just for the time being to test.

Screen shot:

https://www.dropbox.com/s/hu0dyxa8c9py5c3/conv.png?dl=0

Thanks
 
My guess is that it has something to do with the fact that the string is actually a REAL, not an INT. If you'll notice, the S_Conv block isn't highlighted with the green, it is dimmed out. This means the ENO of the block is false, which is usually done to indicate that there was an error/fault state. In this case, you have an invalid INT, so it returns the error.

In the help text for the block, it talks about setting the BR bit to 0 if certain errors occur. ENO being false, and the block being highlighted with dashed blue instead of thick green is the result. If you were to put a coil at the end of the network, it would evaluate false.

Have you tried using the S_Conv to REAL? If you really need the value as an INT, you can just convert it after.
 
My guess is that it has something to do with the fact that the string is actually a REAL, not an INT. If you'll notice, the S_Conv block isn't highlighted with the green, it is dimmed out. This means the ENO of the block is false, which is usually done to indicate that there was an error/fault state. In this case, you have an invalid INT, so it returns the error.

In the help text for the block, it talks about setting the BR bit to 0 if certain errors occur. ENO being false, and the block being highlighted with dashed blue instead of thick green is the result. If you were to put a coil at the end of the network, it would evaluate false.

Have you tried using the S_Conv to REAL? If you really need the value as an INT, you can just convert it after.

You are right - stupid mistake!

I have change it to string to real and now with a string of

+012.5

I get a conversion to real of

001C_8400

Which makes no sense!?
 
I've never actually tried to use this block before, and after reading the help file closer, I see something I hadn't noticed before:

Conversion of a character string to a tag in REAL format. The string must have the following format: ±v.nnnnnnnE±xx

± = sign
v = 1 digit before the decimal point
n = 7 digits after the decimal point
x = 2 exponential digits

If the length of the string is less than 14, or if it is not structured as shown above, no conversion takes place and the binary result (BR) bit of the status word is set to "0". If the result of the conversion is outside the REAL range, the result is limited to the corresponding value and the binary result (BR) bit of the status word is set to "0".

My guess is the S_Conv block is still returning a negative result, and it can't convert it because it requires a very specific format. I can't believe I never noticed that before; it is incredibly dumb to be that specific.

You might need to massage the numbers you are getting to make them fit the format. I'm not really sure how, though.
 
This is a tough block to use.

First, by the string data you posted you need to do a string to real conversion.

As MK42 indicated, the string must in a fixed format.
The attached graphic shows the format of a successful string to real conversion. Also make sure the LEN value of the string is at 14.

S_Conv.png
 
Don't know if it will apply for you, I only use S7-1200, but there is another conversion command STRG_VAL. I seem to remember I got frustrated with S_CONV but had a lot more success with STRG_VAL.
 
Don't know if it will apply for you, I only use S7-1200, but there is another conversion command STRG_VAL. I seem to remember I got frustrated with S_CONV but had a lot more success with STRG_VAL.

Thats what threw me for a loop above in the thread. I'd been using STRG_VAL lately, and was used to it being pretty flexible about formatting. It never occurred for me to check if the old block was the same.

Unfortunately, I think STRG_VAL is only available in the newer generation processors (1200/1500).
 
I usually convert in STL 'by hand', so I am not familiar with the blocks you mention.

But what if you convert
the numbers before the point as an integer iAAA and
the numbers after the point as iBBB.
Then convert iAAA to real rAAA,
and iBBB to rBBB.
Multiply rBBB with 0.01 and add rAAA.
If the sign is '-' multiply with -1.0.
Finished.

Kalle
 

Similar Topics

Goodmorning everyone, I'm sorry for my english. I need to convert an integer to a string with Step 7, unfortunately I have no experience about...
Replies
2
Views
5,500
Does anyone know why my one string is displaying this way? It is causing issues with my HMI displaying it.
Replies
4
Views
237
Hi I have a vision camera that I’m getting the string data P908765 from the vision on an advance trigger .which im using a S -move. Then I put...
Replies
1
Views
364
Hi to all, I have a quick ask. Is it possible to use a String as a PLC Tag? At input of FB I want to put a String. Lets say the value will be...
Replies
11
Views
2,208
Hello everyone, I have made a sample project with one FC, and one global DB with a few strings. In the global DB all strings are written...
Replies
8
Views
1,911
Back
Top Bottom