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

strsrt function query

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



Joined: 05 Sep 2007
Posts: 252

View user's profile Send private message

strsrt function query
PostPosted: Fri Mar 13, 2015 1:24 am     Reply with quote

Hi i write a code to receive the stream and compare it with a predefined stream and then do the process.
The stream which is coming is
Quote:
\r\nOK\r\n

The code for receiving that stream is as follows

Code:

#include <18f26k22.h>
#fuses HSH,NOPLLEN,NOBROWNOUT
#use delay(crystal=20M)
#use rs232(baud=9600 , UART2, stream=GSM)
#use rs232(baud=9600 , UART1, stream=PC)
                                                           
#include <string.h>
#define BUFFER_SIZE 50   

char c;
char buff[buffer_size];
int a_count=0, n=0;                           
char OK[]={"OK"}; 
                                         
#INT_RDA2                                 
void data_received(void)                             
{
   c=fgetc(GSM);  //receive character in a variable                     
   buff[n]=c;     //copy that character in buffer                                                   
   n++;           //increament the index                           
                                             
   if(c==0x0A) a_count++;//increament whenever '\n' character receives.                                     
}                               
                                       
void main(void)                             
{                                                                 
   enable_interrupts(INT_RDA2);           
   enable_interrupts(GLOBAL);                                                                       
                                             
   do                                             
   {                                   
      if(a_count==2)    // '\n' received two times?             
      {                                               
         n++;           //increament index for adding null character                       
         buff[n]='\0';  //add null character at the end of stream                                         
                                   
         if(strstr(buff,OK)!=NULL)  //if steam matches
         {                                                     
            output_toggle(PIN_C0);
            a_count=0;                             
            memset(buff,0,BUFFER_SIZE);
            n=0;                                         
         }                               
      }             
   }while(TRUE);             
}


The code is working fine.
I have a question. Is it necessary to have a null character at the end of the stream for comparison? because when i comment out the following lines the code also works.
Code:

//!         n++;           //increament index for adding null character                       
//!         buff[n]='\0';  //add null character at the end of stream                                         


Is there something i am missing?
Ttelmah



Joined: 11 Mar 2010
Posts: 19326

View user's profile Send private message

PostPosted: Fri Mar 13, 2015 2:00 am     Reply with quote

It's necessary, if you _don't_ receive the right string.....

strstr, returns when it finds a match, _or_ when it hits the end of the string. If you don't have the terminator, and the match fails, the function will disappear off searching through the entire memory. Normally it'll eventually return, since there probably _will_ be a zero somewhere, but it'll take significant time, and if there isn't a zero, it'll carry on, and may well eventually crash...

Adding the terminator, limits the search to this point, so the function will return OK (with a 'match fail' status), if there is no match.

In fact without it, there is a very good chance it'll accidentally return 'match good', since if there isn't another zero in memory, it is likely to meet the actual search string and match itself....
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Fri Mar 13, 2015 7:58 am     Reply with quote

Quote:

\Is there something i am missing?


another thing that jumps out is the linear receive buffer.

what happens if received character count increments n>49 ?
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