AnsweredAssumed Answered

UART communication not working properly

Question asked by Vaclav Chrascina on Apr 20, 2018
Latest reply on Apr 24, 2018 by Vaclav Chrascina

Hello,

I am trying to cummunicate with device throught UART, but communication is not working reliably. I am trying to read data continuosly by transmitting request in infinite loop, but I receive incomplete or currupted message. Could you give me some some advice what am I doing wrong?

 

Here is my code:

 

int DataRecieved = 0;

 

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(&huart3, (uint8_t*)aTxBuffer, 2) != HAL_OK)
        {
            while(1);
        }
        while(DataRecieved != 0);
        if(HAL_UART_Receive_IT(&huart3, (uint8_t*)aRxBuffer, 4) != HAL_OK)
        {
            while(1);
        }
        while(DataRecieved == 0);

 

         //Do something with received data

  }

 

Here are my callback functions:

 

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;
}

 

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;
}

Outcomes