0

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

jmercier
  • 564
  • 2
  • 7
  • 17
  • It is not clear what you are asking. Are you trying to verify that you are communicating with the Arduino? Or are you asking how to write a monitor/control process on your computer? – Neapolitan Jun 05 '16 at 02:30
  • I'm trying to verify if I'm connected with the arduino.It the only way I found to fl that. But I dont if I build a small Soft GUI in c# or python , if I do that by this way.. I dont find how to listen port connected with this board, ty for your time – jmercier Jun 05 '16 at 02:32
  • If I understand correctly, you are setting pin 13 low when there is no data on the serial connection and high when there is. Presumably flush() is emptying the incoming data? (NB: the [man page](https://www.arduino.cc/en/Serial/Flush) suggests this behaviour has changed.) From python you are sending 6000 bytes, sleeping 2.5 s, sending another 6000 then done. You say this is working. So now what? Are you trying to set it up so that your python program can communicate with Arduino in the background, sending commands as needed and receiving data as it comes? – Neapolitan Jun 05 '16 at 04:17
  • Does [this answer](http://stackoverflow.com/questions/24074914/python-to-arduino-serial-read-write?rq=1) work for you? – Neapolitan Jun 05 '16 at 04:20

0 Answers0