Serial buffer to array of strings
JoMo
Posts: 19
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)
Any·help would be greatly appreciated!
Thanks!
Joe
·
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
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
Phil, great thanks! I will check it out.
-Joe
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