hammies
hammies

Reputation: 1435

Serial available always returns 0 in Arduino

Why do I keep getting 0? I tried several ways and took the sample code on the Arduino site, but that didn’t work either. I always get Serial.available() = 0.

int incomingByte = 0;  // For incoming serial data

void setup() {
    Serial.begin(9600);  // Opens serial port, sets data rate to 9600 bit/s
}

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);
    }
    else
        Serial.print("I received nothing ");
}

Upvotes: 4

Views: 27146

Answers (5)

Dmitriy Zakharov
Dmitriy Zakharov

Reputation: 1

Some boards restart when a serial connection is initiated. This way when you do echo smth > /dev/serial/xxxx board resets, doesn't receive smth, and Serial.available() never happens.

This behavior can be overridden, or you can communicate with cat > /dev/serial/xxxx.

It took me a good while to figure out.

Upvotes: 0

GSC
GSC

Reputation: 1

Above your code for reading data, insert:

if (Serial.available()) {
    int fake = Serial.parseInt();
}

This will clear the 0 value returned by the Serial.available() function.

Upvotes: 0

TrevorLee
TrevorLee

Reputation: 231

What is wrong? Nothing. The code is ok the hardware is ok. Serial.available() returns the number of characters in the buffer waiting to be read, so if you don't send it anything it will always return zero/false.

int incomingByte = 0;

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

void loop() {
        if (Serial.available() > 0) {
                incomingByte = Serial.read();
                Serial.print("I received: ");
                Serial.println(incomingByte, DEC);
        }
        else Serial.print("I received nothing\n");
        delay(1000);

}

Uploading this code and sending 'ss' into the serial buffer will produce:

0:00:00.000 -> I received:115 //s
0:00:00.000 -> I received:115 //s
0:00:00.000 -> I received:10  //newline

Upvotes: 1

Richard Elkins
Richard Elkins

Reputation: 317

Your program works fine on my Arduino although you need some delays to stop the rapid-fire "I received nothing " messages. I would change the "I received nothing " block to include a delay of a few seconds e.g. delay(3000) for a 3-second delay.

Also, consider changing your code to use a SerialEvent() procedure such as depicted here: SerialEvent example

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
}

void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    Serial.println(inputString);
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
}

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

Upvotes: 2

user3704293
user3704293

Reputation: 1056

I had a similar problem and solved it by including short delay() statements. It seems that Arduino reads the first byte and is interrupted with the following...

Upvotes: 0

Related Questions