How do Serial.available() and Serial.read work?

Hey everyone,
I am curious as to how Serial.available and Serial.Read work. If I was to create a loop that was kinda like this:
void loop(){
int var = Serial.read();
//then do something with the variable...
}
Would it work properly? What I'm trying to ask is whether or not checking if Serial.available() is greater than zero is really necessary. Also, does the arduino detect when a certain value as been read from Serial.read()? If not, how does it make it so your not just continually reading the first byte sent to the serial port on the arduino?

Thanks!

No, it wouldn't work properly. If there is no character available, Serial.read() returns -1. Your code should be more like:

void loop(){
int var = Serial.read();
if (var != -1)
    //then do something with the received character...
}

Most people write it as:

void loop(){
if (Serial.available())
    {
    int var = Serial.read();
    //then do something with the received character...
    }
}

Okay that makes sense. Thank you.

Caleborp:
If not, how does it make it so your not just continually reading the first byte sent to the serial port on the arduino?

Every "read" reads a byte. You won't get the same one again.

Hi Nick! Every Serial.read() returns an integer. If the serial buffer has nothing in it, it does not wait. The function returns integer -1 (FFFF, not 00FF). 00FF is the "funny y" = ÿ

Yes, "no data" is 0xFFFF which is outside the range of 0x00 to 0xFF for "data".

SurferTim:
Hi Nick! Every Serial.read() returns an integer. If the serial buffer has nothing in it, it does not wait.

Ah I misunderstood your correction. I meant "it reads a byte if one is there". It returns an integer, as you said, with 0xFFFF (AKA -1) being returned if no byte was available.

I was responding to this:

... how does it make it so your not just continually reading the first byte sent to the serial port ...

My point was that you won't get the first/last/whatever byte again.

is serial available interrupt based?
is there any register flag? which goes high after receiving first character.

Venus_Bilandi:
is serial available interrupt based?
is there any register flag? which goes high after receiving first character.

The short answer is NO. Your code has to call Serial.available() to check if there is data.

In the background when a character arrives an interrupt is triggered and the byte received is moved from the USART to the Serial Input Buffer.

…R
Serial Input Basics - simple reliable ways to receive data.

Venus_Bilandi:
is serial available interrupt based?
is there any register flag? which goes high after receiving first character.

The Theory stands as:
Following the execution of the Serial.print(0x35); instruction, the data byte 0x35 goes to the Receiver-Section of the UART Port of the remote controller. If the arrived data byte is proved to be error-free, the RXC0 (USART Receive Complete) bit of the following UCSR0A Register assumes LH-state and immediately interrupts the MCU. The MCU goes to the ISR Routine; it reads the data byte from the receiver port and saves into an unseen FIFO buffer. The unseen DataCounter is also incremented whose content can be brought into the user variable by executing the instruction byte x = Serial.available();. The content of the unseen FIFO buffer can also be made available into the user variable by executing the instruction byte y = Serial.read();.

ucsroa.png

ucsroa.png