Jump to content

Recommended Posts

Posted

Hi,

Is it possible to convert a number (a REAL or DINT in my case) to an ASCII string equivalent in CX-Programmer (I'm using a CP1H) ?

For example :

REAL = -12.3456 -> String (starting from W0 for instance) = [2D 31][32 2E][33 34][35 36]

                                                                                                 [- 1]     [2 .]      [3 4]    [5 6]

                                                                                                  [W0]    [W1]    [W2]    [W3]

For a DINT, the process would be the same but there would never be any decimal point (but there could be a minus sign AKA 0x2D in ASCII).

This operation is trivial in Sysmac Studio, but seems quite tedious in CX-Programmer.

Posted

Nothing native to do this no. The REAL datatype can be broken apart into the various aspects (sign bit, fraction, exponent) quite easily in an address-based PLC. If you could extract each of these and get them into a format that can be converted to a string and conc them all together you could accomplish this in theory....

Posted (edited)

@MechEngi

Photovoltaic is correct, there is no direct conversion. Don't know the end result you are looking for or how the results will be used, but here is a screenshot of the method i would use to approach what you are trying to do. I would clear the decimal with a multiplier and then convert the remaining digits, so they end up becoming ascii equivalents and then make the result a string.

You could go back and place your decimal "ascii" value back in the string as well as the negative symbol but again, cleanup depends on how you plan to use the results of the conversion.

I put all the address in the watch window so you can see what is going on with the conversions.

 

Convert.jpg

Edited by Str8jCkt
Posted
18 hours ago, Sergei Troizky said:

How about FSTR instruction?

Ding ding, we have a winner! Thank you very much!

It's weird that this instruction is not in the "Conversion" section of the instructions list, but rather in the "Floating-Point Math" section.

Posted
6 hours ago, MechEngi said:

Ding ding, we have a winner! Thank you very much!

It's weird that this instruction is not in the "Conversion" section of the instructions list, but rather in the "Floating-Point Math" section.

This threw me off as well..

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...