user13501: 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.

user10794: 
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


user4143: 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.

13501: <pre><code>if (Serial.available()) {
        digitalWrite(7, HIGH);
} else {
        digitalWrite(7, LOW);
}
</code></pre>

<p>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.</p>
 10794: 

<blockquote>
  <p>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.</p>
</blockquote>

<p>That sounds simple enough.</p>

<pre class="lang-C++ prettyprint-override"><code>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 &gt;= MESSAGE_TIMEOUT)
    {
    // we don't seem to be receiving data  
    }

  }  // end of loop
</code></pre>
 4143: <p>The problem here is twofold.</p>

<p>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 (<em>not</em> USB data) as it arrives and that is that.</p>

<p>Secondly, even on the boards that have a direct USB connection (Leonardo, etc) there is no <em>proper</em> 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.</p>

<p>To check the status of those lines in your sketch you can examine the <em>boolean operator</em> of the <code>Serial</code> object:</p>

<pre><code>if (Serial) {
  // I am connected
} else {
  // I am not connected
}
</code></pre>

<p>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 <em>connection</em> in UART terminology, only data.</p>
