cancel
Showing results for 
Search instead for 
Did you mean: 

RS485 Receive Buffer Corruption

DCtech
Associate II

I have two device on the Rs485 line, one of them transmit the data, another receiving the data.

Transmitter, transmit rs485TxBuffer, it is include `uint8_t rs485TxBuffer[2]={0xAA,0xBB}`.I expect the receiver fill the receive buffer with these data. But I have a problem in here.

My problem is: 

I get receiver side very interesting printing value, I get rxbuffer like this:

  Received Message From Rs485 line : aa, 00

  Received Message From Rs485 line : bb, 00

  Received Message From Rs485 line : aa, 00

  Received Message From Rs485 line : bb, 00

  Received Message From Rs485 line : aa, 00

But I expected the message get like this

   aa,bb

These are my codes:

  uint8_t rs485TxBuffer[2]={0xAA,0xBB};

  uint8_t rs485RxBuffer[2] ={0x00, 0x00};

Transmitter codes:

 while(1){
 
   
 
      RS485_Set_Transmit_Mode();
 
       while(HAL_UART_Transmit_IT(&huart2, (uint8_t*)rs485TxBuffer, 2) != HAL_OK)
 
       {
 
         // ...
 
       }
 
       uartReady = RESET;
 
      while (uartReady != RESET);
 
  }
 
  void RS485_Set_Transmit_Mode(void)
 
  {
 
   HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, SET); // DE pin
 
  }
 
   
 
  void RS485_Set_Receive_Mode(void)
 
  {
 
   HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, RESET); // RE pin
 
   HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, RESET); // DE pin
 
   
 
  }
 
   
 
  void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
 
  {
 
   
 
  uartReady = SET;
 
  }
 
   
 
  void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
 
  {
 
   
 
  uartReady = SET;
 
   
 
  }

Receiver codes:

    while (1)
 
    {
 
        RS485_Set_Receive_Mode();
 
        HAL_Delay(1);
 
        while (HAL_UART_Receive_IT(&huart2, (uint8_t*)rs485RxBuffer, 2) != HAL_OK)
 
        {
 
           // ...
 
        }
 
        SerialPrint("Received Message From Rs485 line : %02x, %02x\n",rs485RxBuffer[0],rs485RxBuffer[1]);
 
    }
 
  void RS485_Set_Transmit_Mode(void)
 
  {
 
   HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, SET); // DE pin
 
  }
 
   
 
  void RS485_Set_Receive_Mode(void)
 
  {
 
   HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, RESET); // RE pin
 
   HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, RESET); // DE pin
 
   
 
  }
 
   
 
  void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
 
  {
 
   
 
  uartReady = SET;
 
  }
 
   
 
  void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
 
  {
 
   
 
  uartReady = SET;
 
   
 
  }

When I didnt use the delay in my function, I didnt get correct values.

How can I solve the receiving values?

  /* USART2 init function */

  static void MX_USART2_UART_Init(void)

  {

   

   huart2.Instance = USART2;

   huart2.Init.BaudRate = 115200;

   huart2.Init.WordLength = UART_WORDLENGTH_8B;

   huart2.Init.StopBits = UART_STOPBITS_1;

   huart2.Init.Parity = UART_PARITY_NONE;

   huart2.Init.Mode = UART_MODE_TX_RX;

   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;

   huart2.Init.OverSampling = UART_OVERSAMPLING_16;

   if (HAL_UART_Init(&huart2) != HAL_OK)

   {

    _Error_Handler(__FILE__, __LINE__);

   }

  }

5 REPLIES 5
TDK
Guru

Probably a few things happening here, but one of them is that you don't wait for the bytes to be received before printing them

        while (HAL_UART_Receive_IT(&huart2, (uint8_t*)rs485RxBuffer, 2) != HAL_OK)
         {
            // ...
         }
         SerialPrint("Received Message From Rs485 line : %02x, %02x\n",rs485RxBuffer[0],rs485RxBuffer[1]);

Blocking mode (HAL_UART_Receive) would be more appropriate here. Or wait until the UART state is ready again.

If you feel a post has answered your question, please click "Accept as Solution".
DCtech
Associate II

Thank you for your response,

How can I check, UART steady ready again.

1 ) Why the _IT mode not good for this type of communication? What is the difference between ?

2) If I use the both device transmitter and receiver ( request- respond mode) , then should I choose IT mode?

I would follow examples in the appropriate CubeMX repository. Once you get one of those to work and understand it, progress to writing your own code. You don't provide the chip you are working with, otherwise I would have provided a link to an example.

> What is the difference between ?

Interrupt mode is non-blocking. Blocking mode is, well, blocking. Google "blocking vs nonblocking calls".

> If I use the both device transmitter and receiver ( request- respond mode) , then should I choose IT mode?

Yes, or DMA. Or non-blocking for one direction and blocking for the other.

If you feel a post has answered your question, please click "Accept as Solution".

In the CubeMX repository, should I look for UART instances? Is there a custom example for Rs485?

The chip doesn't do RS485 natively. That signal must be translated to a single ended signal for the chip to read. Look at UART examples.

If you feel a post has answered your question, please click "Accept as Solution".