0

I send commands to the modem through USART1 and copy them to USART2. I copy the modem's response to USART2. USART2 is connected to the terminal and I see a lot of extraneous characters in it. And the line break does not work everywhere. Why is that? Code:

uint8_t TxData[64];
uint8_t RxData[64];
char GsmTest[10] = "AT\r\n";
char ActivateTxtMode[20] = "AT+CMGF=1\r\n";
uint8_t flag=1;

for(;;)
{
  HAL_UART_Transmit(&huart2, "SMS with GSM Module\r\n", 20, HAL_MAX_DELAY);
  HAL_UART_Transmit(&huart2, "Checking Module...\r\n", 20, HAL_MAX_DELAY);
  HAL_UART_Transmit(&huart2, "--------------------------------------------\r\n", 30, HAL_MAX_DELAY);

while(flag==1){
    HAL_UART_Transmit(&huart2, "STM32 request:\r\n", 20, HAL_MAX_DELAY);
    HAL_UART_Transmit(&huart2, GsmTest, strlen(GsmTest),HAL_MAX_DELAY);
    HAL_UART_Transmit(&huart1,(uint8_t *)GsmTest,strlen(GsmTest),HAL_MAX_DELAY);
    HAL_UART_Receive(&huart1, RxData, sizeof(RxData), 1000);
    HAL_UART_Transmit(&huart2, "Modems answer: \r\n", 20, HAL_MAX_DELAY);
    HAL_UART_Transmit(&huart2, RxData, strlen(RxData),HAL_MAX_DELAY);
    HAL_UART_Transmit(&huart2, "\r\n", 20, HAL_MAX_DELAY);

    if(strstr((char *)RxData,"OK")){
        HAL_UART_Transmit(&huart2,"Module Connected\r\n", 20, HAL_MAX_DELAY);
            flag=0;
        }
    else{
        HAL_UART_Transmit(&huart2,"Module Unconnected\r\n", 20, HAL_MAX_DELAY);
    }
    }

Terminal: enter image description here

Ping-247
  • 21
  • 3
  • You cant use `strlen` or `strstr` as modem response is not NULL character terminated. You cant read fixed size blocks as responses have different lengths. The best solution is to use interrupts, not polling. – 0___________ Aug 02 '22 at 21:31
  • 1
    See: https://community.st.com/s/question/0D50X0000AhPrkRSQS/haluarttransmit-with-haluartreceiveit-fails-if-rx-when-tx-not-completed You have UB. When you do: `HAL_UART_Transmit(&huart2, "STM32 request:\r\n", 20, HAL_MAX_DELAY);` you are appending garbage chars because 20 is longer than the string constant length. You do this in several places. You need a function like in the link. Also you ignore the return length from `HAL_UART_Receive`. There is no guarantee that it returns the response atomically – Craig Estey Aug 02 '22 at 22:05

0 Answers0