cancel
Showing results for 
Search instead for 
Did you mean: 

How to handle the Usart receive with unknow Data length.

Sprak.11
Associate II

i am try to interface the STM32F103C8T6 with GSM & usb to Serial converter (Multi USART). But in GSM AT Comment Response Data Length is Different for Each & every time.

I don't know how to handle the UART Receive with unknown data length Please help me Solve this Problem.

If there is any reference link to Know how to handle that, Please Share that also.

4 REPLIES 4
S.Ma
Principal

There are 2 methods:

  • you get interrupt each received byte and build a valid incoming message to process checking headers, checksum, length byte, termination character. The 8 bit MCU way. Good for reactivity and buffer size optimisation. Cons: interrupt every incoming byte, the higher the baud rate the more cpu intensive it becomes
  • you use a DMA in cyclic mode on a big enough buffer to collect worst case 2 msec of continuous USART data and you time regularly check what arrived and see if one or more messages are pending processing. Pros: Less interrupts, cons: msec reactivity.

Your application and MCU selection should help make a decision.

You keep reading data, and parse what actually arrives.

The STM32F1 is going to interrupt for each byte received, so build a state machine. Use DMA for TX. Decide if the complication of using DMA for RX outweighs other considerations. Have an algorithm that sweeps the buffer at HT, TC and 1ms. Cellular Modems have response times in the 100ms to several minutes.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
prain
Senior III

​All answers are right.

for fastest response time:

make a big enough buffer and fill it with incoming usart bytes via DMA. Also ​enable usart idle line detection interrupt. when a packet received, you will get into idle line ISR. There you can parse the new received bytes based on DMA CNDTR register.

Hi, I made and approach based in polling and timeout with 2 functions. With usr_uart_SerialAvailable you check is there is incoming data based in a timeout value USR_UART_BUFFER_TIMEOUT, if there is incoming data it keep reading it until the buffer is full or the timeout is reached, the buffer size is USR_UART_BUFFER_SIZE, after that you call usr_uart_ReadString for copy the incoming data. Please check it