added a gpib module (handle gpib connection)

Mon, 10 Dec 2007 21:19:15 +0100

author
David Douard <david.douard@logilab.fr>
date
Mon, 10 Dec 2007 21:19:15 +0100
changeset 2
cd9efa64f6da
parent 1
0670b1f5c155
child 3
e2587668ec72

added a gpib module (handle gpib connection)

gpib.py file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpib.py	Mon Dec 10 21:19:15 2007 +0100
@@ -0,0 +1,43 @@
+"""
+gpib: create serial connection to GPIB-USB device (ProLogix is the
+only supported device for now).
+"""
+import serial
+from serial.serialutil import SerialException
+import time
+
+class ConnectionError(Exception):
+    pass
+
+class GPIB(object):
+    _retries = 10
+    def __init__(self, device="/dev/ttyUSB0", baudrate=115200, timeout=0.1,
+                    address=0, mode=1):        
+        self._cnx = serial.Serial(port=device, baudrate=baudrate, timeout=timeout)
+        self._timeout = timeout
+        
+        self.set_mode(mode)
+        if mode == 1:
+            self.set_address(address)
+
+    def _set_cmd(self, cmd, value):
+        self._cnx.write('++%s %d\r'%(cmd, value))
+        rval = self._read()
+        if not rval.isdigit() or int(rval) != value:
+            raise ConnectionError("Can't set GPIB %s to %s"%(cmd, value))
+        
+    def set_address(self, address):        
+        self._set_cmd('addr', address)
+        self._adress = address
+        
+    def set_mode(self, mode):
+        self._set_cmd('mode', mode)
+        self._mode = mode
+        
+    def _read(self):
+        for i in range(self._retries):    
+            rdata = self._cnx.readline()
+            if rdata.strip() != "":
+                break
+            time.sleep(self._timeout)
+        return rdata

mercurial