Converting ASCII to Decimal

ettikudiappan

Member
Join Date
Apr 2002
Location
Singapore
Posts
131
Hi

I got a pressure sensor that sends it's pressure in HEX as ASCII via RS232.
That is the String read from the sensor looks like "Line Press 1A".
The 1A in the string is in Hex and converting it to decimal the pressure value turns out to be 26 PSI.

I need to do this conversion using SLC 500. Any idea on what algorithm to use? By manipulating the string I was able to populate an integer file with the value 1A in Hex. I am unsure of how to proceed further.

Thanks in advance
 
Hi ettikudiappan,

if the integer file contains a value of "1A" when displayed as HEX, then it is at the same time "26" when displayed as DECIMAL. Simply use the value as it is in your program.

Check this by opening the datafile in RSLogix and change the "radix" to "HEX/BCD" or to "Decimal" to view it as 1A and 26 respectively.
 
Hi JesperMP

The 1A displayed is in ASCII. That is, when I view the data file, the radix I use is ASCII and with this radix I get the value 1A.
If you look at the same value in Hex it would be 3141 and if viewed in decimal the value would be 12609. I hope that clarifies my question.
 
The ACI "string to integer" instruction should do the trick.

Here is what the help-text says:

1. The ACI rung is enabled

2. The processor searches the source for the first character between 0-9. All numeric characters are extracted until a non-numeric character or the end of the string is reached. Action is taken only if the numeric characters are found. If the string contains an invalid length (<0 or >82) the ASCII Error bit S:5/15 is set. Commas and sign(+,-) are allowed in the string. However, only the minus sign is displayed in the data table.

3. The extracted numeric string is converted to an integer. The ASCII Error bit S:5/15 is set if a numeric overflow occurs or if the string contains an invalid string length.
 
Hey I just see that there is a problem:
ACI only converts 0-9, not the A-F of the hexadecimal values.

I think there is no other way than to evaluate the contents of each ascii letter and then combine them with a bit of math.

pseudocode:
evaluate letter 1:
if letter 1 is "0" then VAL:=0
if letter 1 is "1" then VAL:=16
if letter 1 is "2" then VAL:=32
etc.
if letter 1 is "E" then VAL:=224
if letter 1 is "F" then VAL:=240

evaluate letter 2:
if letter 2 is "0" then VAL:=VAL
if letter 2 is "1" then VAL:=VAL+1
if letter 2 is "2" then VAL:=VAL+2
etc.
if letter 2 is "E" then VAL:=VAL+14
if letter 2 is "F" then VAL:=VAL+15

you get the idea I hope
 
Have just examined the matter a little further,

it seems that two letters are stored in each word, so that each letter is covered by a byte.

Use MVM "move with mask" to put the content from the first letter into one word, then divide it with 255 (FFhex).
Put the content from the second letter into another word with no further processing.
This should make two words with the contents of the ascii values as integers for further processing
(30ascii=0hex=0dec, Aascii=41hex=65dec, Fascii=46hex=70dec).

revised pseudocode:

evaluate letter 1:
if letter 1 is "30" then VAL:=0
if letter 1 is "31" then VAL:=16
if letter 1 is "32" then VAL:=32
etc.
if letter 1 is "69" then VAL:=224
if letter 1 is "70" then VAL:=240

evaluate letter 2:
if letter 2 is "30" then VAL:=VAL
if letter 2 is "31" then VAL:=VAL+1
if letter 2 is "32" then VAL:=VAL+2
etc.
if letter 2 is "69" then VAL:=VAL+14
if letter 2 is "70" then VAL:=VAL+15
 
JesperMP - that seems like a lot of testing to do the conversion. I suggest doing the following for each of the two isolated ASCII letters yielding a seperate result for each:

Result = Letter - 48(decimal)
If Result > 9(decimal) then Result = Result - 7(decimal)

Then just multiply the upper result by ten and add the lower result
 
Thanks to bernie_carlton and JesperMP for the replies. A small correction to bernie_carlton's reply. The upper result should be multiplied by 16 instead of 10. ;)
 
bernie,

you are absolutely right. I did think that the code could be shorted down, but I just experimented with the ASCII-HEX-DECIMAL values to find out how they actually worked.

ettikudiappan,

I am curious about your pressure transmitter; thinking about it I find it crazy that it sends the values in an ASCII string with the pressure value in HEX. For what reason ?
Pressure transmitters comes in bucket-loads nowadays; cheap and almost always with a 4-20mA output signal.
Is there any particular reason for using that particular pressure transmitter.
 
download it

Go to www.tomantron.com
then go to Downloads and then download muxaps1.zip

It's a complete Ladder program to convert ASCII to Integer and Integer to ASCII.
It's fairly involved.
 

Similar Topics

Do any of you Excel gurus know of a VB code or function that will convert ASCII text into it's decimal equivalent? I'm not VB savy at all. I...
Replies
3
Views
5,801
H
Dear all, i have to convert ASCII code into decimal or hexadecimal in my twido PLC using ladder diagram because the data that i send from my pc...
Replies
0
Views
4,405
hosana
H
We are pulling time through Modbus from 3 reg. the data we are getting back to the PLC is 12593 for hours, 13105 for minutes, and 8240 for...
Replies
3
Views
1,144
Have an unusual one here, guys... I have a remote machine that is sending my 1769-L33ER five 16-bit integers that represent 10 ASCII characters...
Replies
4
Views
2,164
Hello all, We need to convert ASCII data into Real Raw data, here we use Trimble GPS and need to get the position data. We use SCADAPack 357...
Replies
4
Views
2,761
Back
Top Bottom