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..
Here is my code for the test : I "pruned" a bit for easier reading...
ARDUINO SNIPPET
#include <SoftwareSerial.h>
void setup()
{
pinMode(13,OUTPUT);
Serial.begin(9600);
}
void loop(){
if(Serial.available()<0);
{
digitalWrite(13,LOW);
}
if(Serial.available()>0);
{
digitalWrite(13,HIGH);
}
delay(2000);
Serial.flush();
}
And here is my python 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 <6000:
ser.write(b'4')
count = count + 1
print('finito')
time.sleep(2.5)
while countx <6000:
ser.write(b'5')
countx = countx + 1
print('Real finito !')
ser.close()
My test is quite easy:
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.
my questions are :
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..
If you have another method I'm interested.. TY