Converting string to numeric????

ponoli

Guest
P
I am using a Ge 90-30 PLC as a modbus master to read watthour readings from a Watt hour meter using RS 485.but the watthour readings are soterd in ASCII format.I need to convert this in to numeric so as to display it.I am using versapro software.can anyone help me??
 
There is no ASCII-to-integer conversion in the 90-30, so you'll have to write your own routine. The data will probably be stored as two bytes per register (packed ASCII).

If you have a SCADA or HMI cnnected to the PLC, it might prove easier to pass along the entire ASCII string to the HMI and let it do the conversion and send the result back to the PLC.

To get you started, assume that you have the number '1234' stored in %R101 and %R102 as packed ASCII. Register %R101 will probably contain the value 12849 (hex 3231), and %R102 will probably have the value 13363 (hex 3433).

To isolate the low byte, AND the register with hex 00FF. To isolate the high byte, AND the register with hex FF00, then divide by 256. To convert from ASCII to an integer, subtract 48 (hex 30) from the ASCII value.
 
I can't help you on the brand specific information, but here's a tip:

If you can tranfer or analyze each digit as a character you should be able to determine its ASCII value (0-127). You can then subtract a constant (off the top of my head, I think it's 48 or 42. The ASCII value of the character "0") from the ASCII value to offset the position in the table. Since the numbers 0-9 are sequential in the ASCII table, you'll end up with the value of the digit.

Multiply by the correct powers of ten and add it all together, there's your value.

That's the short explanation of what you may need to do (and I know from experience that it can work). Things get more complicated if you have decimal points, spaces, signs, or other charaters. But, you can still handle it if you've got your pencil sharp enough.

AK
 
Rolling Your Own

Rolling your own ASCII-to-Numeric-code, here is an example of its principle. Its missing some lines, but the basic is there.

Using a sequense of word's, e.g. BufferWord, BufferWord+1, ..
Load these words with <0> (NUL) and then with the string, but only one ASCII character in each word.
MSB digit (or sign) in BufferWord, next digit/ASCII character in BufferWord+1 and so on.

For PLCs supporting structured text, this could be used almost as is. Those with only ladder or instruction list, have to do some rewriting.

Integer ASCII: If the ASCII-data is Integer-ASCII, e.g. "01234" :

; All variables of type Word

n:=0
sign:=1
dummy:=0

; Assuming the sign in the first character..
IF BufferWord[n]= 45 ; <45> = - character
THEN
sign:= -1
n:=1
END_IF

IF BufferWord[n]= 43 ; <43> = + character
THEN
n:=1
END_IF

WHILE BufferWord[n] != <0> DO
dummy:= dummy * 10
dummy:= dummy + (BufferWord[n] - 48) ; <48> = 0 character
n:=n + 1
END_WHILE

integer:= sign * dummy


Instead of checking different from <0>, one could check: GREATER than <47> AND LESS than <58> since "0" = 48 and "9" = 57


Float-ASCII e.g. "-0123.0012" :

The PLC must support floating number and having a IntToFloat function.


n:=0
NoDec:=1.0 ; Dword with float/real
sign:= 1.0 ; Dword with float/real
floatint:=0.0 ; Dword with float/real
floatdecimal:=0.0 ; Dword with float/real

IF BufferWord[n]= 45 ; <45> = - character
THEN
sign:= -1.0
n:=1
END_IF

WHILE (BufferWord[n] != <0>) AND (BufferWord[n] != <46>) AND (BufferWord[n] != <44>) DO
floatint:=floatint * 10.0
floatint:=floatint + IntToFloat(BufferWord[n]-48) ; <48> = 0 character
n:=n + 1
END_WHILE

IF (BufferWord[n] = <46>) OR (BufferWord[n] = <44>) ; "." OR ","
THEN
n:=n+1
END_IF

WHILE (BufferWord[n] != <0>) DO
floatdecimal:= floatdecimal * 10.0
floatdecimal:= floatdecimal + IntToFloat(BufferWord[n]-48) ; <48> = 0 character
n:=n + 1
NoDec:= NoDec * 10.0
END_WHILE

floatdecimal:=floatdecimal/NoDec
float:= sign*(floatint + floatdecimal)


This is only the basic.Some checks are missing before it is bullet-proof
 

Similar Topics

Hi all, I've got a bit of a need to convert an integer (and a real in another situation) to a string with a particular format. This seems possible...
Replies
11
Views
2,442
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,213
Hello folks, Using AB RSLogix micro.. I have a 6 digit ASCII text string and Im looking to convert the 4 most significant digits to an integer...
Replies
2
Views
1,444
I am sending Omron Text over devicenet to an AB GuardLogix Processor. I am having a difficult time converting the string to readable data. I am...
Replies
8
Views
4,151
Is there a way to convert these integers (17476,16688,13110,12598) to Ascii character (DDA03616)? 17476 = 4444hex = DD on ascii chart 16688 =...
Replies
7
Views
6,727
Back
Top Bottom