comparison usb488.py @ 2:adaff1c4fd6b

Rip out procedural interface and use the OO one.
author Daniel O'Connor <darius@dons.net.au>
date Wed, 13 May 2009 15:26:42 +0930
parents e2089824735a
children 62ffab79227e
comparison
equal deleted inserted replaced
1:e2089824735a 2:adaff1c4fd6b
249 # Trim off \n if present 249 # Trim off \n if present
250 if result[-1] == '\n': 250 if result[-1] == '\n':
251 result = result[0:-1] 251 result = result[0:-1]
252 252
253 return result 253 return result
254
255 def find488():
256 """Search for a USB488 device, returns a handle, iface, dev tuple for it"""
257
258 busses = usb.busses()
259
260 found = False
261 for bus in busses:
262 for dev in bus.devices:
263 for confidx in xrange(len(dev.configurations)):
264 # XXX: what do multi-interface devices look like?
265 iface = dev.configurations[confidx].interfaces[0][0]
266 # Check if this is a USB488 capable interface
267 if iface.interfaceClass == USB_CLASS_APP_SPECIFIC and \
268 iface.interfaceSubClass == USB_SUBCLASS_TMC and \
269 iface.interfaceProtocol == USB_PROTOCOL_488:
270 handle = dev.open()
271 handle.setConfiguration(1)
272 handle.claimInterface(0)
273 handle.setAltInterface(0)
274 #handle.setConfiguration(confidx)
275 #handle.claimInterface(0)
276 found = True
277 break
278
279 if found:
280 break
281
282 if not found:
283 raise "Could not find scope, check perms"
284
285 return (handle, iface, dev)
286
287 def geteps(iface):
288 """Returns a tuple of intr,input,output addresses of endpoints for the interface"""
289 intrep = bulkinep = bulkoutep = None
290
291 for ep in iface.endpoints:
292 if ep.type == usb.ENDPOINT_TYPE_INTERRUPT and \
293 ep.address & usb.ENDPOINT_IN == usb.ENDPOINT_IN:
294 intrep = ep.address
295
296 if ep.type == usb.ENDPOINT_TYPE_BULK:
297 if ep.address & usb.ENDPOINT_IN == usb.ENDPOINT_IN:
298 bulkinep = ep.address
299 else:
300 bulkoutep = ep.address
301
302 # Required for 488.2 devices, optional otherwise
303 if intrep == None:
304 print "Can't find interrup endpoint"
305
306 # Data from the scope
307 if bulkinep == None:
308 raise "Can't find bulk-in endpoint"
309
310 # Data to the scope
311 if bulkoutep == None:
312 raise "Can't find bulk-out endpoint"
313
314 return intrep, bulkinep, bulkoutep
315
316 def main(): 254 def main():
317 handle, iface, dev = find488() 255 u = USB488Device()
318 print "Found device" 256 print "Found device"
319 257
320 intrep, bulkinep, bulkoutep = geteps(iface) 258 print "ID is.."
321 print "Found endpoints" 259 u.write("*IDN?")
260 print u.read()
322 261
323 if __name__ == "__main__": 262 if __name__ == "__main__":
324 main() 263 main()
325 264