i have seted up a modbus link with a exsternal device the problem is now that i get my data in as 4 bytes of hex numbers
eg. 07D9 is what i am getting it must become a year number 2009
any idea's how i can get this conversion
thanks in advance
i have seted up a modbus link with a exsternal device the problem is now that i get my data in as 4 bytes of hex numbers
eg. 07D9 is what i am getting it must become a year number 2009
any idea's how i can get this conversion
thanks in advance
this is what i have tried so far
DataA=DATAIN(8)
Gosub HEXConvert
The idea is data from the attay is pased to a temp value, a subrotine is called end if the values are bigger than 9 the coresponding digital value is replaced into the temp value, if none of the if statements are true the value are returnd
it still return the same hex caractor - any ideas
Code:HEXConvert: IF DATAA = $A THEN DATAA = 10 RETURN ENDIF IF DATAA = $B THEN DATAA = 11 RETURN ENDIF IF DATAA = $C THEN DATAA = 12 RETURN ENDIF IF DATAA = $D THEN DATAA = 13 RETURN ENDIF IF DATAA = $E THEN DATAA = 14 RETURN ENDIF IF DATAA = $F THEN DATAA = 15 RETURN ENDIF RETURN
If you make an assumption firstly that you have valid (UPPERCASE) ASCII HEX and not Klingon or some other alien data... then your ASCII HEX to Numeric conversion could be as simple as...
If you need to handle UPPERCASE or lowercase (valid) HEX, then your code needs an extra line...Code:DataA=AsciiHexByte-48 If DataA>9 then DataA=DataA-7
What YOU are doing is NOT converting ASCII HEX... you are converting one form of Number representation to another form of exactly the same number... $A=10 anyway, so what you are doing is stating...Code:DataA=HEXByteInput-48 If DataA>9 then DataA=DataA-7 If DataA>15 then DataA=DataA-32
If DataA=$A then DataA=10
... which is the same as...
If DataA=10 then DataA=10
... which is kinda pointless... what you should have said is...
If DataA="A" then DataA=10
... or to be long-winded about it...
If DataA=65 then DataA=10
... because $A=10 whilst "A"=65
Thanks
This is how I hot it working, you were right about the A being 65
Code:HEXConvert: IF DATAA = 65 THEN DATAA = 10 RETURN ENDIF IF DATAA = 66 THEN DATAA = 11 RETURN ENDIF IF DATAA = 67 THEN DATAA = 12 RETURN ENDIF IF DATAA = 68 THEN DATAA = 13 RETURN ENDIF IF DATAA = 69 THEN DATAA = 14 RETURN ENDIF IF DATAA = 70 THEN DATAA = 15 RETURN else DATAA = DATAA - 48 ENDIF RETURN
Hmm, some people still like spaggeti much more!
Hope at least there is a good salsa with it...
Ioannis
If you're sure your data really just consists of the digits and letters A through F, use this instead. Including comments will help when you come back to it later too.
http://www.asciitable.com/.Code:' This works because 0 - 9 have decimal codes 48 - 57 and A - F have ' decimal codes 65 - 70 in ASCII HEXConvert: IF DATAA >= 65 THEN DATAA = DATAA - 55 ' 65 - 55 = 10, 66 - 55 = 11 etc else DATAA = DATAA - 48 ' 48 - 48 = 0, 49 - 48 = 2 etc ENDIF RETURN
An algoritm to convert Hex to Dec is as follows:eg. 07D9 is what i am getting it must become a year number 2009
Hex 07D9 = 9x16^0 + 13x16^1 + 7x16^2 + 0x16^3 = Dec 2009
I don't see how you achieve the convertion with your system!
Al.
Last edited by aratti; - 26th June 2009 at 00:18.
All progress began with an idea
Bookmarks