cancel
Showing results for 
Search instead for 
Did you mean: 

What delay time should be set when transmitting data via UART?

MMust.5
Senior II

The fourth parameter in the HAL_UART_Transmit function.

Suppose I send 5 characters, is probably 5*9==45 .

Transfer rate 115200 Bits/s

What delay should I set in the HAL_UART_Transmit function?

5 characters at a speed of 115200 Bits/s will be transmitted in 0.000390625 seconds.

In 390 microseconds, 5 characters can be sent.

But I saw cases where a time delay of 1000 milliseconds is set and everything worked.

How should the time delay be calculated?

4 REPLIES 4
MM..1
Chief II

This parameter isnt delay, but timeout if error. For example uart isnt in ready state...

In normal situation code start send and continue after sucess as you calculate.

But for example if you send 50 chars , time to send ok is 4msec and if you set less code dont send all ...

MMust.5
Senior II

Ok. Thank you.

And how to find out what should be the timeout in case of an error?

>In normal situation code start send and continue after sucess as you calculate.

You probably meant: In a normal situation, the UART waits for readiness and then starts sending?

I did not understand how to calculate the timeout. This time, as I understand it, should not depend on the number of transmitted characters, but should depend only on the readiness of the UART and other errors with the UART.

------------------------

Here https://electronics.stackexchange.com/questions/273587/stm32-hal-uart-transmission

explain differently. As I wrote in my first post.

670 bits / 10 milliseconds timeout == 67000 Bits/s .

That is, 670 bits must be transmitted in a timeout of 10 milliseconds, this requires a speed of 67000 Bits/s .

In this case, it is clear how to calculate the speed. But do I really have to always count the number of transmitted characters and enter the UART baud rate very precisely for this?

Bob S
Principal

> Suppose I send 5 characters, is probably 5*9==45

You are sending 9-bit data? If so, you also need to include the start/stop bits. Or are you sending 7-bit data and have already included the start/stop bits?

When using polled TX (which I rarely do), I set the "timeout" param to something relatively huge (say 200ms). Because if something is wrong that will cause a timeout, it will be REALLY wrong (i.e. the UART is mis-configured). That is presuming normal single sender, singe receiver UART, not multi-drop.

Or, an easy way to calculate a "sure to be long enough" timeout, is to use the "size" param as the timeout. At 115200 baud, 1 "character" takes much less than 1ms to send. So a timeout of 1ms per character should be more than sufficient.

At 9600 baud figure 1ms/byte

Figure what the byte times are for 10 or 11 bits times, give yourself a 10% margin, or a couple of characters, whichever is larger.

Its somewhat non-critical, it just stops the code getting stuck in infinite loops if the peripheral stops clocking, or you get some error or failure condition.

1000 ms is sloppy, but fine.

For AT commands to modems don't use arbitrary delays between commands, they should all have OK/ERROR responses, and you should use those. Some cellular network interactions have command timeouts in the 2-3 MINUTE range.

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