-3
\$\begingroup\$

I have a problem with strstr function, I receive characters from UART with interrupt & save them in the n length array(buffer array). Then in main loop I check the buffer with strstr function from string.h for finding a special sequence of characters(for example" hello"). It works fine for the first time but sometimes it dosen't work and without receiving that array of string acts like when find matching.

Sample code :

Interrupt handler, when a character is received, it saves 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 returns "1234") but after some tries it permanently returns "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 am using stm32 & keil.

\$\endgroup\$
  • 5
    \$\begingroup\$ "1234" is all that this code can print. Why do you expect this code to print anything other than "1234" to UART? \$\endgroup\$ – Nick Alexeev Aug 31 '15 at 5:54
  • \$\begingroup\$ I agree with @NickAlexeev this code seems like it's working fine. If it's not what you want, change it. Also have a look at using memcmp for comparing an incoming string with a stored string for -exactly- the same set of bytes. It must be exactly the same, or the function will return false (!= 0). \$\endgroup\$ – KyranF Aug 31 '15 at 6:25
2
\$\begingroup\$

Step 1: Make your do{}while() loop a do{}while() loop, in stead of a do and infinite while() loop if you want that behaviour to be predictable.

Step 2: Stop overwriting your 100 place string with spaces to blank it, as internal str functions look for a '\0' to stop. So they now walk into unknown memory once you have had one match. Set it all to '\0'. Google memset().

Step 3: Actually initialise the array when you begin to '\0' with memset (you happily don't show all your code, but I'm just going to be assuming here), or similar, so you actually know what will happen.

Step 4: Consider what happens when you start sending hello when the loop is at count 98. You might want to manage the numbering upon match as well.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.