diff usb488.py @ 55:ad5942d22f78

Use BaseException rather than strings. Add ask method. Wrap tag to a byte.
author Daniel O'Connor <doconnor@gsoft.com.au>
date Tue, 08 Dec 2020 13:59:05 +1030
parents 876d951bbcc0
children 91b476ebc0f2
line wrap: on
line diff
--- a/usb488.py	Thu May 02 17:55:20 2013 +0930
+++ b/usb488.py	Tue Dec 08 13:59:05 2020 +1030
@@ -32,6 +32,7 @@
 # http://svn.openmoko.org/developers/werner/ahrt/host/tmc/README
 # http://www.home.agilent.com/agilent/redirector.jspx?action=ref&cname=AGILENT_EDITORIAL&ckey=1189335&lc=eng&cc=US&nfr=-35560.0.00
 # linux-2.6.29.3/drivers/usb/class/usbtmc.c
+# http://sdpha2.ucsd.edu/Lab_Equip_Manuals/usbtmc_usb488_subclass_1_00.pdf
 #
 
 import usb
@@ -132,7 +133,7 @@
             if found:
                 break
         if not found:
-            raise "Could not find a suitable USB device"
+            raise BaseException("Could not find a suitable USB device")
     
         # Open the device and claim the USB interface that supports the spec
         handle = dev.open()
@@ -154,7 +155,7 @@
             if ep.type == usb.ENDPOINT_TYPE_INTERRUPT and \
                    ep.address & usb.ENDPOINT_IN == usb.ENDPOINT_IN:
                 self.intrep = ep.address
-                                 
+
             if ep.type == usb.ENDPOINT_TYPE_BULK:
                 if ep.address & usb.ENDPOINT_IN == usb.ENDPOINT_IN:
                     self.bulkinep = ep.address
@@ -168,11 +169,11 @@
 
         # Data from the scope (mandatory)
         if self.bulkinep == None:
-            raise "Can't find bulk-in endpoint"
+            raise BaseException("Can't find bulk-in endpoint")
 
         # Data to the scope (mandatory)
         if self.bulkoutep == None:
-            raise "Can't find bulk-out endpoint"
+            raise BaseException("Can't find bulk-out endpoint")
 
         self.tag = 1
 
@@ -184,7 +185,7 @@
         return rtn
     
     def incrtag(self):
-        self.tag += 1
+        self.tag = (self.tag + 1) % 255
         if self.tag == 0:
             self.tag += 1
 
@@ -220,7 +221,7 @@
             #print "Sending %s bytes of data: %s" % (len(chunk), chunk)
             wrote = self.handle.bulkWrite(self.bulkoutep, chunk)
             if wrote != len(chunk):
-                raise "Short write, got %d, expected %d" % (wrote, len(chunk))
+                raise BaseException("Short write, got %d, expected %d" % (wrote, len(chunk)))
 
     def read(self, timeout = None):
         """Read data from the device, waits for up to timeout seconds for each USB transaction"""
@@ -260,11 +261,11 @@
             #print "Read %s bytes: %s" % (len(read), str(read))
 
             if read[0] != DEV_DEP_MSG_IN:
-                raise "Unexpected Msg ID, got %s expected %d" % (read[0], DEV_DEP_MSG_IN)
+                raise BaseException("Unexpected Msg ID, got %s expected %d" % (read[0], DEV_DEP_MSG_IN))
             if read[1] != exptag:
-                raise "Unexpected tag, got %d expected %d" % (read[1], exptag)
+                raise BaseException("Unexpected tag, got %d expected %d" % (read[1], exptag))
             if read[2] != ~exptag & 0xff:
-                raise "Unexpected tag inverse, got %d expected %d" % (read[1], ~exptag & 0xff)
+                raise BaseException("Unexpected tag inverse, got %d expected %d" % (read[1], ~exptag & 0xff))
 
             actualdata = read[4] | read[5] << 8 | read[6] << 16 | read[7] << 24
             #print "Computed datalen is %d" % (actualdata)
@@ -280,7 +281,11 @@
             result = result[0:-1]
 
         return result
-    
+
+    def ask(self, s, timeout = None):
+        self.write(s)
+        return self.read(timeout = None)
+
     def isConnected(self):
         """Check if the device is present"""