Sterling
Sterling
DIIDevHeads IoT Integration Server
Created by Sterling on 8/6/2024 in #firmware-and-baremetal
How to Receive Variable-Length Data Over RS485 Using HAL_UART_Receive_IT on STM32F4?
Hello everyone, So I have this custom STM32F4 MCU board and need to receive various data sequences from a Master board over an RS485 network. The lengths of the data sequences are variable, such as 10 bytes, 25 bytes, etc. I need to handle each sequence and respond accordingly. How can I effectively use the HAL functions HAL_UART_Receive_IT and HAL_UART_RxCpltCallback to properly receive variable-length data? I noticed that the third argument of HAL_UART_Receive_IT requires a fixed number of bytes, like this:
HAL_UART_Receive_IT(&huart1, &uart1_rx_byte, no_of_bytes);
HAL_UART_Receive_IT(&huart1, &uart1_rx_byte, no_of_bytes);
Can you suggest a good implementation for this? The frame format for receiving data is as follows:
| Start byte | Slave Addr | Byte count | Func. ID | Data1 | Data2 | ... | Data n | Checksum byte 1 | Checksum byte 2 |
| Start byte | Slave Addr | Byte count | Func. ID | Data1 | Data2 | ... | Data n | Checksum byte 1 | Checksum byte 2 |
Where Byte count represents the number of bytes from Func. ID to Checksum byte 2.
6 replies