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.

Greenonline
3,1527 gold badges36 silver badges48 bronze badges
asked Sep 20, 2015 at 15:36
1

2 Answers 2

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
answered Sep 20, 2015 at 21:51
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.

answered Sep 20, 2015 at 15:48
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. Commented Sep 20, 2015 at 16:17
  • That method is known as keepalive. Commented Sep 20, 2015 at 16:18
  • "method" as in a function, or technique? Any example somewhere? Commented Sep 20, 2015 at 16:27
  • Technique. How you do it depends on what you are doing serial-wise at the moment. Commented Sep 20, 2015 at 16:28
  • I'm lighting a LED when there's a USB connection between a PC program ans Arduino. Commented Sep 20, 2015 at 16:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.