2

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?

Here is my 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(&huart3, (uint8_t*)aTxBuffer, 2) != HAL_OK)
        {
            while(1);
        }
        while (UartReady != SET);
        UartReady = RESET;
        if(HAL_UART_Receive_IT(&huart3, (uint8_t*)aRxBuffer, 4) != HAL_OK)
        {
            while(1);
        }
        while (UartReady != RESET);
        //do somethink 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;
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;
}

Thank you very much

V.CHR.
  • 31
  • 1
  • 6

2 Answers2

0

The data you transmit, does it also appear at your RX-buffer, while you expect it not to?

Stian Skjelstad
  • 2,277
  • 1
  • 9
  • 19
0

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?

Not sure, but maybe it would help to toggle the DE pin not in the interrupt callback functions, and instead switch to "transmit mode" right before invoking HAL_UART_Transmit_IT() and then to switch back to receive mode after the transmission, or even after calling HAL_UART_Receive_IT(), to make sure it will catch the incoming bytes. Tiny delays do matter sometimes:

void set_transmit_mode(void)
{
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET);  // RS 485 transmit mode
}

void set_receive_mode(void)
{
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET); // RS 485 receive mode
}

{
    //...
    set_transmit_mode();
    if (HAL_UART_Transmit_IT(&huart3, (uint8_t*)aTxBuffer, 2) != HAL_OK) {
       // ...
    }
    while (UartReady != SET);
    //set_receive_mode();
    UartReady = RESET;
    if (HAL_UART_Receive_IT(&huart3, (uint8_t*)aRxBuffer, 4) != HAL_OK) {
        // ...
    }
    set_receive_mode();
    while (UartReady != RESET);
    //...
}
rel
  • 764
  • 5
  • 18