Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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
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:

horace1

Advanced Member level 5
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,071
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

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,274
Trophy points
1,413
Location
Southwest, USA
Activity points
62,512
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

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
19,707
Helped
4,340
Reputation
8,689
Reaction score
4,303
Trophy points
1,393
Activity points
130,467
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
 

pic.programmer

Advanced Member level 3
Joined
Aug 19, 2015
Messages
773
Helped
141
Reputation
284
Reaction score
140
Trophy points
43
Activity points
7,542
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.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top