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

IF statement not works

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



Joined: 07 Nov 2008
Posts: 60

View user's profile Send private message

IF statement not works
PostPosted: Fri Feb 13, 2009 9:16 am     Reply with quote

Dear user, I need of your help since IF statement in my code doesn't work; could someone help me how to write in correct form the IF statement?
Thanks in advance and regards

Code:
#include <input.c>
char temp[6];


#int_rda
void Serial_isr()
   {
   char string[6], term[3], *ptr;
   char *valore;
   char *comando;
   
   printf("Enter string: ");
   get_string(temp, sizeof(temp));
   printf("\n\rReceived: %s \n\r", temp);
   strcpy(string,temp);
   strcpy(term,",");
   ptr = strtok(string, term);
   comando=ptr;
   while(ptr!=0) {
      //puts(ptr);
      valore=ptr;
      ptr = strtok(0, term);
   }
   if(comando!="55")
   {
   printf("\n\rComando= %s \n\r", comando);
   printf("\n\rValore= %s \n\r", valore);
   }
   }
Ttelmah
Guest







PostPosted: Fri Feb 13, 2009 10:53 am     Reply with quote

The reason the 'if' doesn't work, is that you cannot compare strings with the '!=' test. This _only_ applies for numeric values.

However, consider doing this all a completely different way. You are really 'abusing' the point of the interrupt. The idea of a serial ISR, should be to get just the _one_ character that is waiting in the hardware buffer. In your ISR, you are getting multiple characters, and outputting even more. Since potentially you are in the ISR, for 12 character times, before you even read a character, the hardware buffer (just two characters), can easily be overloaded, resulting in loss of characters (at the least), and hanging the UART (at the worst).

Do a search here about this. Unless you really know what you are doing, you should keep interrupt handlers short. If you want a 'input routine' triggered by the arrival of the first character, use something like the ex_sisr code, but add a flag, so that as soon as the interrupt is triggered, your main code calls the actual input routine. Avoid doing complex tasks like this in the ISR.

Best Wishes
nicotec



Joined: 07 Nov 2008
Posts: 60

View user's profile Send private message

PostPosted: Fri Feb 13, 2009 12:42 pm     Reply with quote

above pic24 has 4 character buffer, so there are no risk to lost since I send only 5 char not so quickly; could you hel me to solve IF statement problem?
thanks
nicotec



Joined: 07 Nov 2008
Posts: 60

View user's profile Send private message

PostPosted: Fri Feb 13, 2009 1:17 pm     Reply with quote

Please provide me a solution on if statement; i tried a lot of combination like =='55' ="55" but since i'm new user i need of your help.
thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Feb 13, 2009 1:46 pm     Reply with quote

http://www.ccsinfo.com/forum/viewtopic.php?t=36705&highlight=strcmp
http://www.ccsinfo.com/forum/viewtopic.php?t=34302&highlight=strcmp
nicotec



Joined: 07 Nov 2008
Posts: 60

View user's profile Send private message

PostPosted: Fri Feb 13, 2009 2:11 pm     Reply with quote

Thanks, so I can change my code as follows?

Code:
#include <input.c>
char temp[6];


#int_rda
void Serial_isr()
   {
   char string[6], term[3], *ptr;
   char *valore;
   char *comando;
   char *pan_chk;

   strcpy(pan_chk,"55");
   
   printf("Enter string: ");
   get_string(temp, sizeof(temp));
   printf("\n\rReceived: %s \n\r", temp);
   strcpy(string,temp);
   strcpy(term,",");
   ptr = strtok(string, term);
   comando=ptr;
   while(ptr!=0) {
      //puts(ptr);
      valore=ptr;
      ptr = strtok(0, term);
   }
   if (strcmp(comando,pan_chk)!=1)
   {
   printf("\n\rComando= %s \n\r", comando);
   printf("\n\rValore= %s \n\r", valore);
   }
   }


What do You think?
Regards
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Feb 13, 2009 2:30 pm     Reply with quote

Quote:
if (strcmp(comando,pan_chk)!=1)

There is no guarantee that strcmp() will return a value of 1.
If the strings are equal, it returns a value of 0.
If they are not equal, it returns a positive or negative number.

You need to get a book on C, or read an online tutorial on C.
In other words, we don't want to teach how to use every ordinary
C function such as strcmp(). You need to get a book about it.
nicotec



Joined: 07 Nov 2008
Posts: 60

View user's profile Send private message

PostPosted: Fri Feb 13, 2009 2:33 pm     Reply with quote

Sorry to disturb, I have already ordered a book and waiting it to receive.
Regards
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