Sign in to follow this  
Followers 0
Duffanator

ASCII to decimal conversion

14 posts in this topic

Hey all, I've got a question about ASCII conversion. I have data going into several D registers and I want to convert it from ASCII to decimal. The problem I have is the way that it is set up in the PLC. I am pulling some weight data out of an ethernet card and putting it into some D registers in the PLC. The data syntax is: Scale# Date Time Weight ex: 02 1/6/2008 06:20:50 26.14 The problem is that when I pull the data out and put it into D registers it breaks it up so that there are 2 ASCII characters in each D register. I want to convert the weight so that it is just a 4 digit number in one D register with no decimal point. I am not sure how to break it up so that I can convert the ASCII characters to decimal and then add them together to make one number. I also can't figure out how to get rid of the space character and the decimal character without having hundreds of steps of math functions. This is what I am getting: D54 - SP,2 (ASCII) D55 - 6,. (ASCII) D56 - 1,4 (ASCII) And this is what I want: D?? - 2614 (Decimal) Any help would be greatly appreciated. Thanks!

Share this post


Link to post
Share on other sites
First thing you need to say is what model PLC you are using. There are many commands for converting text to integer or floating point to integer, but the names of the commands and the format vary from processor to processor. Also, that space on the beginning may be a problem, you might need to do some bit shifting to move the string to start without the space, but it should be fairly easy if you tell us what PLC.

Share this post


Link to post
Share on other sites
As Crossbow states, the processor makes a difference. If you're using the new FX3U, check out the BTOW and WTOB commands for formatting the string. With the rest of the FX series you'll need to do bit shifts. In either case, you'll want the BIN command to turn your formatted string into a binary number. I haven't done this on the Q, but should be similar.

Share this post


Link to post
Share on other sites
wand d54 h0ff d60 ' get highest digit, strip not needed bits, move it to temp (D60 for example) ' note that this is already in lower part of the D60 register (no swapping or shifting) sub d60 h30 d60 ' convert ascii to binary (just subtract 30h or 48d) ' first digit done... ("2") mul d60 k10 d70 ' multiply result by 10 before adding next digit (10 is base in decimal system, result is "20") wand d55 hff00 d60 ' move next digit to temp and as always strip not needed bits swap d60 ' place the digit to lower part of register sub d60 h30 d60 ' convert ascii to binary (subtract 30h or 48d) add d60 d70 d70 ' add the two (result is "26") mul d70 k10 d70 ' multiply result by 10 so next digit can be added (result is "260") wand d56 hff00 d60 ' extract next digit swap d60 ' it is in upper byte but we need it in lower (swap or shift right 8 places or divide by 256 or whatever...) sub d60 h30 d60 ' convert ascii to binary (subtract 30h or 48d) add d60 d70 d70 ' add it (result is "261") mul d70 k10 d70 ' multiply result by 10 so next digit can be added (result is "2610") wand d56 h0ff d60 ' extract last digit (this one is already lower byte, nothing to swap) sub d60 h30 d60 ' convert ascii to binary (subtract 30h or 48d) add d60 d70 d70 ' add it to result (result is now decimal value "2614") this off course may be simpler and shorter with other instructions (ones specific to selected CPU) but this example should be easy to understand (and remember) and - it should work on ANY plc (and any brand). also check if the sent number of digits is changing etc. (this code sample doesn't do error checking). i normally copy the mentioned block to bits so one can use fewer instructions and avoid swaps etc. (easy to index digits) also this may be shorter if implementing a loop, specially when number of digits increases.

Share this post


Link to post
Share on other sites
Can you change the scale to get rid of the decimal point. ie if your weight is 26.14g, then can you ask the scale to work in mg so you would get 2614mg. I know its not the complete solution but it helps a bit.

Share this post


Link to post
Share on other sites
oops, yeah sorry I forgot to say what PLC I am working with. It is an A2SH CPU, thanks!

Share this post


Link to post
Share on other sites
that is good idea but 26.14gram is 26140 instead of 2614 miligram (mili = 1/1000) let's see who can come up with neatest conversion code....

Share this post


Link to post
Share on other sites
OOPS!!! I should know better.

Share this post


Link to post
Share on other sites
Well, I figured out one way to do it. I just used a DIS funtion to pull out every 4 bits and put it into it's own D register. Since it's ASCII code and a 0 in ASCII is 30 in HEX it actually works out that you can just pick out the digits you need. if the weight is 24.14: D52 = 2,SP(ASCII), 3220H you can use a DIS with K4 and get: D60 = 0 Decimal D61 = 2 Decimal D62 = 2 Deciaml D63 = 3 Decimal If you do that for every D register you can pull the weight digit out and mulitply it by what ever place it should be in (1000, 100, 10) and then add them together. Maybe not the cleanest way to do it, but it worked! Thanks for the help everyone, much appreciated.

Share this post


Link to post
Share on other sites
Is it always ##.##, or are there sometimes more digits?

Share this post


Link to post
Share on other sites
It is always ##.##, the max weight is around 30 pounds.

Share this post


Link to post
Share on other sites
I think the DIS command is your best option. That's an instruction I've never used, and it's a "nibble" version of the WTOB (word-to-byte) instruction that I mention previously. I've only done ASCII - Binary conversion on the FX3U, which has quite a few dedicated instructions for that.

Share this post


Link to post
Share on other sites
I tried this too and it works as well, interesting way of doing it and something I wouldn't have thought of. Thanks for the info!

Share this post


Link to post
Share on other sites
nigel powers (as programmer): "it's not the complexity of instruction mate, it's how you use it..."

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