Chavak

String to Real

12 posts in this topic

Hi,

 

I am receiving the following string from a serial port - +0153.8 +026.81 +0000.6 +0000.9     Air$R    .

I use 'Mid' instruction to separate the bold digits out of the string. Now I need to convert the separated value to Real format. How should I go about doing it?

Thank You

Regards

Share this post


Link to post
Share on other sites

I just discovered this awesome instruction. I tried converting the DWORD to REAL but it did not handle the Minus (-) value properly.

MrPLC_STRINGtoREAL.thumb.jpg.5aa931638dc

Share this post


Link to post
Share on other sites

There are commands in the CPU to convert a string to REAL or LREAL.  

STRING_TO_REAL

STRING_TO_LREAL

Share this post


Link to post
Share on other sites
Just now, Crossbow said:

STRING_TO_REAL

Wow... much easier! I overlooked the Data Type Conversion menu in the Toolbox.

I seem to have this knack for doing things the hard way.

Share this post


Link to post
Share on other sites

Thank you both of you, I'll try it out.

 

Regards

Share this post


Link to post
Share on other sites

BTW, how to know the various functions available in the CPU. It doesn't reflect in the toolbox, right?

I did look for available instructions in the toolbox

Share this post


Link to post
Share on other sites

It does. I was looking in the "Conversion" menu. The STRING_TO_REAL is in the "Data Type Conversion" menu. Also, sometimes I'll take a guess at an instruction name and begin typing it. You will get a drop down box with suggestions. Of course, some get overlooked just as I did.

No one can be expected to know them all. This is why we browse these forums and share our experiences.

Share this post


Link to post
Share on other sites

Found a search box right below of the toolbox, - very handy to search for Functions, keyed in String and all the string related functions appeared:-)

Share this post


Link to post
Share on other sites

All commands are in the toolbox.  No exceptions.

Share this post


Link to post
Share on other sites

I also have this issue. working on Omron's CJ2M.

String received can be +4.24737E-03 or +2.17737E+01

Somehow, the structure text function does not have STRING_TO_REAL.

 

Share this post


Link to post
Share on other sites
7 hours ago, nicepal said:

I also have this issue. working on Omron's CJ2M.

String received can be +4.24737E-03 or +2.17737E+01

Somehow, the structure text function does not have STRING_TO_REAL.

 

This is an NJ / Sysmac Studio thread, so the instructions are not the same  However, there is a String to Float instruction in the CJ2M, it is FVAL.

Share this post


Link to post
Share on other sites

Thanks. Just realized this is under NJ after I posted.:)

Seems the FVAL is in the Ladder format. Under Structure Text, don't have direct.

Anyway, I have work around this by extracting the Integer part to a DINT -> Real, the Decimal part to another DINT -> Real -> Convert into Decimal value. Add them together. Then check if it is E+ or E-, and Multiply or Divide according to the E number.

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