|
|
View previous topic :: View next topic |
Author |
Message |
Schneider
Joined: 20 Nov 2006 Posts: 8
|
Problem with char protocol validation |
Posted: Mon Jan 22, 2007 9:03 pm |
|
|
I wrote a function that should return true or false dependind on a string received from the rs232.
Code: |
boolean isCmd(char *pStr[]){
char sTmp[] = {"#CMD:"};
int iTmp;
iTmp = (strlen(*pStr)-1);
printf("strPar: %s\r\n", *pStr);
printf("strCom: %s\r\n", sTmp);
printf("Match: %u\r\n", strspn(*pStr, sTmp));
printf("Index: %u\r\n", iTmp);
printf("Close: %c\r\n", *pStr[iTmp]);
if ((strspn(*pStr,sTmp)==5) & (*pStr[iTmp] == ';')){
return true;
}else{
return false;
}
}
|
the printf in the function is to trace the values while testing the function.
I use it this way:
Code: |
char strBuffer[];
// receive data trough rs232 #CMD:123;
gets(strBuffer);
if(isCmd(&strBuffer)){
printf("CMD is valid!\r\n");
}else{
printf("CMD is invalid!\r\n");
}
|
everything works fine until the *pStr[iTmp]. it doesn't return the last character in the array. what am i doing whrong?
thanks for any help. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jan 22, 2007 9:28 pm |
|
|
I didn't review your code in detail, but this statement here has got a
few problems:
Quote: | if ((strspn(*pStr,sTmp)==5) & (*pStr[iTmp] == ';')){ |
The strspn() function takes two pointers as arguments. You've got
a dereferenced pointer as the first argument. It should be a pointer.
The 2nd thing I see is that your intention is to do a logical operation
on the two expressions but you have a bitwise AND operator.
You should be using the logical AND operator, which is &&.
There might be other problems. Those are just two that I spotted.
This is from the Help file from my old QuickC for Windows:
Code: |
Syntax: size_t strspn( char *string1, char *string2 );
Returns: The position of the first nonmatching character in string1.
/*
STRSPN.C: This program uses strspn to determine the
length of the segment in the string "cabbage" consisting
of a's, b's, and c's. In other words, it finds the first
non-abc letter.
*/
void main()
{
char string[] = "cabbage";
int result;
result = strspn( string, "abc" );
printf( "The portion of '%s' containing only a, b, or c "
"is %d bytes long\n", string, result );
}
|
|
|
|
Schneider
Joined: 20 Nov 2006 Posts: 8
|
|
Posted: Tue Jan 23, 2007 6:34 am |
|
|
the strspn() is working fine. The only problem in the function is that I'm not obtaining the last char in the parameter string.
when I call the function, i get this feedback on the terminal screen:
assuming that the parameter passed to the function is #CMD:123
strPar: #CMD:123
strCom: #CMD:
Match: 5
Index: 7
Close: ??? (sometimes show some garbadge)
this should return false
and if the parameter passed to the function was #CMD:123;
the function should return true
i don't know why the 'Close' character is not returned when i try to get the *pStr[iTmp] value.
I realy missed the bitwise AND. I will try with the &&, butt the biggest problem is beffore the AND test. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Jan 23, 2007 7:06 am |
|
|
Try this:
Code: |
boolean isCmd(char *pStr)
{
char sTmp[] = {"#CMD:"};
int iTmp;
iTmp = (strlen(pStr)-1);
printf("strPar: %s\r\n", pStr);
printf("strCom: %s\r\n", sTmp);
printf("Match: %u\r\n", strspn(pStr, sTmp));
printf("Index: %u\r\n", iTmp);
printf("Close: %c\r\n", pStr[iTmp]);
if ((strspn(pStr,sTmp)==5) && (pStr[iTmp] == ';'))
{
return true;
}
else
{
return false;
}
}
|
char *pStr[] is an array of pointers to strings which is not what you have. You just need a pointer to a string |
|
|
Schneider
Joined: 20 Nov 2006 Posts: 8
|
|
Posted: Tue Jan 23, 2007 11:41 pm |
|
|
you're the best dude.. the problem is solved.. thanks a lot.. |
|
|
|
|
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
|