Problem with Serial.available and Serial.read

Hi,
I'm a beginner with arduino.
I have some problems with the simple code from the Guide:
//
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}
//

Serial.available and Serial.read always return -1, whatever key is pressed.
My Arduino is mega2560 R3
Thanks

Serial.available should never return negative numbers.
Are you sure?

Please remember to use code tags when posting code

Are you using the Serial Monitor to test this? If so, you need to press ENTER to actually send the information to the Arduino.

blh64:
Are you using the Serial Monitor to test this? If so, you need to press ENTER to actually send the information to the Arduino.

I'm a fool. Pressing "Enter" works perfectly. I'm sorry I wasted your time!
Thank you

If you are using other terminal software (PuTTY, TeraTerm, ...) then they actually transmit key strokes immediately so it is not surprising that the fact the Serial Monitor works on a line by line basis trips people up.

TheMemberFormerlyKnownAsAWOL:
Serial.available should never return negative numbers.

I forgot to answer your kind question. You're right, maybe it returns zero if the buffer's empty. Only Serial.read should return -1.

blh64:
If you are using other terminal software (PuTTY, TeraTerm, ...) then they actually transmit key strokes immediately so it is not surprising that the fact the Serial Monitor works on a line by line basis trips people up.

Thank you for this other kind information!

If you are receiving data from the InputBox of the Serial Monitor (Fig-1), then you should click on the Send button of the Serial Monitor rather than hitting on the Enter key of the Keyboard of the PC though it works?


Figure-1:

GolamMostafa:
If you are receiving data from the InputBox of the Serial Monitor (Fig-1), then you should click on the Send button of the Serial Monitor rather than hitting on the Enter key of the Keyboard

They both do the exact same thing - transmit whatever is in the input box plus whatever line ending is selected.

blh64:
They both do the exact same thing - transmit whatever is in the input box plus whatever line ending is selected.

But, I observed the following:

Enter key sends data of 'InputBox + Line ending tab' whenever the cursor is in the InputBox or on the Send Button.

Send button always sends data of 'InputBox + Line ending tab' regardless of the position of the cursor -- whether it is in the InputBox, Send button, Autoscroll, Show timestamp, Baud Rate Tab, Clear output.

Obviously, you can't type something into the input box and then move the focus to another part of the window or a different window or a different control and expect the ENTER key to work. No different than putting the cursor over the window and clicking and expecting the Send button to work.