-4

I was trying UART using STM32F407V6T6 and CubeMx.

As I have posted some problems with strstr() function. Here is some new problems.

Here is the code:

char rxBuff[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);
  while (1)
  {

        HAL_UART_Transmit_IT(&huart2,(uint8_t*)"hello\r\n",10);
        HAL_Delay(1000);
        Orange_Blink(100);
        if (strstr(rxBuff,"hi"))
        {

        }
   }
}

Here what i am doing is I am transmitting hello\r\n using Transmit interrupt. After transmitting I am checking for certain values like hi in rxBuff[10].

In this particular code i am not receiving anything I just created the buffer and checking if it contains hi or not.

Note that while transmitting I am just sending 7 bytes of data. But in HAL_UART_Transmit_IT() at place of size I am giving 10.

HAL_UART_Transmit_IT(&huart2,(uint8_t*)"hello\r\n",10);

After flashing this code to my stm32 I can see in terminal various output like

hello\r\nhi

If I change the txt from hi to some other thing like planets, then the output will be like

hello\r\npl

But when I give the perfect size like this

HAL_UART_Transmit_IT(&huart2,(uint8_t*)"hello\r\n",7);

I get the actual output i should be getting

hello\r\n

Transmitting the data via UART and finding if substring is present in some buffer is totally different tasks.

Any suggestions why this is happening?

P.S: Basically my project is with GSM MODULE SIM800c and STM32f407VGt6 I want to send various AT commands from the stm32 and check the response so that I can work accordingly.

Previously I have tried this project with AVR series mcus where I could send sms,receive a particular sms,send data to server using post and get methods and everything worked completely fine.

After moving to ARM I am not getting how STM32 is behaving.

halfer
  • 19,824
  • 17
  • 99
  • 186
sourav maity
  • 23
  • 1
  • 7
  • 2
    Please don't repost [your question](https://stackoverflow.com/questions/51829039/uart-transmit-and-strstr-gives-wierd-outputs) – André Kool Aug 14 '18 at 14:39

3 Answers3

5

This has nothing to do with the strstr function. You are telling HAL_UART_Transmit_IT to go read beyond the end of the array (string literal) "hello\r\n". What it will find there is anyone's guess, it's undefined behavior.

Lundin
  • 195,001
  • 40
  • 254
  • 396
2

On this line:

HAL_UART_Transmit_IT(&huart2,(uint8_t*)"hello\r\n",10);

you are telling it to transmit 10 bytes. "hello\r\n" is only 7 bytes + NULL terminator, so it also sends the next thing in memory, in this case the first 2 bytes of that other string that you're using in strstr.

Thomas Jager
  • 4,836
  • 2
  • 16
  • 30
1

Your "hello\r\n" string was placed into some read-only area of your program. This is the same for your hi or planets strings. Compiler put these strings one after another in memory (or better to say, linker) and since you did not specify exact memory length of your string, you printed out if valid are.

You were lucky that both strings were in memory one after another.

h e l l o \r \n \0 p l a n e t s.

Since you printed 10 characters, you printed hello\r\n, followed by NULL termination and then pl, together 10 characters.

Lesson learned: Do not access to memory you are not allowed to. You were allowed to access only strlen("hello\r\n") + 1 bytes.

unalignedmemoryaccess
  • 7,246
  • 2
  • 25
  • 40