Note that when you fail a test for the second or third etc character in trying to test for a sequence of characters on they fly like this, when you loop back to start, you may want to have stuffed the character that just failed back into the stream to check from. In fact, you may want to stuff all of the characters back in except the very first character, this depends a bit on what you are trying to find. For example, if I am parsing for the sequence AABC and I encounter the sequence AAABCD the routine will match the first two AA's and reset on the third A. Unless you just dropped the first character in the sequence, you would miss your string. Everything Peter said is valid of course, it just depends on what you require. regards bob Bob Bullock President Western Support Group Ltd. Certified Microchip Consultants At 02:19 AM 6/5/2003 +0300, you wrote: >(long post) > > >as well as having examined the NMEA 0183 spec and all I have to do now is > >to figure out how to extract a five byte string out of a sentence that > >begins with 4 unique recognizable ASCII characters and is followed by a > >number of commas. At 4800 baud or faster I guess I would have to UART all > >the data in, then parse for my favorite characters, which will fall > >between say, the 7th & 8th comma. > >Not pic, not nmea, but normally you do not keep data you do not need, >instead build a state machine that recognizes the characters one by one. >You then examine each received character and do something with it >depending on where you are in the state diagram of the fsm (you get to >draw this - you know - bubbles with text and arrows). > >Then you write have code like: > > state = START; // have to start somewhere > >loop: // forever, or at least while wanting to decode input > c = read_char(); > switch(state) { // test state against some constants > case START: > if(c == FIRST_CHAR) > state = FIRST_SEEN; > break; // not seen, just stay in START and look at next char > case FIRST_SEEN: // seen, expect 2nd > if(c == SECOND_CHAR) > state = SECOND_SEEN; // got it > else > state = START; // bad char seen, try again from start > break; > // more cases like this ... > case BEFORE_FIRST_USEFUL_GROUP: // ! this is important for ea. > group > if(isuseful(c)) { // start storing here, continue in next state > isuseful[0] = c; // it's good, remember it > isusefulcounter = 1; // got the 1st already > state = FIRST_USEFUL_GROUP; // in the group now, *next* state > } else > state = START; // bad char, try from start > break; > case FIRST_USEFUL_GROUP: // with each c. in this group it except > // the first! > if(isuseful(c)) { > if(isusefulcounter >= MAX_USEFUL) { // got too many ? > scream_and_die(); // one of these will work for you > state = START; > break; > // eat_sliently(); > } > isuseful[isusefulcounter++] = c; // a good un', loop for more > } else { > if(c == SEPARATOR) { // say, comma, or space ? > // done reading a group of interesting chars, use them > do_someting_useful_with(isuseful[]); // bad syntax, good idea > state = BEFORE_SECOND_USEFUL_GROUP; // ea. group has a > 'before' > } else > state = START; // bad char, you know what to do > } > break; > case BEFORE_SECOND_USEFUL_GROUP: // cont. as above > // more like this as needed ... > default: // catch all, no such thing as an undefined state > printf("This state (%d) does not exist. Go to sleep.\n", state); > state = START; // you may want to stop the program instead > } > goto loop; // or equiv. interrupt-driven 'loop' > >Now this is verbose. Yacc and Flex produce similar code but in a much more >compact way, representing the states and transitions as bytecode. They are >not recommended for small pics (the code is huge). To write your own you >can do it by hand (one off) or write a tool that produces such a state >machine from a definition (like yacc does). A one off starts with a >drawing of the state machine (bubbles and arrows) and then you implement >it, each bubble is a state (and has a case statement) and each arrow out >of a bubble is equivalent to a tested condition in that case code (falling >out the bottom of the case also represents an arrow). Relatively easy >troubleshooting involves counting the states (must be == to the number >of bubbles in the drawing) and the exits from each case (must == the >number of arrows *starting* from the bubble whose case is being examined). > >good luck, > >Peter > >-- >http://www.piclist.com#nomail Going offline? Don't AutoReply us! >email listserv@mitvma.mit.edu with SET PICList DIGEST in the body -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body