cancel
Showing results for 
Search instead for 
Did you mean: 

search for matching string on the other string

mhdizgah
Associate II
Posted on August 31, 2015 at 07:49

hi every body

i have a problem with strstr function, i receive characters from uart with interrupt & save them on the n length array(buffer array), then in main loop i check buffer with strstr function from string.h for finding a special arrayof characters(for example'' hello''),it works fine for the first time but some time it dosent work and without receiving that array of string it act like when find matching. sample code : interrupt handler ,when character received,it save that in the buffer:

Code:

void USART1_IRQHandler(void)
{
/* RXNE handler */
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
Rx_Buffer[Counter_Num1]=(char)USART_ReceiveData(USART1);
Counter_Num1++;
if(Counter_Num1==100)
{
Counter_Num1=0;
}
}
}

main loop: in some primary matching it works fine (when i type ''hello'',it return ''1234'') but after some tries it permanently return ''1234'' without matching

while(1)
{
//--------------------------------------
do
string_pointer = strstr (Rx_Buffer, ''hello'');
while(string_pointer==NULL);
USART_SendData(USART1, '1');
delay_ms(50);
for(loop_counter=0;loop_counter<100;loop_counter++)
{
Rx_Buffer[loop_counter]=' ';
}
string_pointer=NULL;
USART_SendData(USART1, '2');
delay_ms(50);
USART_SendData(USART1, '3');
delay_ms(50);
USART_SendData(USART1, '4');
delay_ms(50);
//-----------------------------------
}

i used stm32 & keil thanks for any help
2 REPLIES 2
Aurélien f
Senior
Posted on August 31, 2015 at 09:37

Hello,

If Rx_Buffer is not well itnialized to zero (even if it is not), it should be safer to use strnstr i think:

string_pointer = strnstr (Rx_Buffer, ''hello'', 

sizeof(Rx_Buffer)

);

But i'm not sure this is the issue :)

Aurélien

Posted on August 31, 2015 at 14:18

String functions are VERY sensitive to the strings being properly NUL terminated. Otherwise the operations are unbounded.

Also WAIT for TXE before using USART_SendData(), it's the way to KNOW the buffer is empty so it's ready to accept a new character, and you don't need arbitrary software delays.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..