Moving scanSerial into its own module
authorPaul Robinson <darkraver@gmail.com>
Mon, 5 Nov 2012 16:19:19 +0000 (16:19 +0000)
committerPaul Robinson <darkraver@gmail.com>
Mon, 5 Nov 2012 16:19:19 +0000 (16:19 +0000)
Moving scanSerial so it can be used by other things other than pyobd

obd_recorder.py [new file with mode: 0644]
obd_utils.py [new file with mode: 0644]
pyobd

diff --git a/obd_recorder.py b/obd_recorder.py
new file mode 100644 (file)
index 0000000..fb242b8
--- /dev/null
@@ -0,0 +1,23 @@
+import obd_io
+import serial
+import platform
+
+from obd_utils import scanSerial
+
+class OBD_Recorder():
+    def connect(self):
+        portnames = scanSerial()
+        print portnames
+        for port in portnames:
+            self.port = obd_io.OBDPort(port, None, 2, 2)
+            if(self.port.State == 0):
+                self.port.close()
+                self.port = None
+            else:
+                break
+
+        if(self.port):
+            print "Connected to "+self.port.port.name
+            
+o = OBD_Recorder()
+o.connect()
\ No newline at end of file
diff --git a/obd_utils.py b/obd_utils.py
new file mode 100644 (file)
index 0000000..6452c7e
--- /dev/null
@@ -0,0 +1,48 @@
+import serial
+import platform
+
+def scanSerial():
+    """scan for available ports. return a list of serial names"""
+    available = []
+    for i in range(256):
+      try: #scan standart ttyS*
+        s = serial.Serial(i)
+        available.append(s.portstr)
+        s.close()   # explicit close 'cause of delayed GC in java
+      except serial.SerialException:
+        pass
+    for i in range(256):
+      try: #scan USB ttyACM
+        s = serial.Serial("/dev/ttyACM"+str(i))
+        available.append(s.portstr)
+        s.close()   # explicit close 'cause of delayed GC in java
+      except serial.SerialException:
+        pass
+    for i in range(256):
+      try:
+        s = serial.Serial("/dev/ttyUSB"+str(i))
+        available.append(s.portstr)
+        s.close()   # explicit close 'cause of delayed GC in java
+      except serial.SerialException:
+        pass
+    for i in range(256):
+      try:
+        s = serial.Serial("/dev/ttyd"+str(i))
+        available.append(s.portstr)
+        s.close()   # explicit close 'cause of delayed GC in java
+      except serial.SerialException:
+        pass
+        
+    # ELM-USB shows up as /dev/tty.usbmodemXXXX, where XXXX is a changing hex string
+    # on connection; so we have to search through all 64K options
+    if len(platform.mac_ver()[0])!=0:  #search only on MAC
+      for i in range (65535):
+        extension = hex(i).replace("0x","", 1)
+        try:
+          s = serial.Serial("/dev/tty.usbmodem"+extension)
+          available.append(s.portstr)
+          s.close()
+        except serial.SerialException:
+          pass 
+    
+    return available
diff --git a/pyobd b/pyobd
index a8180a4..dbc3b4e 100755 (executable)
--- a/pyobd
+++ b/pyobd
@@ -40,6 +40,7 @@ import webbrowser #open browser from python
 
 from obd2_codes import pcodes
 from obd2_codes import ptest
+from obd_utils import scanSerial
 
 from wx.lib.mixins.listctrl import ListCtrlAutoWidthMixin
 
@@ -599,59 +600,12 @@ the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  0211
         self.ThreadControl=1
         self.nb.SetSelection(3)
         
-    
-    def scanSerial(self):
-        """scan for available ports. return a list of serial names"""
-        available = []
-        for i in range(256):
-          try: #scan standart ttyS*
-            s = serial.Serial(i)
-            available.append(s.portstr)
-            s.close()   # explicit close 'cause of delayed GC in java
-          except serial.SerialException:
-            pass
-        for i in range(256):
-          try: #scan USB ttyACM
-            s = serial.Serial("/dev/ttyACM"+str(i))
-            available.append(s.portstr)
-            s.close()   # explicit close 'cause of delayed GC in java
-          except serial.SerialException:
-            pass
-        for i in range(256):
-          try:
-            s = serial.Serial("/dev/ttyUSB"+str(i))
-            available.append(s.portstr)
-            s.close()   # explicit close 'cause of delayed GC in java
-          except serial.SerialException:
-            pass
-        for i in range(256):
-          try:
-            s = serial.Serial("/dev/ttyd"+str(i))
-            available.append(s.portstr)
-            s.close()   # explicit close 'cause of delayed GC in java
-          except serial.SerialException:
-            pass
-            
-        # ELM-USB shows up as /dev/tty.usbmodemXXXX, where XXXX is a changing hex string
-        # on connection; so we have to search through all 64K options
-        if len(platform.mac_ver()[0])!=0:  #search only on MAC
-          for i in range (65535):
-            extension = hex(i).replace("0x","", 1)
-            try:
-              s = serial.Serial("/dev/tty.usbmodem"+extension)
-              available.append(s.portstr)
-              s.close()
-            except serial.SerialException:
-              pass 
-        
-        return available
-
     def Configure(self,e = None):
         id = 0
         diag = wx.Dialog(self.frame, id, title="Configure")
         sizer = wx.BoxSizer(wx.VERTICAL)
         
-        ports = self.scanSerial()
+        ports = scanSerial()
         rb = wx.RadioBox(diag, id, "Choose Serial Port",
                         choices = ports, style = wx.RA_SPECIFY_COLS,
                         majorDimension = 2)