It might be an obvious answer to this, but I can't find it via a quick google search. To give my question a bit of more context, I'm writing an application which simulates a communication with Arduino via serial port, therefore I need to know how it is implemented internally.
From my previous experience with micro-controllers, the serial port is normally presented in micro-controller as two separate queues, one for TX and another for RX. For transmit out of MC, my software writes a byte to the end of transmit queue, and MC hardware reads byte from the beginning of the queue, converts it to bits, and toggles the pin to transmit data/parity/stop bits. And it keep doing that until queue is exhausted. The opposite happens for receive queue.
Am I right in assuming that Arduino has similar implementation with two separate queues? In particular, I want to read and write to serial port simultaneously.
-
1It depends on which Arduino and which serial port you are talking about. It can be a raw UART (TX and RX pins), an UART tunneled through USB by another chip (e.g. in the Uno communicating with the host computer), or a completely virtual UART seen through USB (e.g. in the Leonardo).Edgar Bonet– Edgar Bonet2017年04月27日 13:22:21 +00:00Commented Apr 27, 2017 at 13:22
-
but to answer the question, there are two separate buffers, the arduino can only read or write at a time, but the hardware side can be treated as being full duplex with separate buffers.James Kent– James Kent2017年04月27日 13:25:02 +00:00Commented Apr 27, 2017 at 13:25
-
1The AVR UART has a simple one step hardware queue for both the RX and the TX. The hardware has a register for the parallel to serial conversion and a queue register to allow write/read while a transmit/receive is in progress. The Arduino HardwareSerial class adds a software queue (32 or 64 bytes). These buffers are used to feed the hardware queues on interrupts from the UART hardware module.Mikael Patel– Mikael Patel2017年04月27日 15:28:02 +00:00Commented Apr 27, 2017 at 15:28
-
1Please do not cross-post the same question on different Stack Exchange sites. See Is cross-posting a question on multiple Stack Exchange sites permitted if the question is on-topic for each site?.Nick Gammon– Nick Gammon ♦2017年04月27日 23:08:00 +00:00Commented Apr 27, 2017 at 23:08
2 Answers 2
Yes. Arduino has two buffers. See the HardwareSerial.h and HardwareSerial.cpp
I have a lengthy post about Arduino serial communications: How does serial communications work on the Arduino?
In short, the hardware (on an Atmega328 like on the Uno) has a two-byte receive queue (so you can be putting one byte into memory while the next byte is being received). It has a one-byte send queue.
Lengthier queues are implemented in software using interrupts to move data from the RAM queues in/out of the hardware queues.