Shop OBEX P1 Docs P2 Docs Learn Events
Extracting data from a string — Parallax Forums

Extracting data from a string

AdamLAdamL Posts: 30
edited 2005-11-04 19:45 in BASIC Stamp
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.

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)

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

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

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-11-03 18:56
    If your field has fixed formatting, it's pretty easy:

    -- 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
  • AdamLAdamL Posts: 30
    edited 2005-11-03 19:04
    The only problem is that the input data is in Hex, so it's not always going to be that easy.......

    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
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-11-03 19:27
    Hex can be a little tougher becasue of the alpha characters -- luckily PBASIC has a few tricks up its sleeve.· Here's how you can convert a single hex character to a value between zero and 15 (does not trap an illegal characters):

    · 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
  • AdamLAdamL Posts: 30
    edited 2005-11-03 19:41
    Okay, so now if I tell it that I0 = KLine(0), then debug I0, it shows that I0 = the correct number/letter without needing a hex conversion.

    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)

    ·
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-11-03 19:44
    Remember that DEBUG expects ASCII characters, if you send it numeric values that coincide with screen control characters you may get "interesting" results. If you want to display a value as HEX, you must tell DEBUG to do so:

    DEBUG HEX myValue

    The help file (hint, hint) goes into a great deal of detail on how DEBUG works.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • AdamLAdamL Posts: 30
    edited 2005-11-03 20:08
    It is storing the characters as the ASCII equivalents of the numbers.....and you can't do math on ASCII. How can I change the value of the stored character from ASCII into the simple number or letter? Is that where I would need to use the LOOKDOWN command?

    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?
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-11-03 22:18
    As I was writing a demo program it occurred to me that the BASIC Stamp can handle the conversion for you.· If you're getting the input from some sort of serial stream you can do this:

    ··· 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 WilliamsJon Williams Posts: 6,491
    edited 2005-11-03 22:20
    Are your numbers actually BCD?· You keep referring to them as "hex" and now I'm wondering.· If they are in fact BCD, i.e., the string value for a number will be "00" to "99" then you need to change the HEX2 modifiers to DEC2.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax

    Post Edited (Jon Williams (Parallax)) : 11/3/2005 10:23:31 PM GMT
  • AdamLAdamL Posts: 30
    edited 2005-11-04 19:45
    Excellent! Works like a charm.

    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
Sign In or Register to comment.