# HG changeset patch # User Daniel O'Connor # Date 1242200821 -34200 # Node ID 51d1fc44a7533525b55542f6239d76c030d57462 # Parent 6b360c30eed648c71d485dda12b72ef0f89d3984 Add variable timeout for reads. Modify the example to use it & print how long curve takes. diff -r 6b360c30eed6 -r 51d1fc44a753 example.py --- a/example.py Wed May 13 17:09:14 2009 +0930 +++ b/example.py Wed May 13 17:17:01 2009 +0930 @@ -10,17 +10,17 @@ print "Found device" u.write("*IDN?") - print "ID is..", - print u.read() + print "IDN reports " + u.read() u.write("DATA:ENC RIB") # Big endian signed u.write("DATA:WIDTH 2") # 2 bytes wide u.write("SELECT:CH1 ON") # Turn channel 1 on u.write("DATA:SOURCE CH1") # Set the curve source to channel 1 u.write("CURVE?") # Ask for the curve data - time.sleep(1) # Wait for the data.. - # XXX: need some way of polling - result = u.read() + then = time.time() + result = u.read(1.0) # Takes the CRO a while for this + now = time.time() + print "CURVE read took %f milliseconds" % ((now - then) * 1000.0) data = result[13:] # Chop off the header dattype = numpy.dtype('>h') # Big endian 16 bit quantity ary = numpy.fromstring(data, dtype = dattype) diff -r 6b360c30eed6 -r 51d1fc44a753 usb488.py --- a/usb488.py Wed May 13 17:09:14 2009 +0930 +++ b/usb488.py Wed May 13 17:17:01 2009 +0930 @@ -195,8 +195,11 @@ if wrote != len(chunk): raise "Short write, got %d, expected %d" % (wrote, len(chunk)) - def read(self): - """Read data from the device""" + def read(self, timeout = 0.2): + """Read data from the device, waits for up to timeout seconds for each USB transaction""" + + # Mangle into milliseconds + _timeout = int(timeout * 1000.0) # Maximum we accept at once # Was 2^31 - 1 but that seems to make things take too long to @@ -218,12 +221,12 @@ # Send it #print "Sending " + str(pkt) - wrote = self.handle.bulkWrite(self.bulkoutep, pkt) + wrote = self.handle.bulkWrite(self.bulkoutep, pkt, _timeout) if wrote != len(pkt): print "Short write, got %d, expected %d" % (wrote, len(pkt)) #print "Reading.." - read = self.handle.bulkRead(self.bulkinep, datalen) + read = self.handle.bulkRead(self.bulkinep, datalen, _timeout) #print "Read %s bytes: %s" % (len(read), str(read)) if read[0] != DEV_DEP_MSG_IN: