Shop OBEX P1 Docs P2 Docs Learn Events
Serial buffer to array of strings — Parallax Forums

Serial buffer to array of strings

JoMoJoMo Posts: 19
edited 2007-12-12 22:09 in Propeller 1
Hi,
Sorry to be ignorant here, but I can't seem to wrap my head around strings (and arrays) in Spin.
I want to have a method that will read the serial buffer and return the lines in it·in a·string array.

Let's say the serial buffer had this in it ( <LF> is a linefeed ) ·
howdy: john <LF> howdy: paul <LF> howdy: george <LF> howdy: insert drummer here <LF>

I want to be able to iteratate through the names in an array.
(ultimately stripping out the 'howdy:' part, but I will figure that out later)

' I know this won't work, but this is what i want to do:

var 
    byte line[noparse][[/noparse]255]    
    byte lineArray[noparse][[/noparse]20][noparse][[/noparse]255]  
 
pub printBuffer | bufData, k, i
    k := getNames 
    repeat i from 0 to k
       LCD.str(lineArray[noparse][[/noparse]i])
       
 
pri getNames| i,k,n
    b := 0
    i := 0
    k := 0
    while b <> -1 ' let's say that a -1 means the buffer was empty
      b := serial.rx
      if(i<255) ' if there's room, add to the line
         line.byte[noparse][[/noparse]i++] := b
         if(b==10 and k<20) ' if we get a lineFeed and there's room, 
            iineArray[noparse][[/noparse]k++] = byteArray ' then add the line to the line array.
            'then reset the byte array for the next line.
            repeat n from 0 to i
              line.byte[noparse][[/noparse]n] :=0 
    ' so at this point I would have an array of names that I could iterate thru.
    return k ' return the size of the line array

Any·help would be greatly appreciated!

Thanks!
Joe

·

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-12-12 01:31
    1) Arrays are single dimension only

    2) There really are no built-in array or string operations in Spin (other than BYTEMOVE, STRSIZE, and STRCOMP).
    You can do a lot of stuff with STRSIZE and STRCOMP which assume zero-terminated strings and BYTEMOVE which
    requires a length (which you can get with STRSIZE).

    3) It would be pretty straightforward programming to have two arrays, one a large byte array with successive lines in it, each zero-terminated (with the zero byte substituted for the LF) and one a smaller word array with the index (or address) of the start of each line stored. You could then take each line and skip any leading phrase, leaving the pointer to point to the start of the name you want to keep.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-12-12 01:40
    Awhile back, I wrote a strings object that will do what you want, using the split procedure. It comes with some baggage, though, in that the strings are dynamically-allocated by means of an included heap manager. Anyway, here's the link, if you think it might be useful: http://forums.parallax.com/showthread.php?p=587930.

    -Phil
  • JoMoJoMo Posts: 19
    edited 2007-12-12 03:45
    Thanks Mike, good info.
    Phil, great thanks! I will check it out.

    -Joe
  • JoMoJoMo Posts: 19
    edited 2007-12-12 22:09
    Phil,
    Great!
    Clone, Remove, Split and presto! I now have exactly what I wanted.
    Very nice work!
    Thank you!

    Cheers
    Joe

    Post Edited (JoMo) : 12/12/2007 10:14:10 PM GMT
Sign In or Register to comment.