user32367: I want to implement a function in another micro-controller which works like serial.available(). I don't know how it works. I read about Serial.available() in this link.
Whenever I write a string on the terminal, it prints the length of the string. Can anyone tell me how it works?

user4143: It reports the difference between the head and the tail of the circular buffer the incoming characters are stored in.
The way serial reception on the Arduino works is:

A character is received into the internal hardware RX buffer (UDRx)
An interrupt is triggered
An interrupt service routine (ISR) is executed
The character in UDRx is read and, if there is room, stored in a circular buffer (_rx_buffer).
The head pointer of _rx_buffer is incremented and wrapped if needed.

When you actually read a character with Serial.read() the following happens:

If the head and tail of rx_buffer are equal, return -1
Get the character at tail from _rx_buffer
Increment tail and wrap if needed
Return the character

So when you use Serial.available() it just returns head - tail (taking into account the wrapping, so the sum is slightly more complex than a simple subtraction).
You should examine the files HardwareSerial.h and HardwareSerial.cpp in the Ardino AVR core software.

32367: <p>I want to implement a function in another micro-controller which works like <code>serial.available()</code>. I don't know how it works. I read about <code>Serial.available()</code> in <a href="https://www.arduino.cc/en/serial/available" rel="nofollow noreferrer">this link</a>.</p>

<p>Whenever I write a string on the terminal, it prints the length of the string. Can anyone tell me how it works?</p>
 4143: <p>It reports the difference between the head and the tail of the circular buffer the incoming characters are stored in.</p>

<p>The way serial reception on the Arduino works is:</p>

<ol>
<li>A character is received into the internal hardware RX buffer (<code>UDRx</code>)</li>
<li>An interrupt is triggered</li>
<li>An interrupt service routine (ISR) is executed</li>
<li>The character in <code>UDRx</code> is read and, if there is room, stored in a circular buffer (<code>_rx_buffer</code>).</li>
<li>The <code>head</code> pointer of <code>_rx_buffer</code> is incremented and wrapped if needed.</li>
</ol>

<p>When you actually read a character with <code>Serial.read()</code> the following happens:</p>

<ol>
<li>If the <code>head</code> and <code>tail</code> of <code>rx_buffer</code> are equal, return <code>-1</code></li>
<li>Get the character at <code>tail</code> from <code>_rx_buffer</code></li>
<li>Increment <code>tail</code> and wrap if needed</li>
<li>Return the character</li>
</ol>

<p>So when you use <code>Serial.available()</code> it just returns <code>head - tail</code> (taking into account the wrapping, so the sum is slightly more complex than a simple subtraction).</p>

<p>You should examine the files <code>HardwareSerial.h</code> and <code>HardwareSerial.cpp</code> in the Ardino AVR core software.</p>
