Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if device is connected Pyserial

I am connecting with my Arduino through a USB port and sending data to it by using PySerial module. At first I can check if the device is connected by using this code:

try:
    ser = serial.Serial("COM3", 9600)
except serial.serialutil.SerialException:
    print "Arduino not connected"

Now what I want to do is to check periodically if the Arduino is still connected to the computer. I tried ser.isOpen() but this returns true even if the Arduino is disconnected. I would also like to know how to reconnect the device. I mean once you disconnect the device the program can no longer send any data to Arduino.

like image 964
Asmastas Maz Avatar asked Jan 10 '14 17:01

Asmastas Maz


5 Answers

Most of the answers propose 2 approaches:

  1. In some point of the code, send some sort of message through serial to check if your device is still alive
  2. Start a separate thread and continuously check if the device is alive by opening a communication

The problem with the first solution is that you are not always checking the connection, but only checking in some specific points: this solution isn't very elegant and if badly written could even be not working.

The second solution solves the problem of the first solution, but introduces a new problem: checking the connection, or worst sending a message, in a threaded loop will cause problem or may even interrupt the connection to the device from other functions.

A solution that allows you to constantly check the connection without monopolizing the communication involves the reading of the existing COM:

import serial.tools.list_ports
myports = [tuple(p) for p in list(serial.tools.list_ports.comports())]
print myports

output:

[(u'COM3', u'Arduino Due Programming Port (COM3)', u'some more data...'),
(u'COM6', u'USB Serial Port (COM6)', u'some more data...'),
(u'COM100', u'com0com - serial port emulator (COM100)', u'some more data...')]

then we save the tuple that contains our port:

arduino_port = [port for port in myports if 'COM3' in port ][0]

then we create a function that checks if this port is still present:

import time

def check_presence(correct_port, interval=0.1):
    while True:
        myports = [tuple(p) for p in list(serial.tools.list_ports.comports())]
        if arduino_port not in myports:
            print "Arduino has been disconnected!"
            break
        time.sleep(interval)

At last, we run this function as a daemon thread:

import threading
port_controller = threading.Thread(target=check_presence, args=(arduino_port, 0.1,))
port_controller.setDaemon(True)
port_controller.start()

in this way, you'll check each 0.1 secs if the arduino is still connected, and the thread will end when arduino is disconnected or all other activities have ended

like image 69
Gsk Avatar answered Nov 01 '22 04:11

Gsk


You can set a timeout.

import serial

ser = serial

try:
  ser = serial.Serial("COM3", 9600, timeout=10)

  while ser.read():
    print 'serial open'

  print 'serial closed'
  ser.close()

except serial.serialutil.SerialException:
  print 'exception'
like image 40
mchant Avatar answered Nov 01 '22 02:11

mchant


Unfortunately, the best way I can find to do this is to try some communication and see if it fails. A pretty safe way would be:

try:
   ser.inWaiting()
except:
   print "Lost connection!"

You'll probably still want to close the connection with a ser.close() after the connection is lost, although you may need to place that in a "try:except" block also.

like image 30
Peter Avatar answered Nov 01 '22 03:11

Peter


 import serial
 import time

 ser = serial.Serial()
 ser.braudrate = 115200
 ser.port = "/dev/ttyUSB0"
 ser.open()

 print(ser.name)
 if ser.isOpen():
    print("serial is open!")

 ser.close()
like image 2
Harshan Gowda Avatar answered Nov 01 '22 04:11

Harshan Gowda


For example to detect ttyUSB0:

import os

x=os.system("ls /dev/ttyUSB0")

if x==0:
    print "connected"
else:
    print "disconnected"
like image 1
Muhammad Satrio Pakarti Avatar answered Nov 01 '22 02:11

Muhammad Satrio Pakarti