Can data be transferred and received at the same time through uart ?

Thread Starter

Kittu20

Joined Oct 12, 2022
462
I don't understand whether data can be transmitted and received at the same time or only data can be transferred or received at a time using uart protocol.

I have seen some sample program of uart protocol. I see two functions one for transferring data and another for receiving data. so i think we can not transfer and receive data at the same time using uart protocol
 

DickCappels

Joined Aug 21, 2008
10,153
Bit simply bit-banging it is usually done such a way that communications are simplex (one way at a time) though it could be duplex with the use of interrupts and/or timers. When using a hardware UART/USART all that I have seen over many decades were capable of full duplex operation.

With, for example, a hardware UART one can take data from the receiving register, modify it, and send it out through the transmitting register, even as another character is being received. The UART only needs attention when something is received or the CPU needs to send something. The shifting bits in and out is automatic.
 

Irving

Joined Jan 30, 2016
3,843
Yes and no. The physical UART hardware can certainly transmit and receive simultaneously, however the communication with the CPU is one way at a time. But because the data transfer to/from the UART by the CPU is orders of magnitude faster than the UART's serial transmission it can be made to appear simultaneous.

Simple UART interfaces will wait for a character to be received, or wait for the transmit side to be free before sending a character. These are called 'blocking' calls because the CPU does nothing except wait for each process to complete at the character level - clearly this is inefficient yet easy to implement. More sophisticated implementations will use a buffer and interrupts so that sending and receiving occur in the background as interrupt driven tasks; transmission and reception at the main program level are seen as events, either at the character level or, more commonly, at the buffer (string or line) level, allowing the main program flow to run continuously and be responsive to user input for example.
 

Papabravo

Joined Feb 24, 2006
21,158
Even if the UART peripheral can send and receive at the same time, the physical connection may not allow that. A 2-wire RS485 connection is inherently a half-duplex connection. At any point in time, for all nodes, they can either be sending or receiving but not both. There needs to be another mechanism to determine which node can transmit. Clearly it is not reasonable for two nodes to transmit at the same time.
 

nsaspook

Joined Aug 27, 2009
13,079
There are implementations of the UART interface that use DMA with FIFO's. This allows for very high speed transmit and receive simultaneously and directly to the program information buffers with no CPU intervention and much fewer interrupts.
https://www.infineon.com/dgdl/Infineon-CE218552_PSoC_6_MCU_UART_to_Memory_Buffer_Using_DMA-Code Example-v02_00-EN.pdf?fileId=8ac78c8c7d0d8da4017d0e6d04ff020d
This PSoC 6 code example demonstrates a UART-to-memory buffer data transfer using DMA, with no CPU usage. DMA channels are used to implement data transfer both on received data and transmitted data of the UART.
The physical hardware implementation determines what's possible with the program.
https://forum.allaboutcircuits.com/...nd-sensor-node-for-canbus.189388/post-1779041
 
Last edited:

sagor

Joined Mar 10, 2019
903
With hardware UART, yes. With software uart emulation (bit-banging), no.
Simultaneous TX/RX with a hardware UART usually requires interrupt routines. Many programmers avoid that. Also, in many cases, the program only wants to send and wait for a reply, so the programmer has no need to set up simultaneous TX/RX.
 

Papabravo

Joined Feb 24, 2006
21,158
Aren't we talking about RS-232 which has separate transmit and receive connections?
UARTs can be used with several different types of physical layers, of which I presented a particular example. I did not see where the TS specified that he was using an RS-232 physical layer. You may have reasonably assumed that and that's OK.
 

Papabravo

Joined Feb 24, 2006
21,158
With hardware UART, yes. With software uart emulation (bit-banging), no.
Simultaneous TX/RX with a hardware UART usually requires interrupt routines. Many programmers avoid that. Also, in many cases, the program only wants to send and wait for a reply, so the programmer has no need to set up simultaneous TX/RX.
Not true. It is manifestly possible to bit-bang full duplex serial communication, but I will readily admit that it is neither easy nor straightforward.
 

sagor

Joined Mar 10, 2019
903
Not true. It is manifestly possible to bit-bang full duplex serial communication, but I will readily admit that it is neither easy nor straightforward.
Well, technically is can be done with bit-banging, but I would think only if each bit is interrupt driven so the code can check both TX and RX bits as they transition. Like you say, complicated for sure. Just about all bit banging code I've seen is a simple timing loop, which cannot be "interrupted", else bits would be missed.
In simple terms, software RX/TX "public domain" code for bit banging is very rudimentary. I'm thinking also in terms of simple microprocessors like PIC or Arduino type of chips.
 

nsaspook

Joined Aug 27, 2009
13,079
Well, technically is can be done with bit-banging, but I would think only if each bit is interrupt driven so the code can check both TX and RX bits as they transition. Like you say, complicated for sure. Just about all bit banging code I've seen is a simple timing loop, which cannot be "interrupted", else bits would be missed.
In simple terms, software RX/TX "public domain" code for bit banging is very rudimentary. I'm thinking also in terms of simple microprocessors like PIC or Arduino type of chips.
There's nothing stopping a program from having intermixed RX/TX state machines if your update rates are fast (optimized ASM).
http://www.piclist.com/techref/microchip/16F819-rs232-9600-mm.htm
 

Papabravo

Joined Feb 24, 2006
21,158
Well, technically is can be done with bit-banging, but I would think only if each bit is interrupt driven so the code can check both TX and RX bits as they transition. Like you say, complicated for sure. Just about all bit banging code I've seen is a simple timing loop, which cannot be "interrupted", else bits would be missed.
In simple terms, software RX/TX "public domain" code for bit banging is very rudimentary. I'm thinking also in terms of simple microprocessors like PIC or Arduino type of chips.
It is only the START bit on receive that triggers an interrupt. The rest is done with timers and state machines running concurrently.
 

John P

Joined Oct 14, 2008
2,025
I've set up multiple full-duplex UARTs on a PIC16F processor, and they did work, but it's certainly not an ideal setup. The serial lines only operated at 9600 Baud, so I could have an interrupt at 4X that frequency, which could poll each receive line. That gives an adequate way to get incoming characters, and outgoing characters are much easier, because the processor has control of the timing. It's pretty time-intensive for the processor, so you need to ask "What else does this processor have to do?" Maybe it's too much. There could be a compromise where there are multiple serial ports, but perhaps only some proportion of them are active at the same time.
 
Top