|
|
View previous topic :: View next topic |
Author |
Message |
Christophe
Joined: 10 May 2005 Posts: 323 Location: Belgium
|
testing a string |
Posted: Fri May 27, 2005 1:28 am |
|
|
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
|
|
Posted: Fri May 27, 2005 4:11 am |
|
|
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
|
|
Posted: Wed Jun 15, 2005 5:40 am |
|
|
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); |
|
|
|
|
|
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
|