1
if (Serial.available()) {
        digitalWrite(7, HIGH);
} else {
        digitalWrite(7, LOW);
}

It doesn't seem to be so simple. My testing LED turns on when the USB connection with another program is established, but it doesn't turn off (LOW) when that program exits.

1
1

How about having the other program send data to Arduino and Arduino knowing the connection is lost when new data isn't received? I have no idea what Arduino code would do that but just an idea.

That sounds simple enough.

const unsigned long MESSAGE_TIMEOUT = 10UL * 1000;  // 10 seconds

void setup ()
  {
  Serial.begin (115200);
  while (!Serial) ;  // wait for Serial to become active
  Serial.println ("Starting");

  }  // end of setup

unsigned long lastMessage;

void processInput ()
  {
  while (Serial.available ())
    {
    char c = Serial.read ();
    // do something with the data
    }  // end of while loop
  }  // end of processInput

void loop ()
  {
  if (Serial.available ())
    {
    lastMessage = millis ();  // remember when we last got input
    processInput ();  // now handle the input
    }  

  if (millis () - lastMessage >= MESSAGE_TIMEOUT)
    {
    // we don't seem to be receiving data  
    }

  }  // end of loop
1
2

The problem here is twofold.

Firstly, on Arduinos that don't have a direct USB connection, (i.e., boards like the Uno, Mega, etc that use a separate chip to manage the USB connection) you have no way of knowing if the USB is open or not. That information simply isn't there. The main chip just receives UART serial data (not USB data) as it arrives and that is that.

Secondly, even on the boards that have a direct USB connection (Leonardo, etc) there is no proper way to know if the port has been opened or not. The CDC/ACM protocol simply doesn't have that kind of information (a bit short sighted of them really). The closest you can do is monitor the DTR and/or RTS signals that are encapsulated in the USB data stream. It is down to the host operating system (and maybe even your own program) to exert those signals properly, so isn't foolproof even then.

To check the status of those lines in your sketch you can examine the boolean operator of the Serial object:

if (Serial) {
  // I am connected
} else {
  // I am not connected
}

But as I say that can only work on boards like the Leonard - the other indirect boards think they are always connected, since there is no such thing as a connection in UART terminology, only data.

14
  • How about having the other program send data to Arduino and Arduino knowing the connection is lost when new data isn't received? I have no idea what Arduino code would do that but just an idea.
    – Leo Ervin
    Sep 20 '15 at 16:17
  • That method is known as keepalive.
    – Majenko
    Sep 20 '15 at 16:18
  • "method" as in a function, or technique? Any example somewhere?
    – Leo Ervin
    Sep 20 '15 at 16:27
  • Technique. How you do it depends on what you are doing serial-wise at the moment.
    – Majenko
    Sep 20 '15 at 16:28
  • I'm lighting a LED when there's a USB connection between a PC program ans Arduino.
    – Leo Ervin
    Sep 20 '15 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.