cstm32strstr

strstr issue in C


I'm using strstr to search for the beginning and end of brackets so I can filter out incorrect entries sent on the UART from information I actually want/need.

I have my UART interrupt storing characters into an array and incrementing the counter. Below is the code in my main function:

  volatile char     UART_RX_Buffer[128]; // updated in UART
  volatile uint16_t UART_Byte_Counter = 0; // updated in UART
  
  while (1)
  {
        if(strstr(&UART_RX_Buffer[0], "{") != NULL)
        {
            if(strstr(&UART_RX_Buffer[0], "}") != NULL)
            {
                char *ret;
                int startindex;

                ret = strstr(&UART_RX_Buffer[0], "{");
                startindex = (ret - UART_RX_Buffer);

                // Clear out UART array and counter
                char Empty_Buffer[128] = {0};
                memcpy (UART_RX_Buffer, Empty_Buffer, sizeof(UART_RX_Buffer));
                UART_Byte_Counter = 0;
            }
        }
  }

So if I send it, say 012{345,678}, it will set the startindex as 3 (which is what I want). It's successfully entering the loop, setting startindex, and clearing the UART_RX_Buffer and counter correctly.

What's odd is it only works the first time through. If I send it a second time, I can see the buffer is holding the same data as the first time around . . . but it's not entering the first loop, recognizing the "{" character. I can see an issue if I'm not doing things correctly with the UART interrupt and the buffer isn't being filled correctly . . . but that doesn't appear to be the case. I don't really understand why I would get different results with what appears to be the same data (especially if I can confirm the data is in the UART buffer when trying to run that line).

Clearly I'm missing something. Any ideas what might be causing this? If it helps, it's an STM32L4R5ZI chip (though I would assume this is a C issue and not device specific). I'll include screenshots from the debugger all the same in case it helps. Thanks in advanceenter image description here!


Solution

  • strstr issue in c

    No, it is always your code issue. strstr, the compiler, linker and the silicon are OK.

    (especially if I can confirm the data is in the UART buffer when trying to run that line).

    But it is not the data you think you have :)

    enter image description here The C sting is empty as it starts with the null terminating character. strstr sees the empty string. Your buffer-filling algorithm is definitely broken.

    I can even tell you that you have not updated the index or pointer of the current buffer position as '0' goes to the place where the previous transition ended: enter image description here