0

I was trying UART using STM32F407V6T6 and CubeMx.

My UART is working fine. The problem I'm getting while comparing the buffer: I am using strstr() to check that my buffer contains valid substring or not.

Here is the code:

uint8_t buff[10];

int main(void) {
    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();
    MX_USART2_UART_Init();
    Green_Blink(100);
    Orange_Blink(100);
    Blue_Blink(100);
    Red_Blink(100);

    __HAL_UART_ENABLE_IT(&huart2, UART_IT_TC);
    __HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);
    HAL_Delay(1000);

    while (1) {
        HAL_UART_Transmit_IT(&huart2, (uint8_t *)"AT\r\n", 5);
        Orange_Blink(100);
        HAL_Delay(1000);
        HAL_UART_Receive_IT(&huart2, buff, 10);
        buff[9] = '\0';
        if (buff[6] == 'O' && buff[7] == 'K') {
            Green_Blink(1000);    //Blinks
        }
        if (strstr((char*)buff, "OK")) {
            Red_Blink(1000);      //Doesn't blink
        }
        Clear_Buffer((char*)buff);
    }
}

Here what I am doing is I have connect my GSM Module Sim800 and I send AT. After debugging my code I found that buff[6] = 'O' and buff[7] = 'K'. And while checking that I could blink the led.

        if (buff[6] == 'O' && buff[7] == 'K') {
            Green_Blink(1000);    //Blinks
        }

But when I try function strstr() It doesn't return anything.

        if (strstr((char*)buff, "OK")) {
            Red_Blink(1000);   //RED LED DOENS'T Blink
        }

At first I thought my array buff is not ending with \0. So I did this

     buff[9] = '\0';

But nothing changed.

Any suggestions why it's not working with strstr().

Thanks in advance.

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
sourav maity
  • 23
  • 1
  • 7

1 Answers1

1

From your observations and analysis, buff does contain OK at offset 6 and is null terminated at offset 9.

If strstr does not find OK in buff, a possible explanation is buff may contain another null terminator before offset 6 and OK is not present before this null terminator.

buff is initialized to all bits 0, so any element that is not changed is a null terminator. Is it also possible that HAL_UART_Receive_IT store null bytes into buff? null bytes are sometimes used as padding in serial transmissions.

It is also possible that the C library function strstr does not work on your target platform. I once had an inexplicable streak of bugs from faulty string functions on embedded ST platforms with their toolset.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Would the downvoter care to explain? – chqrlie Aug 13 '18 at 21:43
  • Do you still worry? Some people get much more satisfaction when DV than when they UV. Of course your answer is correct and the buff **must** contain zero before the char `O`. BTW OP has a hardware debugger built int in his board and it is sortable in 30 seconds - it enough to hover over the buff when debugging. – 0___________ Aug 13 '18 at 21:46
  • @P__J__: unless the C library `strstr` does not work, which is very well possible given the target. – chqrlie Aug 13 '18 at 21:48
  • `strstr` works very well in all gcc based ARM toolchains I know. So it is very unlikely `strstr` to do not work. I do ARM uCs programming for years and I have not found any faulty `strstr` implementations. BTW I UVed. – 0___________ Aug 13 '18 at 21:52
  • @P__J__: as I said, it did happen to me once on an embedded ST platform with their gcc based toolset. It might have been caused by a bogus configuration, but if was very upsetting to realize the origin of the problem: using custom replacements for `strcpy` and `strcat` fixed the problem. – chqrlie Aug 13 '18 at 21:54
  • Seeing the posted code (and no debug attempts ) I more suspect that the rewritten `strstr` works just incidentally :), and only in this case – 0___________ Aug 13 '18 at 21:56
  • 1
    @P__J__: I agree that the code from robomart.com is horrible, lacks indentation, has a innate function name, uses `NULL` for `'\0'` and is downright incorrect for `strstr("", "")`, but it seems to work for the OPs case. – chqrlie Aug 13 '18 at 21:59
  • Please check the question i have edited the whole question as that question was not even helpful for others – sourav maity Aug 14 '18 at 12:48
  • 1
    The comment from @sourav is now out of date, since it refers to the question being replaced by a completely new one. We don't allow that here, so it has been rolled back. – halfer Aug 14 '18 at 14:50