Mercurial > ~darius > hgwebdir.cgi > pyinst
comparison 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 |
comparison
equal
deleted
inserted
replaced
0:a43a47dfc902 | 1:e2089824735a |
---|---|
165 if self.tag == 0: | 165 if self.tag == 0: |
166 self.tag += 1 | 166 self.tag += 1 |
167 | 167 |
168 def write(self, data): | 168 def write(self, data): |
169 """Send data (string) to the scope""" | 169 """Send data (string) to the scope""" |
170 | |
170 orddata = map(ord, data) | 171 orddata = map(ord, data) |
172 # The device needs a \n at the end, enfore this | |
173 if orddata[-1] != '\n': | |
174 orddata += [ord('\n')] | |
171 datalen = len(orddata) | 175 datalen = len(orddata) |
172 | 176 |
173 # Build the packet | 177 # Build the packet |
174 pkt = [ DEV_DEP_MSG_OUT, self.tag, ~self.tag & 0xff, 0x00, | 178 pkt = [ DEV_DEP_MSG_OUT, self.tag, ~self.tag & 0xff, 0x00, |
175 datalen & 0xff, datalen >> 8 & 0xff, datalen >> 16 & 0xff, | 179 datalen & 0xff, datalen >> 8 & 0xff, datalen >> 16 & 0xff, |
188 # Split it up into maxPacket sized chunks and send.. | 192 # Split it up into maxPacket sized chunks and send.. |
189 while len(pkt) > 0: | 193 while len(pkt) > 0: |
190 chunk = pkt[0:self.maxPacket] | 194 chunk = pkt[0:self.maxPacket] |
191 pkt = pkt[self.maxPacket:] | 195 pkt = pkt[self.maxPacket:] |
192 | 196 |
193 print "Sending %s bytes of data: %s" % (len(chunk), chunk) | 197 #print "Sending %s bytes of data: %s" % (len(chunk), chunk) |
194 wrote = self.handle.bulkWrite(self.bulkoutep, chunk) | 198 wrote = self.handle.bulkWrite(self.bulkoutep, chunk) |
195 if wrote != len(chunk): | 199 if wrote != len(chunk): |
196 raise "Short write, got %d, expected %d" % (wrote, len(chunk)) | 200 raise "Short write, got %d, expected %d" % (wrote, len(chunk)) |
197 | 201 |
198 def read(self): | 202 def read(self): |
199 """Read data from the device""" | 203 """Read data from the device""" |
200 | 204 |
201 | 205 # Maximum we accept at once |
202 datalen = 1024 | 206 # Was 2^31 - 1 but that seems to make things take too long to |
203 # Ask the device to send us something | 207 # read (perhaps libusb tries to malloc it..) |
204 pkt = [ REQUEST_DEV_DEP_MSG_IN, self.tag, ~self.tag & 0xff, 0x00, | 208 datalen = 10240 |
205 datalen & 0xff, datalen >> 8 & 0xff, datalen >> 16 & 0xff, | 209 data = [] |
206 datalen >> 24 & 0xff, 0, 0, 0, 0] | |
207 | |
208 # Bump tag | |
209 self.incrtag() | |
210 | |
211 # Send it | |
212 wrote = self.handle.bulkWrite(self.bulkoutep, pkt) | |
213 if wrote != len(pkt): | |
214 print "Short write, got %d, expected %d" % (wrote, len(pkt)) | |
215 | |
216 read = self.handle.bulkRead(self.bulkinep, datalen) | |
217 print "Read %s bytes: %s" % (len(read), str(read)) | |
218 return read | |
219 | 210 |
211 while True: | |
212 # Ask the device to send us something | |
213 pkt = [ REQUEST_DEV_DEP_MSG_IN, self.tag, ~self.tag & 0xff, 0x00, | |
214 datalen & 0xff, datalen >> 8 & 0xff, datalen >> 16 & 0xff, | |
215 datalen >> 24 & 0xff, 0, 0, 0, 0] | |
216 | |
217 # Expected tag | |
218 exptag = self.tag | |
219 | |
220 # Bump tag | |
221 self.incrtag() | |
222 | |
223 # Send it | |
224 #print "Sending " + str(pkt) | |
225 wrote = self.handle.bulkWrite(self.bulkoutep, pkt) | |
226 if wrote != len(pkt): | |
227 print "Short write, got %d, expected %d" % (wrote, len(pkt)) | |
228 | |
229 #print "Reading.." | |
230 read = self.handle.bulkRead(self.bulkinep, datalen) | |
231 #print "Read %s bytes: %s" % (len(read), str(read)) | |
232 | |
233 if read[0] != DEV_DEP_MSG_IN: | |
234 raise "Unexpected Msg ID, got %s expected %d" % (read[0], DEV_DEP_MSG_IN) | |
235 if read[1] != exptag: | |
236 raise "Unexpected tag, got %d expected %d" % (read[1], exptag) | |
237 if read[2] != ~exptag & 0xff: | |
238 raise "Unexpected tag inverse, got %d expected %d" % (read[1], ~exptag & 0xff) | |
239 | |
240 actualdata = read[4] | read[5] << 8 | read[6] << 16 | read[7] << 24 | |
241 #print "Computed datalen is %d" % (actualdata) | |
242 data += read[12:12 + actualdata] | |
243 if read[8] & 0x01: | |
244 #print "Breaking out due to EOM" | |
245 break | |
246 | |
247 # Stringify result for easier consumption | |
248 result = reduce(lambda x, y: x+y, map(chr, data)) | |
249 # Trim off \n if present | |
250 if result[-1] == '\n': | |
251 result = result[0:-1] | |
252 | |
253 return result | |
254 | |
220 def find488(): | 255 def find488(): |
221 """Search for a USB488 device, returns a handle, iface, dev tuple for it""" | 256 """Search for a USB488 device, returns a handle, iface, dev tuple for it""" |
222 | 257 |
223 busses = usb.busses() | 258 busses = usb.busses() |
224 | 259 |