add a simple 'gpib_detect' command line tool and some minor fixes

Tue, 19 Aug 2008 22:47:16 +0200

author
David Douard <david.douard@logilab.fr>
date
Tue, 19 Aug 2008 22:47:16 +0200
changeset 64
624157f78b77
parent 63
1c0d92f95115
child 65
10d218fbf86f

add a simple 'gpib_detect' command line tool and some minor fixes

bin/gpib_detect file | annotate | diff | comparison | revisions
pygpibtoolkit/gpibcontroller.py file | annotate | diff | comparison | revisions
pygpibtoolkit/prologix.py file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/gpib_detect	Tue Aug 19 22:47:16 2008 +0200
@@ -0,0 +1,34 @@
+#!/usr/bin/python
+import sys
+import os
+import signal
+import time
+
+try:
+    from pygpibtoolkit.gpibcontroller import GPIBController
+except ImportError:
+    sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
+    from pygpibtoolkit.gpibcontroller import GPIBController
+
+def main():
+    import optparse
+    opt = optparse.OptionParser("A simple tool for detecting connected GPIB devices")
+    opt.add_option('-d', '--device', default="/dev/ttyUSB0",
+                   dest="device",
+                   help="Device of connected Prologix GPIB bundle [/dev/ttyUSB0]",)
+    options, argv = opt.parse_args(sys.argv)
+
+    c = GPIBController(device=options.device)
+    signal.signal(signal.SIGINT, c.stop)
+    signal.signal(signal.SIGQUIT, c.stop)
+    
+    time.sleep(1)
+    devices = c.detect_devices()
+    print "Found devices:"
+    for k in sorted(devices.keys()):
+        print "%-3d: %s"%(k, devices[k])
+    c.stop()
+    
+if __name__ == "__main__":
+    main()
+    
--- a/pygpibtoolkit/gpibcontroller.py	Tue Aug 19 22:21:44 2008 +0200
+++ b/pygpibtoolkit/gpibcontroller.py	Tue Aug 19 22:47:16 2008 +0200
@@ -232,6 +232,7 @@
         while not self._loop_interrupter.isSet():
             self._loop_interrupter.wait()
         self._loop_interrupter.clear()
+        devices = {}
         try:
             spoll = self._cnx.poll()
             for address in spoll:
@@ -241,17 +242,21 @@
                         idn = self._cnx.send_command(id_str, address).strip()
                         if idn:
                             self.register_device(address, idn)
+                            devices[address] = str(idn)
                             break
                         else:
                             # redo a spoll, it should have set the err bit
                             poll = self._cnx.poll(address)
-                            if not poll & 0x20: # 0x20 == ERR bit
-                                print "Strange, device %d did not answer to a %s command without setting its ERR bit"%(address, id_str)
+                            if (isinstance(poll, dict) and address not in poll) or (not poll & 0x20): # 0x20 == ERR bit
+                                print "WARNING: device %d did not answer to a %s command by setting its ERR bit"%(address, id_str)
+                            devices[address] = "UNKNOWN"
                     else:
-                        print "Can't retrieve IDN of device at address ", address
+                        print "WARNING: Can't retrieve IDN of device at address ", address
+                        devices[address] = "UNKNOWN"
         finally:
             self._loop_interrupter.set()
-                    
+        return devices
+    
     def register_device(self, address, idn):
         """
         Register a device manager for device at given GPIB
@@ -263,8 +268,8 @@
             self._devices[address] = devicemgr
             return devicemgr
         else:
-            print "can't find a manager for", repr(idn)
-            print deviceRegister._registry
+            print "WARNING: can't find a manager for", repr(idn)
+            #print deviceRegister._registry
 
     def idn(self, address):
         """
--- a/pygpibtoolkit/prologix.py	Tue Aug 19 22:21:44 2008 +0200
+++ b/pygpibtoolkit/prologix.py	Tue Aug 19 22:47:16 2008 +0200
@@ -137,7 +137,7 @@
                 dico[add] = int(ret)
             time.sleep(0.3) # need to wait at least 150ms (not enough on prologix)
         self.set_address(self._address)
-        if only_one:
+        if only_one and dico:
             return dico.values()[0]
         return dico
     

mercurial