search for matching string on the other string

Status
Not open for further replies.

dizgah

Member level 5
Joined
Nov 8, 2009
Messages
91
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
iran-8par
Visit site
Activity points
2,049
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
Code:
    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
 
Last edited:

you probably need to terminate the string when receiving characters
Code:
    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
    {
	Rx_Buffer[Counter_Num1]=(char)USART_ReceiveData(USART1);
	Counter_Num1++;
        Rx_Buffer[Counter_Num1]=0;    // terminate the string
	if(Counter_Num1==1023)
 

    V

    Points: 2
    Helpful Answer Positive Rating
I've only taken a brief look at your code, therefore there maybe other issues, however one statement particularly grabs my attention which I've highlighted in red:

Code:
 while(1)
   {
			//--------------------------------------
		[COLOR="#FF0000"]do[/COLOR]
		string_pointer = strstr (Rx_Buffer, "hello");
		[COLOR="#FF0000"]while(string_pointer==NULL);[/COLOR]

		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);
		//-----------------------------------
			
}

Once the string_pointer variable is set to NULL, code execution essentially stalls permanently executing that while loop in red, unless of course there is another ISR not shown which then changes the value contain in the string_pointer variable to something other than NULL.

Perhaps, a better strategy would be to enclose the code you do not want to run when a NULL is return and change the conditional:

From:
Code:
while(string_pointer==NULL);

To:
Code:
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);
}

Also, you appear to have a dangling "do" in your code, I can only assume there is a while() closing the do while loop missing from the code snippet.

You should also keep in mind, using software delays in conjunction with ISRs should typically be avoided, there usually other more appropriate and effective methods to regulate the speed of UART character transmission.



BigDog
 

    V

    Points: 2
    Helpful Answer Positive Rating
Hi,

I'm guessing, that you want transmit some instructions and parse it at the receiver side.

Then you should work with instruction delimiters.
Example:
Let's say the delimiter is [CrLf]
Now if your receive buffer includes data like this: "d 123; 45 [CrLf]Write 45; 34 [CrLf]Write 6"

Do a search on the first [CrLf], move the data to the parse_string.
Now receive buffer:"Write 45; 34 [CrLf]Write 6"
Parse_string:"d 123; 45 [CrLf]"
Check the parse_string only. Here you may find the data is incomplete, garbage. So delete it

Do a new search on the first [CrLf], move the data to the parse_string.
Now receive buffer:"Write 6"
Parse_string:Write 45; 34 [CrLf]"
Check the parse_string only. Here you may find the data is complete and you could process the instruction. Write data 34 to address 45

Do a new search on the first [CrLf]
No [CrLf] found. So the instruction is not yet complete. Leave the receive buffer unchanged.
Leave the parse routine and wait for new data to receive.

Klaus
 

After string matching is complete and proper action is taken based on match then clear the UART buffer and index.
 

    V

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…