-1
\$\begingroup\$

This was my first topic, RS485 Tutorial Issue. I referenced because of the schematics.

I solved the problem in above topic, I forgot to pin settings for DE and RE pins.

I have a different problem now.

I have two devices on the Rs485 line, one of them transmits the data, the other receives the data.

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

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__);
  }
}
\$\endgroup\$
8
  • \$\begingroup\$ Can you share the configuration of huart2? \$\endgroup\$
    – po.pe
    May 6, 2021 at 9:24
  • \$\begingroup\$ I added end of the topic \$\endgroup\$
    – gogogo
    May 6, 2021 at 9:26
  • 1
    \$\begingroup\$ Why do you use the _IT implementations of the UART interface instead of the blocking ones? \$\endgroup\$
    – po.pe
    May 6, 2021 at 9:27
  • \$\begingroup\$ I've seen the example used in this way. But since I am new, I am not good at this. How should I use it? \$\endgroup\$
    – gogogo
    May 6, 2021 at 9:29
  • \$\begingroup\$ In your Transmit code you while (uartReady != RESET); to wait for the transmit to finish, but you don't do the same in your receive code. This results in your receiver printing the message before the receive is complete. Using the non-_IT versions of the HAL functions would work just as well here and you wouldn't need the interrupt callback functions or the while (uartReady != RESET); loops. \$\endgroup\$
    – brhans
    May 6, 2021 at 12:03

1 Answer 1

0
\$\begingroup\$

Yo have to start the receive with interrupt, with set timeout - a timeout timer that restarts at received char. So you should start receive and this should trigger an interrupt when last character is received + timeout_char / or if no character is received + timeout_recv

Then you you have to parse the received telegram by using receive complete callback.

You do wait in endless loop, but the interrupt will call a callback, so no need to wait in loop.

\$\endgroup\$
3
  • \$\begingroup\$ How can I do, receive with interupt with set timeout ? \$\endgroup\$
    – gogogo
    May 6, 2021 at 10:37
  • \$\begingroup\$ it sounds very complicated, could you give an example,please ? \$\endgroup\$
    – gogogo
    May 6, 2021 at 11:34
  • \$\begingroup\$ @gogogo have a look github.com/MaJerle/stm32-usart-uart-dma-rx-tx \$\endgroup\$ May 6, 2021 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.