Better serial comm handling in LPC connector
authorLolitech
Fri, 04 Jun 2010 16:11:19 +0200
changeset 563 c74a37d156df
parent 561 4cc6eef4778f
child 564 464d470bdbd7
Better serial comm handling in LPC connector
connectors/LPC/LPCAppObject.py
connectors/LPC/LPCAppProto.py
connectors/LPC/LPCObject.py
connectors/LPC/LPCProto.py
--- a/connectors/LPC/LPCAppObject.py	Fri Jun 04 15:39:14 2010 +0200
+++ b/connectors/LPC/LPCAppObject.py	Fri Jun 04 16:11:19 2010 +0200
@@ -33,17 +33,6 @@
                                          2)      #timeout
         self.HandleSerialTransaction(IDLETransaction())
         
-    def HandleSerialTransaction(self, transaction):
-        if self.SerialConnection is not None:
-            try:
-                self.PLCStatus, res = self.SerialConnection.HandleTransaction(transaction)
-                return res
-            except LPCProtoError,e:
-                self.pluginsroot.logger.write_error(str(e)+"\n")
-                self.SerialConnection = None
-                self.PLCStatus = "Disconnected"
-                return None
-
     def StartPLC(self, debug=False):
         self.HandleSerialTransaction(STARTTransaction())
             
--- a/connectors/LPC/LPCAppProto.py	Fri Jun 04 15:39:14 2010 +0200
+++ b/connectors/LPC/LPCAppProto.py	Fri Jun 04 16:11:19 2010 +0200
@@ -15,7 +15,9 @@
             if current_plc_status is not None:
                 res = transaction.ExchangeData()
             else:
-                raise LPCProtoError("LPC transaction error - controller did not answer as expected")
+                raise LPCProtoError("controller did not answer as expected")
+        except Exception, e:
+            raise LPCProtoError("LPC transaction error : "+str(e))
         finally:
             self.TransactionLock.release()
         return LPC_STATUS.get(current_plc_status,"Broken"), res
--- a/connectors/LPC/LPCObject.py	Fri Jun 04 15:39:14 2010 +0200
+++ b/connectors/LPC/LPCObject.py	Fri Jun 04 16:11:19 2010 +0200
@@ -45,6 +45,7 @@
                 return res
             except LPCProtoError,e:
                 self.pluginsroot.logger.write_error(str(e)+"\n")
+                self.SerialConnection.close()
                 self.SerialConnection = None
                 self.PLCStatus = "Disconnected"
                 return None
--- a/connectors/LPC/LPCProto.py	Fri Jun 04 15:39:14 2010 +0200
+++ b/connectors/LPC/LPCProto.py	Fri Jun 04 16:11:19 2010 +0200
@@ -17,25 +17,32 @@
         # open serial port
 #        self.serialPort = serial.Serial( port, rate, timeout = timeout )
         # Debugging serial stuff
-        self._serialPort = serial.Serial( port, rate, timeout = timeout )
-        class myser:
-            def read(self_,cnt):
-                res = self._serialPort.read(cnt)
-                if len(res) > 16:
-                    print "Recv :", map(hex,map(ord,res[:16])), "[...]"
-                else:
-                    print "Recv :", map(hex,map(ord,res))
-                    
-                return res
-            def write(self_, str):
-                if len(str) > 16:
-                    print "Send :", map(hex,map(ord,str[:16])), "[...]"
-                else:
-                    print "Send :", map(hex,map(ord,str))
-                self._serialPort.write(str)
-            def flush(self_):
-                self._serialPort.flush()
-        self.serialPort = myser()
+        self.serialPort = serial.Serial( port, rate, timeout = timeout )
+#        class myser:
+#            def read(self_,cnt):
+#                res = self._serialPort.read(cnt)
+#                if len(res) > 16:
+#                    print "Recv :", map(hex,map(ord,res[:16])), "[...]"
+#                else:
+#                    print "Recv :", map(hex,map(ord,res))
+#                    
+#                return res
+#            def write(self_, str):
+#                if len(str) > 16:
+#                    print "Send :", map(hex,map(ord,str[:16])), "[...]"
+#                else:
+#                    print "Send :", map(hex,map(ord,str))
+#                self._serialPort.write(str)
+#            def flush(self_):
+#                self._serialPort.flush()
+#        self.serialPort = myser()
         # start with empty
         self.serialPort.flush()
     
+    def __del__(self):
+        if self.serialPort:
+            self.serialPort.close()
+
+    def close(self):
+        self.serialPort.close()
+        self.serialPort = None