Verify Serial connection

Hi,

Tell me if I understand this wrong, but isn’t if(Serial) supposed to detect if there is a serial connection to the Arduino?

I’m using the FTDI on my Arduino Duemilannove (IDE 1.0.3) to configure my program via a terminal application, but instead it just gets stuck in my configuration program even if I power it by battery.

This is my test code:

void setup()
{ 
  Serial.begin(9600);
  delay(100);  
  if(Serial)
  {
    digitalWrite(13, HIGH);  // This means the Serial Port is active
  }
  else
  {
    digitalWrite(13, LOW); // This means that the Arduino is powered by battery
  } 
 
}

void loop()
{
  
}

This test the LED on pin 13 always turns on, if I power it by battery or USB or if I have the terminal application open. How do I determine if I have something connected to the FTDI?

Hello,

bigbro: Tell me if I understand this wrong, but isn't if(Serial) supposed to detect if there is a serial connection to the Arduino?

To [u]some[/u] Arduino models. For other models, the condition is always true.

I'm using the FTDI on my Arduino Duemilannove (IDE 1.0.3) to configure my program via a terminal application...

For a Duemilannove, the condition is always true (as you have discovered).

How do I determine if I have something connected to the FTDI?

You determine something is connected when the something transmits data.

Ok that was my suspicion.

Is that just a bug or something that was added to later Arduino's?

bigbro: I'm using the FTDI on my Arduino Duemilannove (IDE 1.0.3) to configure my program via a terminal application, but instead it just gets stuck in my configuration program even if I power it by battery.

There's nothing there that would get stuck.

As Coding Badly said, just write your code to react to incoming serial data. If there is none, it won't have to do anything.

Is that just a bug or something that was added to later Arduino's?

Not a bug, AFAIK the Leonardo uses a virtual serial connection via the USB port and if(Serial) is how you determine when the connection has been made.

On other Arduinos you are probably just testing a pointer to a Serial class/object against 0 which it won't be so you get a true result.

I think this is pretty typical of the ad-hoc approach often used with Arduino code, how can if(Serial) possibly be intuitive? Maybe there's a good reason for not having a Serial.ready() method or something.


Rob

Thanks,

I'll simply run my configuration program if the user types $$$.

I'm sorry, I just realized that I miss read the following article http://arduino.cc/en/Serial/IfSerial

It states that for all other instances it will always return a 1 apart from on the leonardo.

On the Leonardo, if (Serial) indicates wether or not the USB CDC serial connection is open. For all other instances, including if (Serial1) on the Leonardo, this will always returns true.