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

testing a string

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



Joined: 10 May 2005
Posts: 323
Location: Belgium

View user's profile Send private message

testing a string
PostPosted: Fri May 27, 2005 1:28 am     Reply with quote

Hi I am communicating with a device via a small protocol: This is our bytestring:

C-cmd1-cmd2-cmd3-cmd4-DL-D1-D2-....-Ddl

C = 0xAA = you're getting a command
CMD1-CMD2-CMD3-CMD4 = the command; eg. BATT/ADAP (means send me the battery-voltage)
DL = length of the data that is now coming
D1...Ddl = DATA itself

The question is now, how can I easily state in CSS-C :

IF ( the command = "BATT")
{
do_the_required();
}

ELSE IF ( CMD1,CMD2,CMD3,CMD4 == "BRAI" )
{
do_an_other_command();
}

These are the variables for the data:
Code:
char data[50] ;
char *p;
p = data;

appreciate your replies..
libor



Joined: 14 Dec 2004
Posts: 288
Location: Hungary

View user's profile Send private message

PostPosted: Fri May 27, 2005 4:11 am     Reply with quote

Code:
char data[50] ;
char *p;
char tempstr[5];
p = data;

strcpy(tempstr, "BATT");     //strncmp works with both strings in RAM only
if (strncmp(p+1, tempstr, 4))
  {
    do_batt_things();
  }

strcpy(tempstr, "BRAI");
if (strncmp(p+1, tempstr, 4))
  {
    do_brai_things();
  }
as an alternative you can concatenate all possible command words into one string, and search thru it for the received word. The index at which the word is found would serve as a parameter for the switch jump. strstr() has no n parametered version so you have to delimit your string manually by a 0.
Code:
strcpy(allcmdstr, "BATTBRAICMD3CMD4ETC0");
strncpy(tempstr, p+1, 4);
tempstr+4 = 0;   //you can save all this copying if you extract the DL parameter earlier and replace it with a 0 in place.
index = strstr(allcmdstr, tempstr);
switch (index) {
  case 0:  do_BATT();     break;
  case 4:  do_BRAI();  break;
  // ...etc
  default: do_syntax_error(); break;
}
If execution speed is of concern, I would rather test only some significant characters one-by-one to make a switch-case selection tree, though it would result a less readable/maintanable code and limited syntax checking.
These are untested codes typed directly here, may have errors, use them as clues only.
Christophe



Joined: 10 May 2005
Posts: 323
Location: Belgium

View user's profile Send private message

PostPosted: Wed Jun 15, 2005 5:40 am     Reply with quote

Code:
I've tested that 1st piece of code, and it's NOT working:

char data[47];
      char *p;
      char command[5];
      p = data;

// Commando gaan toetsen:

// Batterij commando?
    strcpy(command,"BATT");
    if (strncmp(p+1,command,4))
    {   
      putc('B');
    }

// Adapter commando?   
   
   strcpy(command,"ADAP");
   if (strncmp(p+1,command,4))
   {   
      putc('A');
   }


if I send: 170,65,68,65,80,0 (170,A,D,A,P,0) the program returns 66,65 (shouldn't return 66)

help wanted!


edit: this returns 1 (int8)
Code:
strcpy(command,"BATT");
i = strncmp(data[0],command,4);
putc(i);
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