Extracting data from a string
AdamL
Posts: 30
I'm trying to extract some data from a serial byte stream that is coming into the PBasic module.· To make it easier for initial programming, I am using the DEBUG and DEBUGIN commands to simulate the incoming data.· I'm able to isolate the information that I want, and it's able to pull the entire 8 byte chunk into the variable, but it will not allow me to take that variable and break it into 4 seperate pieces.· This is just a part of the program, so there are a lot of variables that appear to not be used--they are.
Here's what the debug window shows.· You can see that the KLine value is correct (and it waits until after 1100 to begin recording), but the others are not!
How can I get the values of 12, 13, 14, and 15 into different variables?· Obviously, using KLine(x) isn't working, but I'm not sure what would be the correct method.· Any help would be greatly appreciated!
Thank you,
Adam Lambertus
IOL······ VAR·· Bit
SOL······ VAR·· Bit
IL······· VAR·· Bit
SL······· VAR·· Bit
IR······· VAR·· Bit
SR······· VAR·· Bit
IOR······ VAR·· Bit
SOR······ VAR·· Bit
Position· VAR·· Word
KLine···· VAR·· Byte(8)
Baud····· CON·· 396
PSC······ PIN·· 15
KOut····· PIN·· 14
KIn······ PIN·· 13
MAIN:
· DEBUG "Enter string for sensors.· Enter hex values for four sensors, starting with initialization value of 1100", CR
· DEBUGIN WAIT ("1100"), STR KLine \8
· DEBUG CR, "KLine = ", STR KLine, CR
· DEBUG ? KLine(0)
· DEBUG ? KLine(1)
· DEBUG ? KLine(2)
· DEBUG ? KLine(3)
· DEBUG ? KLine(4)
SOL······ VAR·· Bit
IL······· VAR·· Bit
SL······· VAR·· Bit
IR······· VAR·· Bit
SR······· VAR·· Bit
IOR······ VAR·· Bit
SOR······ VAR·· Bit
Position· VAR·· Word
KLine···· VAR·· Byte(8)
Baud····· CON·· 396
PSC······ PIN·· 15
KOut····· PIN·· 14
KIn······ PIN·· 13
MAIN:
· DEBUG "Enter string for sensors.· Enter hex values for four sensors, starting with initialization value of 1100", CR
· DEBUGIN WAIT ("1100"), STR KLine \8
· DEBUG CR, "KLine = ", STR KLine, CR
· DEBUG ? KLine(0)
· DEBUG ? KLine(1)
· DEBUG ? KLine(2)
· DEBUG ? KLine(3)
· DEBUG ? KLine(4)
Here's what the debug window shows.· You can see that the KLine value is correct (and it waits until after 1100 to begin recording), but the others are not!
110012131415
KLine = 12131415
KLine(0) = 49
KLine(1) = 50
KLine(2) = 49
KLine(3) = 51
KLine(4) = 49
KLine = 12131415
KLine(0) = 49
KLine(1) = 50
KLine(2) = 49
KLine(3) = 51
KLine(4) = 49
How can I get the values of 12, 13, 14, and 15 into different variables?· Obviously, using KLine(x) isn't working, but I'm not sure what would be the correct method.· Any help would be greatly appreciated!
Thank you,
Adam Lambertus
Comments
-- grab 10's character, subtract "0" (48) from it, multiply by 10, move to result variable
-- grab 1's character, subtract "0" (48) from it, add to result variable
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
How could I turn it back into four seperate two digit hex characters?
Appears that it's returning it as ascii characters.......but I want it to be hex!
Post Edited (AdamL) : 11/3/2005 7:08:21 PM GMT
· LOOKDOWN hexChar, [noparse][[/noparse]"0123456789ABCDEFabcdef"], decVal
· IF (decVal > 15) THEN
··· decVal = decVal - 6
· ENDIF
You can use this with your parser routine to convert hex values to decimal; just remember that·you're dealing with base 16.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
I can do that for each of the characters in the string individually, and get the correct number/letter.
But when I try to say something along the line of IOL = I0 * 10 + I1, it clears the screen and skips it (error)
·
DEBUG HEX myValue
The help file (hint, hint) goes into a great deal of detail on how DEBUG works.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
The incoming bytestream is representative of 4 distance values, each between 0 and 180 cm. The original manufacturer chose to use hex as the output, so if it gives an output of 2c it means 44 cm. The DEBUG command understands the ASCII codes, but the mathematical operators try to just do the math on ASCII as if it were decimal.
It seems like the LOOKDOWN as you did it goes from hex into decimal. I need it to go from ASCII to hex. Oh, and can PBasic do hex math, or will I need to convert it first?
··· SERIN Sio, Baud, [noparse][[/noparse]WAIT("1100), HEX2 val1, HEX2, val2, HEX2 val3, HEX2 val4]
This works because you have fixed formatting and the BIN, DEC, and HEX modifiers are bi-directional: they convert numbers to strings for output, and strings to numbers for input.
Sorry I didn't think of this earlier ... my noisy neighbor woke me up a 3:30 AM and I haven't been thinking as clearly as I usually do.· And yes, I tried this with DEBUGIN and it does work.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Post Edited (Jon Williams (Parallax)) : 11/3/2005 10:23:31 PM GMT
The values from the other device are actually transmitted as hex (gives a range of 0 - 255 with one byte), so the way you had it above was perfect.
Thank you very much, your help was invaluable, and amazingly quick. You guys have an excellent product, and even better customer support. Thanks again!
Adam Lambertus