Serial.available and other Serial class methods???please help

hello...i am new to Arduino programming....just bought the Duemilanove a week ago....

i have trouble in using Serial class methods....

for example when i attach my Duemilanove board to my computer and perform input through Serial Monitor,i get unexpected results....

for example,the first input i gave to the buffer of the Arduino Board is abcdef....so the size of the buffer should be 6.....

but i get unexpected results....

the OUTPUT is somewhat like(i said somewhat because i dont have the board with me right now,its with my friend)

Before Read opn:1 //the first time when loop() runs
After Read opn:0
Before Read opn:4
After Read opn:3
.
.
.
Before Read opn:0 //this is the last time loop() runs before getting stuck in while loop
After Read opn:0

Serial.available() is giving unexpected outputs

code-

void setup() 
{
Serial.begin(9600);
}

void loop()
{
while(Serial.available()==0);

Serial.print("Before Read operation:");
Serial.println(Serial.available());

Serial.read();

Serial.print("After Read operation:");
Serial.println(Serial.available());

char val=Serial.read();

}

output is bizaare...

but when i changed the code a bit...

void loop(){

while(Serial.available()==0){
delay(1000);
}

//rest code is same

}

then the output is what i expected...which is-

Before Read opn:6
After Read opn:5
Before Read opn:4
After Read opn:3
Before Read opn:2
After Read opn:1

can anyone please explain the proper working of Serial.available(),Serial.read(),etc..???

i will be highly thankful....

Millions of instructions are executed every second. Serial is much slower than that, so your Arduino will read the available bytes as 1 well before the remaining characters are received.

yeah i am thinking the same....but i need to grab the Serial class methods concepts before i move on further.....

the result is what i expect when i perform the delay() thing....

can you please help me on Serial Communication??any good link will be appreciated...

thanks in advance...

Cosmos:
yeah i am thinking the same....but i need to grab the Serial class methods concepts before i move on further.....

the result is what i expect when i perform the delay() thing....

can you please help me on Serial Communication??any good link will be appreciated...

thanks in advance...

I don't know what you're trying to accomplish, so I can't really help. If you're looking to pull input from the serial monitor, you either need to know the length, or use start/end of packet markers. Search the forums, there should be plenty of examples of it.

I want to clarify my concepts on Serial Communication regarding Arduino....i.e. the internal working of Arduino board when it performs Serial Communication with Computer....

thank you so much Sir…
:slight_smile:

Cosmos:
I want to clarify my concepts on Serial Communication regarding Arduino....i.e. the internal working of Arduino board when it performs Serial Communication with Computer....

All the source code for the arduino libraries, including Serial (called HardwareSerial) are included when you download the IDE. It's under hardware/arduino/cores/arduino/
Libraries that you need to include are under libraries/ or your sketchbook/libraries