Parsing Strings...


Closed Thread
Results 1 to 24 of 24

Hybrid View

  1. #1
    Join Date
    Feb 2009
    Posts
    41

    Default Parsing Strings...

    Hi Everyone,

    I have written a big chunk of code an it is working well, but I am stuck on something having to do with strings.

    I have received a string from a serial device which contains two target strings seperated by a comma. The data looks like this:

    1432,765

    the length may vary like this:

    22567,8345

    so the the comma is key as a locator. I can't figure out with what little documentation is in the users manual how to parse the string for these two numbers.

    Thanks in advance!
    TR

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Look in the manual under SERIN2.

    Look for the part something like:
    WAIT STR\n followed by optional end character...for the first part and then use something like WAIT[,] to get the last part.
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    967


    Did you find this post helpful? Yes | No

    Default

    Can you clarify what you mean when you say "I have received a string from a serial device" Does it mean it has been stored somewhere and you have to parse it from there? SERIN2 will not do that for you.

  4. #4
    Join Date
    Feb 2009
    Posts
    41


    Did you find this post helpful? Yes | No

    Default Thanks...

    I appreciate all of your help!

    I had written the code using Hserin to capture the raw data from my serial port into a single string of [10]. It works so now I need to separate the two values. So...

    14575,354 would end up as two longs,

    one with 14575 and another with 345. Either number could be as large as 99999 or as small as 1.

    Thanks in advance,
    TR

  5. #5
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    HSERIN uses the same syntax as SERIN2, so do the "separation" as the data is coming in.
    Dave
    Always wear safety glasses while programming.

  6. #6
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    967


    Did you find this post helpful? Yes | No

    Default

    Ok, since you have the string to be parsed, you will need a parser too! I dont have anything ready at hand but, I'll give it a shot anyway. UNTESTED CODE FOLLOWS

    Code:
    ' assume the string is TOPARSE
    ' it will return you 2 numbers,  one from before the comma, one from after the comma
    Number1 var Long
    Number2 var Long
    Cntr       var byte      ' counter to index the string
    
    ParseNumber:
     Number1 = 0              ' start with 0 in both numbers
     Number2 = 0
     'collect Number1
     for Cntr = 0 to 10        ' you said your string is 10 places long
        if TOPARSE[Cntr] <> "," then
             Number1=Number1*10                             ' x10 to make place for the new digit
             Number1 = Number1+TOPARSE[Cntr]-'0'      ' I'm assuming this is an ASCII string
        else
             goto GetNum2     ' collect the remainder as number2
        endif
     next
     return
    GetNum2:
     for Cntr=Cntr+1 to 10
        if TOPARSE[Cntr] <> "," then
             Number2=Number2*10
             Number2 = Number2+TOPARSE[Cntr]-'0'      ' I'm assuming this is an ASCII string
        else
             return                ' because, you said there are only 2 numbers ;)
        endif
     next
     return
    You could modify this code to do a repetitive parse. Every time you call it, it would return you a number, but I'll leave that to you.

    Good luck.
    Last edited by Jerson; - 11th February 2009 at 16:12. Reason: GetNum2: for Cntr=Cntr+1 to 10

  7. #7
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    I still think separating the numbers on the way in is the way to go. Although Jerson's code is pretty slick.

    Here is what I was thinking. Just tested it using SERIN2 as the chip I have on the bench is not set up for HSERIN right now.

    The first WAIT is to keep garbage out.
    Code:
    N1	VAR	LONG
    N2	VAR	LONG
    LOOP:
    SERIN2 PORTD.2, 16416, [WAIT("X"),DEC N1,WAIT(","),DEC N2]
    GOTO DISPLAY
    
    DISPLAY:
    Serout2 PORTC.6, 16416, [ DEC N1, $d, $a]
    Serout2 PORTC.6, 16416, [ DEC N2, $d, $a]
    GOTO LOOP
    If you need the data in an array just modify the above with STR...
    Last edited by mackrackit; - 11th February 2009 at 15:09.
    Dave
    Always wear safety glasses while programming.

  8. #8
    Join Date
    Feb 2009
    Posts
    41


    Did you find this post helpful? Yes | No

    Default Hmmm...

    Both great suggestions for which I appreciate!!! I'm pondering....

Similar Threads

  1. Parsing serial data
    By Heckler in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 4th March 2010, 14:25
  2. Please help with storing strings in codespace
    By g-hoot in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 16th May 2008, 01:02
  3. How to write/read strings EEPROM/LCD
    By g-hoot in forum mel PIC BASIC Pro
    Replies: 22
    Last Post: - 11th February 2007, 06:26
  4. Processing lengthy strings
    By sougata in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 21st March 2006, 05:27
  5. I2CWRITE writing Strings to EEPROM
    By NavMicroSystems in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 27th March 2005, 19:45

Members who have read this thread : 0

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts