Regarding the number of bits that is sent using UART and configuring the UART handle is unclear as well as setting the parity bit, is it odd or even.

"uint8_t data[] = " I want to send this sentence "


The above consists of 30 bytes.

If I configure the UART to have a wordlength of 8 bits in a stm32f4

BaudRate = 9600;
StopBits = 1;

how will it send the 30 bits if I configured the UART in 8 bits/1 byte?

Will it send it one by one so 30 times:


Also, the parity bit, if the data is 011001 then parity is odd but how to translate data[] into 0's and 1's?

  • \$\begingroup\$ That data has 30 bytes, not bits. \$\endgroup\$ – CL. Mar 1 at 9:11
  • \$\begingroup\$ That data has 31 bytes, as the compiler will add a \0 at the end of the string. Depending how the data is sent to the uart it may or may not contain the null terminating char, all depends of the code used. \$\endgroup\$ – Damien Mar 1 at 9:26
  • \$\begingroup\$ My question is whether the data thus will be sent in packets? So one byte each time after which the complete sentence has been sent (31 times as you say)? \$\endgroup\$ – ll ll Mar 1 at 9:37
  • \$\begingroup\$ If you define a packet as being equivalent to an asynchronous frame (which is reasonable in this context) then 30 or 31 packets will be sent (depending on implementation). \$\endgroup\$ – Peter Smith Mar 1 at 10:04

The above consists of 30 bits.

No. It consists of 30 Bytes.

Will it send it in packets?

This depends on the hardware that controls your UART. Usually it can be configured that way that a byte you write into a transmit register is immediately sent.
Some implementations have a FIFO that you can fill before triggering transmission.
Or a transmission is automatically triggered when the FIFO is full...

If so, what happens if I keep resending the data, will the fourth time include 2 bits from the second time it will send the data?

I don't really understand what you mean, but anyway: You simply write a value (usually a 8 bit value, i.e. a byte) to the transmit buffer/register, and that data is sent once.
There may be implementations that allow re-sending of the same value if you don't change the transmit register and trigger transmission again.

but how to translate data[] into 0's and 1's?

You may have a look at the ASCII table given here: https://en.wikipedia.org/wiki/ASCII
Also, this site may be useful to learn: https://www.binaryhexconverter.com/ascii-text-to-binary-converter

Your example text results in the following binary values: 00100000 01001001 00100000 01110111 01100001 01101110 01110100 00100000 01110100 01101111 00100000 01110011 01100101 01101110 01100100 00100000 01110100 01101000 01101001 01110011 00100000 01110011 01100101 01101110 01110100 01100101 01101110 01100011 01100101 00100000

BTW: You don't have to care about parity calculation when using UART. The UART hardware will do that for you. The sender sets the parity bit, and the receiver checks it and flags an error if it's wrong.

  • \$\begingroup\$ Thank you, I've edited the question. So it will send it 30 times? It transmits a byte stored in the buffer and we have 30 bytes...? \$\endgroup\$ – ll ll Mar 1 at 9:26
  • \$\begingroup\$ Of course, if you write that 30 bytes to your transmit buffer, it will transmit that 30 bytes - assumed you have set "8 bit datalength". There are implementations that allow data lengths below 8 bit. In this case it would be up to you to split your values to not lose information. Writing a byte to the buffer, but having "6 bit" data length would just ignore the two remaining (usually upper) bits. \$\endgroup\$ – mic Mar 1 at 10:24

enter image description here

The above image shows a typical UART data frame. The number of bits transmitted for each character depends on how its configured.

You a 8 bits for data 1 start bit, possibly a parity bit and and 1 or 2 stop bits so the data sent over the UART is therefore between 10 and 12 bits per character.

Assuming the UART is configured for parity it will calculate it for you and the other end will check it signalling an error if required.

Parity can be defined as "None" meaning there is no parity bit, "Even" or "Odd" the UART stuffs an extra bit in to make the number of logic '1's in the data plus the parity bit even or odd as required.

The data is sent least significant bit first but you don't need to worry about this when loading the UART. The data is encoded as ASCII.

Without looking at your code I can't tell how many characters will get transmitted. C stores strings with a terminating NUL, '\0', ASCII code zero. Your transmit code may or may not send this depending on how it is written.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.