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?
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.
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).
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.
If you want to detect whether your partner is still there
and you forgot to note whether you communicated with him a millisecond ago :
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.
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?