strstr() function bugs

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey Guys,

Can someone help me with this small gcc problem.

Im connecting to gsm modem and trying to check when i receive a new sms. at this stage i ma just checking data between the microcontroller and computer terminal.

a small issue with the strstr() subroutine... When a correct comparion is made it enters the if loop. But it also enters when its comparing with a carriage return(enter key pressed).

can some one shine some light.

Thanks

void CheckForSms()
{
	
	char ser[]="+CMTI";
	char s[64];
	int n;
	n = UART_gets(s, 6);
	s[strlen(s)-1] = 0;
	if (strstr(ser, s))
		{
		UART_puts_P(PSTR("\r\nFOUND at index "));
		n = UART_gets(s, 6);
		n = UART_gets(s, 5);
		UART_puts_P(PSTR("\r\n"));
		UART_puts_P(PSTR("\r\nAT+CMGR="));
		UART_puts(s);
		UART_puts_P(PSTR("\r\n"));
		}
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi rodney,

i would try:

void CheckForSms()
{

  char ser[]="+CMTI";
  char s[64];
  int n;
  n = UART_gets(s, 6);
  if (n>0)
  {
	 s[n]=0;
	 if (strstr(s, ser))
	 {
		UART_puts_P(PSTR("\r\nFOUND at index "));
		n = UART_gets(s, 6);
		n = UART_gets(s, 5);
		UART_puts_P(PSTR("\r\n"));
		UART_puts_P(PSTR("\r\nAT+CMGR="));
		UART_puts(s);
		UART_puts_P(PSTR("\r\n"));
	 }
  }
}

Angelo

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are you sure that the CarriageReturn gets through to the array s? Could it be that it is stripped out and that the lenght of the string in s is zero?

From the strstr documentation

Quote:
If s2 points to a string with zero length, the function shall return s1.

( http://www.opengroup.org/onlinep... )

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes thats one thing solved

Thankyou Spectrum and JohanEkdahl.

Much appriciated

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
s[strlen(s)-1] = 0; 

Just thought I'd mention that this piece of code would not work well for adding a zero termination to the string s since strlen relies on the string being zero terminated in order to give you a length.