Help - Search - Members - Calendar
Full Version: Convert ASCII to REAL
Forums.MrPLC.com > PLCs and Supporting Devices > Siemens
PLC_NEWBIE
I am writing an STL function to read data from an external device on ethernet using AG_RECV.

I have the read working however the received data is all ascii which I now need to convert to a bunch of reals. The return data looks like this...

100.001
101.2
5.32
etc

(Notice the varying lengths of the data - there is a CR at the end of each value)


Can anyone tell me the best way to convert an array of bytes containg ascii values into a real in S7-300 STL ?

Thanks
Geoff
JesperMP
That is why controls should be written for computers and not for humans.
The format that we easily understand, contains many interpretation rules that are not so easy for computers to understand.

There is the STRNG_R function in the standard library. But you cannot use it, because it demands the string is formatted as
±v.nnnnnnnE±xx
where
± = Sign
v = 1 digit before the decimal point
n = 7 digits after the decimal point
x = 2 exponential digits

If no one else has an idea, then I think there are two possible ways:

Here is one way:
There is the function FIND FC11 in the standard library. You can use that to search for the "."
Use it to split into two strings for integer part and fractional part.
To get the number of zeroes after the decimal point maybe you have to use FIND again. For the fractional part you need to know how many digits there are including the zeroes after the decimal point.
Then it is a question of using STR_I on the integer part and the fractional part, and then generate a REAL like this:
realVal := INT_TO_REAL(integerPart) + INT_TO_REAL(FractionalPart)/(10^Number_of_digits)

Here is another way:
Use FIND again to split into an integer and fractional part.
Then figure out how big the integer part is. Then reformat the string to adhere to STRNG_R, moving the decimal point and calculate the exponent.
Use STRNG_R to convert into a REAL
GixRamones
Hi.
Time ago I used standar Fc.
From library\IEC
fc38 from string to 16 bit.
fc37 from string to 32 bit.
fc39 from string real 32 bit.
bye
PLC_NEWBIE
Thanks for the ideas, will give them all a try.

Geoff
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.