cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f4 Discovery UART Rx IT - receiving msg of unknown bytes

marsenault
Associate II
Posted on May 28, 2014 at 20:13

I have basic RS-232 Tx/Rx running between the Discovery board and TeraTerm. I am successfully invoking HAL_UART_Receive_IT() in main with a fixed Rx message size.

My question is - what is the method for receiving, unblocked, messages where the length of the message is unknown? Basically the message will be terminated with a line feed, but various Rx'd command messages will have different lengths.

Additionally, is there a way to set an inter-character timeout in the driver so as not to get hung up on incomplete messages?

Thanks!
2 REPLIES 2
Posted on May 29, 2014 at 08:52

> My question is - what is the method for receiving, unblocked, messages where the length of the message is unknown?

> Basically the message will be terminated with a line feed, but various Rx'd command messages will have different lengths.

In pseudocode:

int main(void) {

  InitializeUart;

  while(1) {

    if (Uart_RxIsNotEmpty) {

      byte = Uart_ReadByte();

      if (byte != EndOfLine) {

        PutByteIntoBuffer(byte);

      } else {

        ProcessBuffer();

      }

    }

  }

}

This can go into the UART ISR of course - or just the buffering part of it, passing a flag to ''main'' when the end-of-line character is received.

> Additionally, is there a way to set an inter-character timeout in the driver so as not to get hung up on incomplete messages?

When the first character of the message arrives, set up one of the timers to fire an interrupt (e.g. and update interrupt) when a given time elapses. When a character arrives, reset the timer. Stop the timer when the complete message arrives. If the timer ISR fires, it should stop the timer and set a flag for main to indicate that timeout occured, main should check it and process the portion of message accordingly.

JW

marsenault
Associate II
Posted on May 29, 2014 at 13:43

Hello Jan,

Thank you so much. That's just what I needed.

Best regards,

Mark