pygpibtoolkit/prologix.py

changeset 53
8e32c806fcdd
parent 40
1bbea188a7e5
child 56
9573558f9191
--- a/pygpibtoolkit/prologix.py	Sun Feb 17 22:59:59 2008 +0100
+++ b/pygpibtoolkit/prologix.py	Mon Feb 25 18:38:27 2008 +0100
@@ -27,8 +27,9 @@
         
         self.set_mode(mode)
         self.set_address(address)
+        self._set_cmd('auto', 0)
         
-    def set_address(self, address):
+    def set_address(self, address, check=True):
         """
         Set the address of the GPIB device:
         
@@ -37,8 +38,9 @@
 
         - if the device is in GPIB_DEVICE mode, this is its address.
         """
-        self._set_cmd('addr', address)
-        self._adress = address
+        self._set_cmd('addr', address, check)
+        self._address = address
+        #self._set_cmd('auto', 0)
         
     def set_mode(self, mode):
         """
@@ -65,17 +67,34 @@
         """
         self.set_mode(0)
 
-    def send_command(self, cmd):
+    def send_command(self, cmd, address=None):
         """
         Send the specified GPIB command on the bus (must be the CIC),
         and read the answer.
+        Eventually, set the addressed device first.
         """
         assert self._mode == 1
-        self._cnx.write(cmd+'\r')
+        if address is not None:
+            self.set_address(address)
+        self._cnx.write(cmd+';\r')
         time.sleep(self._timeout) # required?
-        ret = self._cnx.readlines()
+        return self.read_eoi()
+        
+    def read_eoi(self, address=None):
+        """
+        Read the HPIB buffer from device, till EOI is performed, or timeout.
+        """
+        if address is not None:
+            self.set_address(address)
+        self._cnx.write('++read eoi\r') # idem
+        ret = ""
+        i = 0
+        while not ret.endswith('\r\n') and i<2:
+            ret += ''.join(self._cnx.readlines())
+            time.sleep(self._timeout) # required?
+            i += 1
         return ''.join(ret)
-
+            
     def check_srq(self):
         """
         Check the SRQ line
@@ -87,18 +106,31 @@
             return bool(int(ret))
         return None
 
-    def poll(self):
+    def poll(self, addresses=None):
         """
         Poll every address, and return a dictionnary
          {add: status, ...}        
-        """
-        assert self._mode == 1, "must be the Controller In Charge"
+        """        
+        assert self._mode == 1, "must be the Controller In Charge"        
+        only_one = False
+        if addresses is None:
+            addresses = range(31)
+        if not isinstance(addresses, (list, tuple)):
+            addresses = [addresses]
+            only_one = True
+        if len(addresses)==0:
+            return None
+        
         dico = {}
-        for add in range(31):
-            self._cnx.write('++spoll %d\r'%i)
+        for add in addresses:
+            self._cnx.write('++spoll %d\r'%add)
             ret = self._cnx.readline().strip()
             if ret:
-                dico[i] = int(ret)
+                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:
+            return dico.values()[0]
         return dico
     
     def _read(self):
@@ -109,9 +141,10 @@
             time.sleep(self._timeout)
         return rdata
 
-    def _set_cmd(self, cmd, value):
+    def _set_cmd(self, cmd, value, check=True):
         self._cnx.write('++%s %d\r'%(cmd, value))
-        self._cnx.write('++%s\r'%(cmd))
-        rval = self._read().strip()
-        if not rval.isdigit() or int(rval) != value:
-            raise ConnectionError("Can't set GPIB %s to %s [ret=%s]"%(cmd, value, repr(rval)))
+        if check:
+            self._cnx.write('++%s\r'%(cmd))
+            rval = self._read().strip()
+            if not rval.isdigit() or int(rval) != value:
+                raise ConnectionError("Can't set GPIB %s to %s [ret=%s]"%(cmd, value, repr(rval)))

mercurial