user1301: im just looking for how to send/ write date to pc over serial port from arduino..vice versa.
I have few Question to ask after taking so much time and couldn't figure it out myself. I hope you guys can guid me please.
First, i found i can use Serial.println() function to send some data then, on the pc side, it just checks and reads the port (/dev/tty..). But, how does this process actually work under the hood? So, if you use serial.println() it knows about where to write?
Second i also found it's something to do with UART communication.. But, inside Hardware.cpp file, i couldn't find any function call to the one for UART.
Third there is the code like extern HardwareSerial Serial; Then where is the definition of this Serial?
Fourth There seem to have ring buffer for serial buffer. So, when data is being sent then, what and who fill the data into this buffer inside Hardware.cpp?
please, forgive my silly question.. i'm just so curious about this..

user168: Serial.println knows where to write because Serial is an object. On boards with more than one port there will also be a Serial1,Serial2,etc. Each object corresponds to a port.
The ring buffers are filled from interrupts. When a new byte comes in, it triggers an interrupt that puts the byte in the buffer. When a byte is finished sending it triggers another interrupt that reads the next byte from the buffer and sends it.
Somewhere there is a source file that sets up the UART. It's full of writing special values to registers and low level things like that. I'm not sure what files things are defined in, but if you use the search feature on github there is tons of interesting stuff.

user7508: The first question already got a perfectly good answer. I am answering the other ones.

i couldn't find any function call to the one for UART.

There are no "UART functions" in the MCU. The UART is controlled by reading and writing into special registers. These come by names like UCSR0A, UCSR0B, UCSR0C (USART 0 Control and Status Registers A, B and C), UBRR0L, UBRR0H (Low and High bytes of the USART 0 Baud Rate Register) and UDR0 (USART 0 Data Register). These registers are all referenced in HardwareSerial.cpp. You can check the datasheet for their meaning.

where is the definition of this Serial?

In HardwareSerial.cpp, by the end of the file, right after the comment "Preinstantiate Objects".

There seem to have ring buffer for serial buffer. So, when data is being sent then, what and who fill the data into this buffer inside Hardware.cpp?

There are actually two ring buffers: the receive buffer (_rx_buffer) is filled by an interrupt service routine and is emptied by you calling Serial.read(). The transmit buffer (_tx_buffer) is filled by you calling Serial.write(), Serial.print() or Serial.println() (the line _tx_buffer->buffer[_tx_buffer->head] = c; in HardwareSerial::write()), and is emptied by an interrupt service routine.
These interrupt service routines are defined in HardwareSerial.cpp. They start with lines like ISR(SOMETHING_vect). They are triggered by the hardware UART. The transmit ISR is triggered when the UART is ready to accept a new byte to transmit (when it is done transmitting byte n, it starts transmitting byte n+1 and is ready to accept byte n+2 in its TX buffer). The receive ISR is triggered when a new byte has been received.

when does it get initialised?

When you call Serial.begin().

1301: <p>im just looking for how to send/ write date to pc over serial port from arduino..vice versa.</p>

<p>I have few Question to ask after taking so much time and couldn't figure it out myself. I hope you guys can guid me please.</p>

<p>First, i found i can use Serial.println() function to send some data then, on the pc side, it just checks and reads the port (/dev/tty..). But, how does this process actually work under the hood? So, if you use serial.println() it knows about where to write?</p>

<p>Second i also found it's something to do with UART communication.. But, inside Hardware.cpp file, i couldn't find any function call to the one for UART.</p>

<p>Third there is the code like extern HardwareSerial Serial; Then where is the definition of this Serial?</p>

<p>Fourth There seem to have ring buffer for serial buffer. So, when data is being sent then, what and who fill the data into this buffer inside Hardware.cpp?</p>

<p>please, forgive my silly question.. i'm just so curious about this..</p>
 168: <p>Serial.println knows where to write because Serial is an object. On boards with more than one port there will also be a Serial1,Serial2,etc. Each object corresponds to a port.</p>

<p>The ring buffers are filled from interrupts. When a new byte comes in, it triggers an interrupt that puts the byte in the buffer. When a byte is finished sending it triggers another interrupt that reads the next byte from the buffer and sends it.</p>

<p>Somewhere there is a source file that sets up the UART. It's full of writing special values to registers and low level things like that. I'm not sure what files things are defined in, but if you use the search feature on github there is tons of interesting stuff.</p>
 7508: <p>The first question already got a perfectly good answer. I am answering the other ones.</p>

<blockquote>
  <p>i couldn't find any function call to the one for UART.</p>
</blockquote>

<p>There are no "UART functions" in the MCU. The UART is controlled by reading and writing into special registers. These come by names like <code>UCSR0A</code>, <code>UCSR0B</code>, <code>UCSR0C</code> (USART 0 Control and Status Registers A, B and C), <code>UBRR0L</code>, <code>UBRR0H</code> (Low and High bytes of the USART 0 Baud Rate Register) and <code>UDR0</code> (USART 0 Data Register). These registers are all referenced in HardwareSerial.cpp. You can check the datasheet for their meaning.</p>

<blockquote>
  <p>where is the definition of this Serial?</p>
</blockquote>

<p>In HardwareSerial.cpp, by the end of the file, right after the comment "Preinstantiate Objects".</p>

<blockquote>
  <p>There seem to have ring buffer for serial buffer. So, when data is being sent then, what and who fill the data into this buffer inside Hardware.cpp?</p>
</blockquote>

<p>There are actually <em>two</em> ring buffers: the receive buffer (<code>_rx_buffer</code>) is filled by an interrupt service routine and is emptied by you calling <code>Serial.read()</code>. The transmit buffer (<code>_tx_buffer</code>) is filled by you calling <code>Serial.write()</code>, <code>Serial.print()</code> or <code>Serial.println()</code> (the line <code>_tx_buffer-&gt;buffer[_tx_buffer-&gt;head] = c;</code> in <code>HardwareSerial::write()</code>), and is emptied by an interrupt service routine.</p>

<p>These interrupt service routines are defined in HardwareSerial.cpp. They start with lines like <code>ISR(SOMETHING_vect)</code>. They are triggered by the hardware UART. The transmit ISR is triggered when the UART is ready to accept a new byte to transmit (when it is done transmitting byte <em>n</em>, it starts transmitting byte <em>n</em>+1 and is ready to accept byte <em>n</em>+2 in its TX buffer). The receive ISR is triggered when a new byte has been received.</p>

<blockquote>
  <p>when does it get initialised?</p>
</blockquote>

<p>When you call <code>Serial.begin()</code>.</p>
