serial.available() receiving 0 (NULL) when nothing is connected

Im trying to connect a mega adk, and a pro mini together with txrx.

My mega sends a signal to the mini to get the mini to send back 'T' to the mega via serial.

This works, but my problem is, if nothing is being sent by the mini - or even if I pull out the rx tx connections, somehow the code still goes into the serial.available() loop, and it says that the incoming byte is 0. It is my understanding, if nothing is being sent, then serial.available returns 0 and therefore doesnt satisfy the loop conditions.

I tried using a serial flush function. When using this, it would just go into the function and stay there.

My code:

int switchIt = LOW;
byte incomingByte = 'X';

void setup() {
  Serial.begin(115200);
  Serial1.begin(4800);
  // put your setup code here, to run once:
  pinMode(13, OUTPUT);
}

void loop() {
  if (switchIt == LOW) {
    switchIt = HIGH;
  } else {
    switchIt = LOW;
  }
  Serial.print("switch: ");
  Serial.println(switchIt);
  digitalWrite(13, switchIt);
  //serialFlush();
  while (incomingByte == 'X') {
     if (Serial1.available() > 0) {
      incomingByte = Serial1.read();
      Serial.println("in");
     }
  }
  Serial.println("something came through");
  Serial.println(incomingByte);
  if (incomingByte == 'T') {
    Serial.println("yep");
  } else {
    Serial.println("nope");
  }
  incomingByte = 'X';
  
  delay(1000);
}

void serialFlush(){
  while(Serial1.available() > 0) {
    char t = Serial.read();
  }
}

Output:

switch: 1
in
something came through
0
nope
switch: 0
in
something came through
0
nope
switch: 1
in
something came through
0
nope
switch: 0
in
something came through
0
nope
switch: 1
in
something came through
0
nope

Thanks.

loop() runs over and over again, hence the name. Any code in loop() will run over and over again. If you want some code to run only when serial data is available then you need to put it inside an if statement

void loop() {
  if (Serial1.available() > 0)  {
    // The code inside this block will only run when there is data available from Serial1
  }
  // The code here will run over and over again, whether there is data available from Serial1 or not.
}

pert:
loop() runs over and over again, hence the name. Any code in loop() will run over and over again. If you want some code to run only when serial data is available then you need to put it inside an if statement

void loop() {

if (Serial1.available() > 0)  {
    // The code inside this block will only run when there is data available from Serial1
  }
  // The code here will run over and over again, whether there is data available from Serial1 or not.
}

I have that ...

The problem is that its going into the if statement when there is nothing there.

Do you get the same behaviour with Serial2 and Serial3 ?

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Robin2:
Do you get the same behaviour with Serial2 and Serial3 ?

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Hi Robin2, I have read your tutorial before and have learnt alot from it. But it doesn't explain why there are bytes to receive when nothing is connected. Can you explain that to me?

Thanks.

benjaminhoey:
Can you explain that to me?

You did not answer my question. I want to establish if your Serial1 is faulty.

...R

Robin2:
You did not answer my question. I want to establish if your Serial1 is faulty.

...R

Yes I am getting the same behaviour on serial2 and serial3

if (Serial2.available() > 1) {
    int rec;
    byte b1  = Serial1.read();
    byte b2 =  Serial1.read();
    rec = ((int)b1) * 256 + b2;
}

this returns the number 4. There is nothing attached to serial2. So shouldnt it no enter the if statement?

And so is it possible to receive something when nothing is connected, and if so, how do I prevent receiving bad data, if for example the wire disconnected by accident during use?

Thanks.

I will bookmark this and try to repeat your experience with my Mega sometime tomorrow.

Bump this Thread if I forget.

...R

thanks

I have just tried this program on my Mega

// python-build-start
// action, upload
// board, arduino:avr:mega:cpu=atmega2560
// port, /dev/ttyACM0
// ide, 1.6.3
// python-build-end

// ide, 1.6.3
// ide, 1.5.6-r2


void setup() {
    Serial.begin(9600);
    Serial2.begin(9600);
    Serial.println("MegaSerialTest.ino");
}

void loop() {
    if (Serial2.available() > 1) {
            // show how much is in the buffer
        Serial.print("Serial2 available ");
        Serial.println(Serial2.available());
            // get the first two bytes and convert to an int
        int rec;
        byte b1  = Serial1.read();
        byte b2 =  Serial1.read();
        rec = ((int)b1) * 256 + b2;
        Serial.print("From Serial2 ");
        Serial.println(rec);
            //empty the buffer
        while(Serial2.available() > 0) {
            Serial2.read();
        }
    }
    Serial.println("  ====  ");
    delay(1000);
}

When I re-start the Mega nothing appears from Serial2.
When I stick a piece of wire into the socket of pin 17 (Rx2) it starts showing 63 characters in the buffer and showing a value of -1 in the variable rec. That continues even after I remove the piece of wire.

This led me to think that the input pin for Rx2 is floating and creating spurious values.

When I connected Rx2 to the 5v pin via a 4700 ohm resistor the input from Serial2 ceased. And I think that would be a suitable solution for you. The reason for connecting to 5v (rather than GND) is because the serial system normally idles HIGH. AFAIK the presence of the resistor will not interfere with genuine serial data. If it does you might try a higher value resistor such as 5600. 6800 or 10k.

An interesting phenomenon.

...R
PS. Just ignore the comments at the top of the program.

Thanks for putting in the effort Robin2.

So just to double check: I connect my rx - tx and tx - rx as normal, but now I put a resistor from the rx to 5v -
and only do this on the mega, not the pro mini as well?

benjaminhoey:
my rx - tx and tx - rx as normal, but now I put a resistor from the rx to 5v -
and only do this on the mega, not the pro mini as well?

That is what I have in mind. The purpose of the resistor is to hold the Mega's Rx pin HIGH even if an external device does not.

I am assuming (from your earlier Posts) that there is no problem at the pro-mni.

...R