Generate " noise" properly - Arduino/serial/python
<p>I'm trying to detect if USB cable is connected or not with my ARDUINO UNO R3
All work like a charm
but I don't know if I take the right path..</p>

<p>Here is my code for the test : 
I "pruned" a bit for easier reading...</p>

<p>ARDUINO SNIPPET</p>

<pre><code>#include &lt;SoftwareSerial.h&gt;

void setup()
{
  pinMode(13,OUTPUT);
  Serial.begin(9600);

}
void loop(){

  if(Serial.available()&lt;0);
  {
    digitalWrite(13,LOW);
  }
  if(Serial.available()&gt;0);
  {
    digitalWrite(13,HIGH);
  }
delay(2000);
Serial.flush();
}
</code></pre>

<p>And here is my python code :</p>

<pre><code>import serial
#import struct
import time
ser = serial.Serial(
    port='COM80',\
    baudrate=9600,\
    parity=serial.PARITY_NONE,\
    stopbits=serial.STOPBITS_ONE,\
    bytesize=serial.EIGHTBITS,\
        timeout=1)

print("connected to: " + ser.portstr)
count = 0
countx = 0
while count &lt;6000:
    ser.write(b'4')
    count = count + 1
print('finito')
time.sleep(2.5)
while countx &lt;6000:
    ser.write(b'5')
    countx = countx + 1
print('Real finito !')
ser.close()
</code></pre>

<p>My test is quite easy:</p>

<p>I generate noise (bytes) on serial80.. and if I disconnect my serial(USB here),I feed my Arduino with a Battery Pack to finish the job.</p>

<p>my questions are :</p>

<p>Is that if I make an infinite loop on the port to do the job,
it's well or not ??
I have not enough networking knowledge to understand it..</p>

<p>If you have another method I'm interested..
TY</p>