From 3f6f1acd867e5c97c75bb6c05e20d4be292b7d12 Mon Sep 17 00:00:00 2001
From: "Michael D. M. Dryden" <mdryden@chem.utoronto.ca>
Date: Tue, 17 Nov 2015 15:05:13 -0500
Subject: [PATCH] Handles USB disconnection more gracefully.

---
 dstat-interface/dstat_comm.py | 46 +++++++++++++++++++----------------
 dstat-interface/main.py       | 16 +++++++-----
 2 files changed, 35 insertions(+), 27 deletions(-)

diff --git a/dstat-interface/dstat_comm.py b/dstat-interface/dstat_comm.py
index 9e2641a..ebd496c 100644
--- a/dstat-interface/dstat_comm.py
+++ b/dstat-interface/dstat_comm.py
@@ -44,8 +44,9 @@ def version_check(ser_port):
     Arguments:
     ser_port -- address of serial port to use
     """
-
+    
     ser = delayedSerial(ser_port, baudrate=1000000, timeout=1)
+        
     ser.write("ck")
     
     ser.flushInput()
@@ -171,31 +172,34 @@ class Experiment(object):
         data to self.main_pipe as result of self.data_handler).
         """
         scan = 0
-        while True:
-            if self.main_pipe.poll():
-                if self.main_pipe.recv() == 'a':
-                    self.serial.write('a')
-                    print "ABORT!"
-                    return False
-                        
-            for line in self.serial:
+        try:
+            while True:
                 if self.main_pipe.poll():
                     if self.main_pipe.recv() == 'a':
                         self.serial.write('a')
                         print "ABORT!"
                         return False
-                        
-                if line.startswith('B'):
-                    self.main_pipe.send(self.data_handler(
-                                 (scan, self.serial.read(size=self.databytes))))
-                elif line.startswith('S'):
-                    scan += 1
-                elif line.startswith("#"):
-                    print line
-                elif line.lstrip().startswith("no"):
-                    print line
-                    self.serial.flushInput()
-                    return True
+                            
+                for line in self.serial:
+                    if self.main_pipe.poll():
+                        if self.main_pipe.recv() == 'a':
+                            self.serial.write('a')
+                            print "ABORT!"
+                            return False
+                            
+                    if line.startswith('B'):
+                        self.main_pipe.send(self.data_handler(
+                                    (scan, self.serial.read(size=self.databytes))))
+                    elif line.startswith('S'):
+                        scan += 1
+                    elif line.startswith("#"):
+                        print line
+                    elif line.lstrip().startswith("no"):
+                        print line
+                        self.serial.flushInput()
+                        return True
+        except serial.SerialException:
+            return False
     
     
     def data_handler(self, data_input):
diff --git a/dstat-interface/main.py b/dstat-interface/main.py
index 2e477f9..1461a38 100755
--- a/dstat-interface/main.py
+++ b/dstat-interface/main.py
@@ -200,14 +200,12 @@ class Main(object):
         """Disconnect from DStat."""
         try:
             self.on_pot_stop_clicked()
+            gobject.source_remove(self.ocp_proc) # Stop OCP measurements
+            self.serial.close()
+            del(self.serial)
         except AttributeError:
             pass
-            
-        # Stop OCP measurements
-        gobject.source_remove(self.ocp_proc)
         
-        self.serial.close()
-        del(self.serial)
         self.connected = False
         self.serial_connect.set_sensitive(True)
         self.serial_disconnect.set_sensitive(False)
@@ -641,8 +639,14 @@ class Main(object):
         """
         try:
             if self.recv_p.poll():
+                incoming = self.recv_p.recv()
+                
+                if isinstance(incoming, basestring): #test if incoming is str
+                    self.on_serial_disconnect_clicked()
+                    return False
+                    
                 data = "".join(["OCP: ",
-                                "{0:.3f}".format(self.recv_p.recv()),
+                                "{0:.3f}".format(incoming),
                                 " V"])
                 self.ocp_disp.set_text(data)
 
-- 
GitLab