Sign in to follow this  
Followers 0
PLC_NEWBIE

Convert ASCII to REAL

4 posts in this topic

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Thanks for the ideas, will give them all a try. Geoff

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
Sign in to follow this  
Followers 0