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.
-
1Cross posted on Arduino Forum and a second time.Nick Gammon– Nick Gammon ♦2015年09月20日 23:01:55 +00:00Commented Sep 20, 2015 at 23:01
2 Answers 2
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
-
And an issue on the Python side... arduino.stackexchange.com/questions/16220/…Leo Ervin– Leo Ervin2015年09月20日 21:53:06 +00:00Commented Sep 20, 2015 at 21:53
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.
-
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– Leo Ervin2015年09月20日 16:17:57 +00:00Commented Sep 20, 2015 at 16:17
-
That method is known as keepalive.Majenko– Majenko2015年09月20日 16:18:50 +00:00Commented Sep 20, 2015 at 16:18
-
"method" as in a function, or technique? Any example somewhere?Leo Ervin– Leo Ervin2015年09月20日 16:27:01 +00:00Commented Sep 20, 2015 at 16:27
-
Technique. How you do it depends on what you are doing serial-wise at the moment.Majenko– Majenko2015年09月20日 16:28:34 +00:00Commented Sep 20, 2015 at 16:28
-
I'm lighting a LED when there's a USB connection between a PC program ans Arduino.Leo Ervin– Leo Ervin2015年09月20日 16:51:32 +00:00Commented Sep 20, 2015 at 16:51