checking if Arduino is connected (serial USB) to a PC?

It loops for 10 seconds in the sense that the detection code block will loop for 10 seconds.
This is the code I had

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

This is in the loop function.

Difference seems to me that you do Serial.available() only for 10 seconds. That's all, is it not? And I said that Serial.available() doesn't detect properly when connection is lost.

And since in my code Arduino writes to the Serial itself in a loop, won't Serial.available() always be true and give no idea about connection?

The shown code is neither a loop, nor a function.

That the snippet is useless was shown by your own test.

Seriously? I just said it is in the loop function.
Here:

void setup() {
  Serial.begin(9600);

  pinMode(7,  OUTPUT);
  
}

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

This doesn't do digitalWrite(7, HIGH); all the time when communicating with another program or in the Serial Monitor and do digitalWrite(7, LOW); when not. It does digitalWrite(7, HIGH); when connected, then doesn't do digitalWrite(7, LOW); ever after disconnecting.
That is the problem.
digitalWrite(7, LOW); only happens for a moment when establishing a new connection (maybe old buffer gets cleared then and thats why).

ghosttrain:
Difference seems to me that you do Serial.available() only for 10 seconds.

Again.

No, it does not do Serial.available for 10 seconds.

The code checks Serial.available() once and after 10 seconds.

If you want to use the Serial connection after a test with the result 'its there',
you have to remove the response from the buffer.

Your code keeps led 13 lit, if the partner happened to send something
since the last reset of the arduino,
so it does not mean somebody is there,
only that somebody has been there.

Even an unlit led does not mean nobody is connected or unwilling to communicate,
the other side simply did not send anything yet.

Right, right, my apologies.

Okay, your code checks Serial.available() once, mine checks in every loop.
But still, It does digitalWrite(7, HIGH); when connected, then doesn't do digitalWrite(7, LOW); ever after disconnecting. Like you said, it says that "somebody has been there" not that it "is" there now. I need to know the second. How? Arduino is writing to the Serial and my other PC program is reading that. How can Arduino know the other program has stopped reading (which right now only happen when disconnecttion happens).

My snippet answers your question at startup time plus 10 seconds
(the 10 seconds are there to let me type something, a pi would respond much faster).

To test again, clear the input buffer (by reading it until available() becomes 0),
send a new challange, record the time, set stillChecking = true.

You could make the code more flexibel, or use your own scheme.

I feel I'm finally getting somewhere:

To test again, clear the input buffer (by reading it until available() becomes 0),

So I hope I got this right as this is what I needed to hear. Serial.available() will return "true" even when I terminate the program communicating with Arduino via Serial because the buffer is not cleared, and to clear the buffer I need to do Serial.read() in Arduino and read as many characters as the other program wrote (for example by looping Serial.read() until Serial.available == 0) and that's it? Please tell me that's it.

Yes. :slight_smile:

Serial.available() will return a number, and any number (except 0) evaluates as true.

And doing Serial.read() enough times will make Serial.available() return 0, right?

If you only read the buffer and do nothing else, yes and very fast.

There are bytes comming in at roughly baudrate/10 per second,
if the pc keeps the pipe full.

wait....

Post #23?

Again.

If you want to detect whether your partner is still there
and you forgot to note whether you communicated with him a millisecond ago :wink: :

You empty the buffer.
You understood that it is possible to empty the buffer?
(by reading it, which is without any processing way faster than the serial stream)

Then you send an 'are you there' message.

You record the time of the send.
You record the fact, that you are waiting for a response/sign of live.

In your loop you look whether enough time elapsed to allow the other side to answer
(only if you are waiting for a response) and if so, have a look at the buffer.
Something in there -> partner alive, empty -> partner gone.

Additional stuff sent by the partner side does not hurt.

ghosttrain:
Please tell me that's it.

Yes.

That is explained in Serial Input Basics which I linked to in Reply #15.

...R

This is getting frustrating.

Here's my Arduino code:

void setup() {
  Serial.begin(9600);

  pinMode(7,  OUTPUT);
  pinMode(8,  INPUT_PULLUP);
}

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

    int val = digitalRead(8);
    if (val == HIGH) {   
        Serial.print("1");
    } else {
        Serial.print("0");    
    }
    
    Serial.println("");  // new line
  
}

The Python code merely reads what Arduino writes. How can you know it hasn't read it? Putting code to clear the buffer in Arduino wouldn't do anything.

Are you saying to make Python write something to the buffer itself?

holmes4:
You send a message via serial. you have software on the other end that replies. You write the software.

Do you read what we write?

Cross-posted to python - Pyserial serial.write() doesn't work - Arduino Stack Exchange.

And: http://arduino.stackexchange.com/questions/16207/know-the-state-of-usb-serial-connection-coonected-or-not-connected

And: make Arduino tell when the Python program stops reading from the Serial (crash, etc) - Arduino Stack Exchange

And on this forum: PySerial issues (Or just Arduino Serial issue) - Interfacing w/ Software on the Computer - Arduino Forum

I'm locking this thread because the OP continues to start new threads on this topic on Arduino StackExchange.