Commits

Philippe Pinard committed 2c095a9

Add auto disconnect function.

Comments (0)

Files changed (1)

src/pypfeiffer/interface.py

 __license__ = "GPL v3"
 
 # Standard library modules.
+import atexit
 import logging
 
 # Third party modules.
                                   timeout=1)
         self._ser.port = comport
 
+        atexit.register(self._auto_disconnect)
+
     def __enter__(self):
         self.connect()
         return self
     def __exit__(self, exc_type, exc_value, traceback):
         self.disconnect()
 
+    def _auto_disconnect(self):
+        if self.is_connected():
+            self.disconnect()
+
     def is_connected(self):
         """
         Returns whether the interface is connected to the unit.