Sign in to follow this  
Followers 0
Vihang

ascii to hex or decimal (floating point)

5 posts in this topic

Dear All I have a FX2N plc with a 232BD card. I have attached a mutimeter to get values from the same. Now I get "+1.9903E+3" . Please note that the decimal point is floating so I could even get "+123.45E+0". But the total length of the string is always 10. Now I want to convert this string in to either hexadecimal or decimal so that I can compare with the stored data. I tried using HEX and then the BIN command, but it doesnt work for ".". Need help Thanks Vihang P.S: The attachment RS232_Multimeter_Test.zip

Share this post


Link to post
Share on other sites
i don't have FX to try it out but if i'm not mistaken BIN is not going to help much. you need result to be floating point so you should try EBIN (or possibly DBIN but with flag M8023 set). alternatively you can always make a loop and parse the string with your own code, something like: ' extract value for i=2 to 7 if s[i]="." then dec_point=i else f=f*10+(s[i]-h30) endif next i ' adjust value based on decimal point location f=f*(10^(i-dec_point)) ' process exponent if s[9]="-" then i=s[10] else i=s[10]*(-1) endif ' apply exponent f=f*(10^i) ' apply sign if s[1] = "-" then f=f*(-1) where: s = string f = floating point result i = integer dec_point = integer (location of decimal point) h30 = k48, used to convert ASCII values '0'-'9' to 0-9 (this is what BIN does for example)

Share this post


Link to post
Share on other sites
Dear Sir The problem is not with BIN command but with the HEX command. HEX only converts ASCII 0 to F. So I am stuck at that point. As far as your code, how do I program that in GX developer. I am habitual to Ladder logic only. Vihang

Share this post


Link to post
Share on other sites
i'm not sure why bother with HEX (which is used to convert to hexadecimal) since you are converting from ASCII to float (decimal) and it's not too convenient for this case. but if you do want to go that route, you just have to make sure that input is in valid range ('0'-'F'). in your case you will also have to filter out 'E' as well as characters that HEX can't process ('+','-','.'). also since dot is not always in same place, you should check each character individually before passing it to HEX instruction. ASCII character set has been around for very long time and it didn't change for at least 30 years. converting to and from ASCII is easily done without special instruction (i only used basic math and loops). since i don't have plc here to try built in instructions out, i just threw few lines together using only few basic instructions to show the concept. of course i could have used the ASCII values directly in inline comparisons etc. and make program shorter but this would be less readable. also i still don't know enough about output of your multimeter so i had to assume few things. since you are used to ladder, you should have no problem to modify the code if needed. good luck string2float.pdf

Share this post


Link to post
Share on other sites
So what you are receiving is a string of text characters. You need to use commands dedicated for ASCII text in the PLC to convert that to a numeric value. The FX2N command set for ASCII is not as complete as the newer FX3U or the rack style PLCs. In the FX3U there is a command called EVAL which converts ASCII to floating point. You may need to consider upgrading the PLC.

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