CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Receive Data Packets

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ttelmah
Guest







PostPosted: Sat Jun 16, 2007 3:20 am     Reply with quote

If you look in 'input.h', at the source code for 'get_string', you will see how to code a function to build a string, and return when the string is complete (with a length limit for safety as well).
This contains some 'extras' (backspace handling primarily), which you won't need.
The alternative (which is quicker when the command completes), is to instead do a 'tree' comparison, on a per character basis.
If you have an array of commands, listed in alphabetical order, and two 'static' counters, one for 'line', and one for 'column', initialised to zero, then when a character arrives, you search 'down' the lines, till you get a match with the first character, incrementing 'line' as you go. Since this is a single character test, it can be done using '==' (quickly) You then increment the 'column' counter, and test the character it is pointing at. If this is '0', then the whole string has been matched, and the 'line' counter gives the command number. If not, you exit, and wait for the next character.
Code:

const int8 keywords[8][11] = {
   "ACKOFF",
   "ACKON",
   "AUTOn ",
   "Axxxxxxxxx",
   "BAUDn ",
   "Bxxxxxxxxx",
   "CAL_ONn ",
   "xxxxxxxxxx"
};
//Typical array of keywords. 'xxxx' markers added see code for why.

int16 ipno; //temporary used to hold an input 'number'

int8 MyIsdigit(int8 val){
  if (val>'/' && val<return>15)
      phase=0;

   //Now scan the incoming character
   while (true) {
   if (phase==0) {
      //Here I am searching for an element in the text array
      temp=(cval>='a' && cval<='z')?cval-32:cval;
      //Now I have uppercase only.
      while (true) {
         srch=keywords[row][col];
         if (srch=='\0') {
            //Have got to the end of the string
            have_command(row);
            //call the handler, with the row number of the 'match'
            ccnt=0;
            break;
         }
         else if (srch==tempch) {
            //Here the character matches
            col++;
            break;
         }
         else if (srch=='n') {
            //Demo of a special command character to read a number
            //from the input
            col++;
            phase=1;
            ipno=0;
            break;
         }
         else if (srch=='x') {
            //Here match has failed
            phase=0;
            break;
         }
         //try next row and loop
         row++;
      }
      //exit the 'while'
      break;
   }
   if (phase==1) {
      //Here I am looking for a numeric field - as each digit arrives, ipval
      //is multiplied by 10, and the new character added. If I reach a 'non
      //numeric' character, it marks the end of the conversion.
      if (Myisdigit(cval)) {
         ipno*=10;
         ipno+=(cval-'0');
         break;
      }
      phase=2;
      //Loop again, in case the new character is the start of another
      //command
   }
   }
}

If you call this, with each character as it is receiver, it'll call 'have_command', with the row number that matches when a command is seen.
The 'x' lines are added to slightly speed the search.
I wouldn't guarantee this to work, it is cut out of a much larger parser, with checksum handling etc., and I'd not warrant I have got it completely right, but it should be 'close'.
You call 'parse(val)', with each received character.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group