Skip to main content
DCtech
Associate III
May 6, 2021
Question

RS485 Receive Buffer Corruption

  • May 6, 2021
  • 1 reply
  • 1345 views

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__);

   }

  }

This topic has been closed for replies.

1 reply

TDK
Super User
May 6, 2021

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
DCtechAuthor
Associate III
May 6, 2021

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?

TDK
Super User
May 6, 2021

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""."