Mercurial > ~darius > hgwebdir.cgi > pyinst
diff usb488.py @ 1:e2089824735a
Make the read support work properly & check what the device gives us.
- Comment out debugging.
- Stringify result.
- Handle >1 packet reads (theoretically).
- Validate returned packet.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Wed, 13 May 2009 15:02:09 +0930 |
parents | a43a47dfc902 |
children | adaff1c4fd6b |
line wrap: on
line diff
--- a/usb488.py Wed May 13 14:44:40 2009 +0930 +++ b/usb488.py Wed May 13 15:02:09 2009 +0930 @@ -167,7 +167,11 @@ def write(self, data): """Send data (string) to the scope""" + orddata = map(ord, data) + # The device needs a \n at the end, enfore this + if orddata[-1] != '\n': + orddata += [ord('\n')] datalen = len(orddata) # Build the packet @@ -190,7 +194,7 @@ chunk = pkt[0:self.maxPacket] pkt = pkt[self.maxPacket:] - print "Sending %s bytes of data: %s" % (len(chunk), chunk) + #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)) @@ -198,25 +202,56 @@ def read(self): """Read data from the device""" + # Maximum we accept at once + # Was 2^31 - 1 but that seems to make things take too long to + # read (perhaps libusb tries to malloc it..) + datalen = 10240 + data = [] + + while True: + # Ask the device to send us something + pkt = [ REQUEST_DEV_DEP_MSG_IN, self.tag, ~self.tag & 0xff, 0x00, + datalen & 0xff, datalen >> 8 & 0xff, datalen >> 16 & 0xff, + datalen >> 24 & 0xff, 0, 0, 0, 0] - datalen = 1024 - # Ask the device to send us something - pkt = [ REQUEST_DEV_DEP_MSG_IN, self.tag, ~self.tag & 0xff, 0x00, - datalen & 0xff, datalen >> 8 & 0xff, datalen >> 16 & 0xff, - datalen >> 24 & 0xff, 0, 0, 0, 0] + # Expected tag + exptag = self.tag + + # Bump tag + self.incrtag() - # Bump tag - self.incrtag() + # Send it + #print "Sending " + str(pkt) + wrote = self.handle.bulkWrite(self.bulkoutep, pkt) + if wrote != len(pkt): + print "Short write, got %d, expected %d" % (wrote, len(pkt)) - # Send it - wrote = self.handle.bulkWrite(self.bulkoutep, pkt) - if wrote != len(pkt): - print "Short write, got %d, expected %d" % (wrote, len(pkt)) + #print "Reading.." + read = self.handle.bulkRead(self.bulkinep, datalen) + #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) + if read[1] != exptag: + raise "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) - read = self.handle.bulkRead(self.bulkinep, datalen) - print "Read %s bytes: %s" % (len(read), str(read)) - return read - + actualdata = read[4] | read[5] << 8 | read[6] << 16 | read[7] << 24 + #print "Computed datalen is %d" % (actualdata) + data += read[12:12 + actualdata] + if read[8] & 0x01: + #print "Breaking out due to EOM" + break + + # Stringify result for easier consumption + result = reduce(lambda x, y: x+y, map(chr, data)) + # Trim off \n if present + if result[-1] == '\n': + result = result[0:-1] + + return result + def find488(): """Search for a USB488 device, returns a handle, iface, dev tuple for it"""