cancel
Showing results for 
Search instead for 
Did you mean: 

Timeout for UART using Interrupt Rx/Tx

MFord.1
Associate III

Hi there,

I currently have a system where I am using a UART Rx/Tx in "interrupt mode". Right now I am receiving packets of indeterminant length so I have a "header" section that contains the size of the "body section".

Basically my code bounces back and forth between

  1. Read header HAL_UART_Receive_IT(header size)
  2. Get length of body from header
  3. Read in body HAL_UART_Receive_IT(body size)
  4. repeat for next commands

I want to implement an error catch in between the reading of the header and body. In case the length bytes in the header get corrupted and now my second receive is waiting until 5 million bytes are received.

I want to use a timeout in between the two receives in case the "body data" never completely comes. Do the HAL drivers have this feature built in with the RX/TX interrupt functions?

I see a timeout parameter in the blocking HAL_UART_Receive() and Tx functions, but not in the HAL_UART_Receive_IT() functions.

Would I have to use a timer to create my own timeout? Start it after the header is read in and reset once the body comes in? If a body doesn't come in, start listening for another header.

Thank you for your time

1 REPLY 1
TDK
Guru

> Would I have to use a timer to create my own timeout? Start it after the header is read in and reset once the body comes in? 

Yes, that's what I would do. Also implement a sync word or CRC or some other validation to ensure the header is correct, if you can spare the overhead.

HAL_UART_Receive_IT is nonblocking, so a timeout doesn't make sense here.

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