title: STM32 - RS485 request-response
body: <p>I am trying to send request to measuring device and receive it's response using UART with interrupts. However communication is unstable, I am receiving incomplete or corrupted responses. I am not sure but I think it's because switching driver enable signal. Could you look at my code and give me some advice what am I doing wrong?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>int main(void){
  
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_USART3_UART_Init();  
  
  HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET);               //RS 485 transmit mode
  
  while (1)
  {
  
          if(HAL_UART_Transmit_IT(&amp;huart3, (uint8_t*)aTxBuffer, 2) != HAL_OK)
          {
              while(1);
          }
          while (UartReady != SET);
          UartReady = RESET;
          if(HAL_UART_Receive_IT(&amp;huart3, (uint8_t*)aRxBuffer, 4) != HAL_OK)
          {
              while(1);
          }
          while (UartReady != RESET);
          //do somethink with received data
            }
  }
  </code></pre>
  
  <p>Here are my callback functions:</p>
  
  <pre><code>void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
  {
  /* Set transmission flag: transfer complete */
  HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_RESET);     //RS 485 receive mode
  //DataRecieved = 0;
  UartReady = SET;
  }
  
  void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
  {
  /* Set transmission flag: transfer complete */
  HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET);       //RS 485 transmit mode
  //DataRecieved = 1;
  UartReady = SET;
  }
  </code></pre>
  
  <p>Thank you very much</p>