Serial.available not working, problem, solution

Hi! I find a solution.

If you read from Serial more than once in a void loop() while (Serial.available()==0){ } is not worked anymore for me.

I made a function that return a long int, but at first it returned 0 even if i don't typed anything so i put a Serial.parseInt(); before a while loop, and itt cleard the Serial and it worked! :)

void setup() {

long int readInt(){ //return a long int from Serial
  long int num; 

  //Sometimes Serial.parseInt is return 0 ewen you dont typed aniting
  //It will clear it
  //It is happening wery quickl you dont have to worry that your answer
  //getting = trash 
  int trash = Serial.parseInt();//reading long int from Serial
    while (Serial.available()==0){
       num = Serial.parseInt(); //reading long int from Serial
       Serial.println(num); // checking it works corectly 
  return num;

void loop() {
int testnum1;
long int testnum2;
Serial.println("Enter testnum1: ");
testnum1= readInt();

Serial.println("Enter testnum2:");
testnum2 = readInt();

Serial.print("testnum1: ");

It is work perfect whit human input, but if you send data from pyton it maybe not work any more!

The cause of the unexpected 0 is likely the line ending on the serial input. You can learn about that by reading these comments:

Have a look at this Simple Python - Arduino demo

The Python code should work on Windows if you edit it to use the Windows style of COM ports. I believe you also have to change the line where it says rtscts = True to rtscts = False

Also have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.