cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_UARTEx_ReceiveToIdle does not receive data correctly through RS485 IC

MÇETİ.1
Associate III

I'm using a rs485 IC. I transmit and receive data through it. When I send small amount of data like 10-20 there is no problem but when I try to receive more than 40 HAL_UARTEx_ReceiveToIdle makes nonsense. It receive data different then what I send or does not receive all of the buffer. Actually, I've never face a problem like this before. When I hit the breakpoint to see if it is calls callback, it does not. Also, it does not do the same thing when it only receive. I replaced it with HAL_UART_ReceiveDMA and it works well. I spend several days to understand the problem. Is anyone know why it happened, I'm really wondering the problem. I put my receiver code bellow. 

Thank you in advance for your help

void uartReceive(){
HAL_GPIO_WritePin(RS485_EN_GPIO_PORT, RS485_EN_Pin, GPIO_PIN_RESET);
HAL_UARTEx_ReceiveToIdle(&huart1, receiveBuff, 45);
}

void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size){
if(huart->Instance == USART1){
HAL_UARTEx_ReceiveToIdle(&huart1, receiveBuff, 45);
}

}
3 REPLIES 3
TDK
Guru

> HAL_UARTEx_ReceiveToIdle(&huart1, receiveBuff, 45);

That code is nonsense and won't even compile. HAL_UARTEx_ReceiveToIdle takes 5 arguments.

https://github.com/STMicroelectronics/stm32f4xx_hal_driver/blob/bfa4a8fde4fa5141619be006df8ef3e08265bd65/Src/stm32f4xx_hal_uart.c#L1588

HAL_UARTEx_ReceiveToIdle doesn't call HAL_UARTEx_RxEventCallback.

Here's an example of how to use HAL_UARTEx_ReceiveToIdle_DMA, if that's what you wanted.

https://github.com/STMicroelectronics/STM32CubeF4/blob/9e7d2aa86197059fc29cf89931d46d0ab64e6081/Projects/STM32446E-Nucleo/Examples/UART/UART_ReceptionToIdle_CircularDMA/readme.txt#L63

 

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

Oh, I'm sorry, typed wrong it here. It should be HAL_UARTEx_ReceiveToIdle_DMA. But this is not my problem, as I said above when I use it for only receiving there is no problem but when I want to transmit and receive data something goes wrong and I want to learn this. This is not only my problem actually. When I ask someone, also he said to me that he faced with same problem and suggest to use HAL_UART_Receive_DMA. How it could receive data wrong or missing. I use 3 RS485 ICs and others do not do this. I changed the connection to if it is about IC but the result was the same. Let's assume these are the data sent, data[0] = 0x45 and data[1] = 0x75, I see in the buffer data[0] = 0x13 and data[1] = 0xAA. I expect to receive 40 bytes but it receives 27 bytes.  My baudrate is correct, connections are ok, I watch the line with logic analyzer to see if data is sent. Also, transmits data correctly. I really wonder why this is happening.

Your HAL_UARTEx_RxEventCallback does not test for Half completed callback or Full completed callback. So if you're expecting 45 bytes you're going to get an interrupt at 23 bytes, assuming all the data is streamed with no idle time. Then you don't set a flag so how do you know if you even received any data?

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.