def __send_and_receive(self, in_timeout=200, out_timeout=9999): """ Send bulkout and and receive bulkin as a response. Arguments: None Keywords: in_timeout = bulkin timeout in ms out_timeout = bilkin timeout in ms Return: the data returned by the usb device. """ done = False while not done: val = usb.bulk_write(self.libusb_handle, self.usb_bulkout_ep_address, self.output_buffer, out_timeout) if val < 0: raise IOError, "error sending usb output" try: numbytes = usb.bulk_read(self.libusb_handle, self.usb_bulkin_ep_address, self.input_buffer, in_timeout) debug_print('usb SR bytes read: %d' % (numbytes, ), comma=False) done = True except usb.USBNoDataAvailableError: debug_print('usb SR: fail', comma=False) sys.stdout.flush() return
def get_analog_input_buffer_rawLE(self): if not self.real_device: outbuf = np.array([],dtype='<u2') # unsigned 2 byte little endian return outbuf EP_LEN = 256 INPUT_BUFFER = ctypes.create_string_buffer(EP_LEN) bufs = [] got_bytes = False timeout = 50 # msec cnt = 0 # Count number of times endpoint has been read min_cnt = 2 # Minimum number of times end point should be read while 1: # keep pumping until no more data try: with self._lock: n_bytes = usb.bulk_read(self._libusb_handle, (ENDPOINT_DIR_IN|ANALOG_EPNUM), INPUT_BUFFER, timeout) except usb.USBNoDataAvailableError: break cnt += 1 n_elements = n_bytes//2 buf = np.fromstring(INPUT_BUFFER.raw,dtype='<u2') # unsigned 2 byte little endian buf = buf[:n_elements] bufs.append(buf) if (n_bytes < EP_LEN) and (cnt >= min_cnt): break # don't bother waiting for data to dribble in if len(bufs): outbuf = np.hstack(bufs) else: outbuf = np.array([],dtype='<u2') # unsigned 2 byte little endian return outbuf
def get_analog_input_buffer_rawLE(self): if not self._have_trigger: outbuf = np.array([],dtype='<u2') # unsigned 2 byte little endian return outbuf EP_LEN = 256 INPUT_BUFFER = ctypes.create_string_buffer(EP_LEN) bufs = [] got_bytes = False timeout = 50 # msec while 1: # keep pumping until no more data try: with self._lock: n_bytes = usb.bulk_read(self._libusb_handle, (ENDPOINT_DIR_IN|ANALOG_EPNUM), INPUT_BUFFER, timeout) except usb.USBNoDataAvailableError: break n_elements = n_bytes//2 buf = np.fromstring(INPUT_BUFFER.raw,dtype='<u2') # unsigned 2 byte little endian buf = buf[:n_elements] bufs.append(buf) if n_bytes < EP_LEN: break # don't bother waiting for data to dribble in if len(bufs): outbuf = np.hstack(bufs) else: outbuf = np.array([],dtype='<u2') # unsigned 2 byte little endian return outbuf
def send_buf(self,return_input=False): buf = self.OUTPUT_BUFFER # shorthand #buf[9] = chr(1) #print 'ord(buf[8])',ord(buf[8]) #print 'ord(buf[9])',ord(buf[9]) if 1: val = usb.bulk_write(self.libusb_handle, 0x06, buf, 9999) debug('set_output_durations result: %d'%(val,)) if 1: INPUT_BUFFER = ctypes.create_string_buffer(16) try: val = usb.bulk_read(self.libusb_handle, 0x82, INPUT_BUFFER, 1000) if 0: print 'framecount LE:' print ord(INPUT_BUFFER[0]) print ord(INPUT_BUFFER[1]) print 'counter ticks:' print ord(INPUT_BUFFER[8]) print ord(INPUT_BUFFER[9]) if 0: print 'read',val except usb.USBNoDataAvailableError: if 0: sys.stdout.write('?') sys.stdout.flush() val = None if return_input: return INPUT_BUFFER
def send_buf(self, return_input=False): buf = self.OUTPUT_BUFFER # shorthand #buf[9] = chr(1) #print 'ord(buf[8])',ord(buf[8]) #print 'ord(buf[9])',ord(buf[9]) if 1: val = usb.bulk_write(self.libusb_handle, 0x06, buf, 9999) debug('set_output_durations result: %d' % (val, )) if 1: INPUT_BUFFER = ctypes.create_string_buffer(16) try: val = usb.bulk_read(self.libusb_handle, 0x82, INPUT_BUFFER, 1000) if 0: print 'framecount LE:' print ord(INPUT_BUFFER[0]) print ord(INPUT_BUFFER[1]) print 'counter ticks:' print ord(INPUT_BUFFER[8]) print ord(INPUT_BUFFER[9]) if 0: print 'read', val except usb.USBNoDataAvailableError: if 0: sys.stdout.write('?') sys.stdout.flush() val = None if return_input: return INPUT_BUFFER
def _read_input(self, timeout=1000): buf = self.input_buffer try: val = usb.bulk_read(self.libusb_handle, USB_BULKIN_EP_ADDRESS, buf, timeout) #print 'read', [ord(b) for b in buf] data = [x for x in buf] except usb.USBNoDataAvailableError: data = None return data
def _read_buf(self): if not self.real_device: return None buf = ctypes.create_string_buffer(16) timeout = 1000 with self._lock: try: val = usb.bulk_read(self._libusb_handle, 0x82, buf, timeout) except usb.USBNoDataAvailableError: return None return buf
def _read_buf(self): if not self.real_device: return None buf = ctypes.create_string_buffer(16) timeout = 1000 epnum = (ENDPOINT_DIR_IN|CAMTRIG_EPNUM) with self._lock: try: val = usb.bulk_read(self._libusb_handle, epnum, buf, timeout) except usb.USBNoDataAvailableError: return None return buf
def get_analog_input_buffer_rawLE(self): """return raw sample values (little-endian)""" EP_LEN = 256 INPUT_BUFFER = ctypes.create_string_buffer(EP_LEN) bufs = [] got_bytes = False while 1: # keep pumping until no more data try: timeout = 50 # msec n_bytes = usb.bulk_read(self.libusb_handle, (ENDPOINT_DIR_IN | ANALOG_EPNUM), INPUT_BUFFER, timeout) except usb.USBNoDataAvailableError: break #self._savedebug(INPUT_BUFFER.raw[:n_bytes]) n_elements = n_bytes // 2 if 0: if not got_bytes: print '*' * 80 got_bytes = True print '%d bytes (first 12): %s' % (n_bytes, ' '.join([ str(ord(INPUT_BUFFER[i])) for i in range(min(12, n_bytes)) ])) print '%d bytes (first 12): %s' % (n_bytes, ' '.join([ hex(ord(INPUT_BUFFER[i])) for i in range(min(12, n_bytes)) ])) buf = np.fromstring(INPUT_BUFFER.raw, dtype='<u2') # unsigned 2 byte little endian buf = buf[:n_elements] #self._savedebug(buf) ## print 'repr(buf[:6])',repr(buf[:6]) ## print 'repr(buf_bytes[:12])',' '.join(map(str,buf.view(np.uint8)[:12])) ## print bufs.append(buf) if n_bytes < EP_LEN: break # don't bother waiting for data to dribble in if len(bufs): outbuf = np.hstack(bufs) else: outbuf = np.array([], dtype='<u2') # unsigned 2 byte little endian return outbuf
def __read_input(self, timeout=1000): """ Read input data from the usb device. Arguments: None Keywords: timeout = the timeout in ms Return: the raw data read from the usb device. """ buf = self.input_buffer try: val = usb.bulk_read(self.libusb_handle, USB_BULKIN_EP_ADDRESS, buf, timeout) #print 'read', [ord(b) for b in buf] data = [x for x in buf] except usb.USBNoDataAvailableError: data = None return data
def get_analog_input_buffer_rawLE(self): """return raw sample values (little-endian)""" EP_LEN = 256 INPUT_BUFFER = ctypes.create_string_buffer(EP_LEN) bufs = [] got_bytes = False while 1: # keep pumping until no more data try: timeout = 50 # msec n_bytes = usb.bulk_read(self.libusb_handle, (ENDPOINT_DIR_IN|ANALOG_EPNUM), INPUT_BUFFER, timeout) except usb.USBNoDataAvailableError: break #self._savedebug(INPUT_BUFFER.raw[:n_bytes]) n_elements = n_bytes//2 if 0: if not got_bytes: print '*'*80 got_bytes = True print '%d bytes (first 12): %s'%(n_bytes, ' '.join([str(ord(INPUT_BUFFER[i])) for i in range(min(12,n_bytes))])) print '%d bytes (first 12): %s'%(n_bytes, ' '.join([hex(ord(INPUT_BUFFER[i])) for i in range(min(12,n_bytes))])) buf = np.fromstring(INPUT_BUFFER.raw,dtype='<u2') # unsigned 2 byte little endian buf = buf[:n_elements] #self._savedebug(buf) ## print 'repr(buf[:6])',repr(buf[:6]) ## print 'repr(buf_bytes[:12])',' '.join(map(str,buf.view(np.uint8)[:12])) ## print bufs.append(buf) if n_bytes < EP_LEN: break # don't bother waiting for data to dribble in if len(bufs): outbuf = np.hstack(bufs) else: outbuf = np.array([],dtype='<u2') # unsigned 2 byte little endian return outbuf
def __send_and_receive(self,in_timeout=200,out_timeout=9999): """ Send bulkout and and receive bulkin as a response. Arguments: None Keywords: in_timeout = bulkin timeout in ms out_timeout = bilkin timeout in ms Return: the data returned by the usb device. """ done = False while not done: val = usb.bulk_write( self.libusb_handle, self.usb_bulkout_ep_address, self.output_buffer, out_timeout ) if val < 0 : raise IOError, "error sending usb output" try: numbytes = usb.bulk_read( self.libusb_handle, self.usb_bulkin_ep_address, self.input_buffer, in_timeout ) debug_print('usb SR bytes read: %d'%(numbytes,), comma=False) done = True except usb.USBNoDataAvailableError: debug_print('usb SR: fail', comma=False) sys.stdout.flush() return
interface_nr = 0 if hasattr(usb,'get_driver_np'): # non-portable libusb extension name = usb.get_driver_np(libusb_handle,interface_nr) if name != '': debug("attached to kernel driver '%s', detaching."%name ) usb.detach_kernel_driver_np(libusb_handle,interface_nr) if dev.descriptor.bNumConfigurations > 1: debug("WARNING: more than one configuration, choosing first") debug('setting configuration') debug('dev.config[0]',dev.config[0]) config = dev.config[0] debug('config.bConfigurationValue',config.bConfigurationValue) usb.set_configuration(libusb_handle, config.bConfigurationValue) debug('claiming interface') debug('config.bNumInterfaces',config.bNumInterfaces) usb.claim_interface(libusb_handle, interface_nr) INPUT_BUFFER = ctypes.create_string_buffer(16) while 1: try: # do your device-specific stuff here val = usb.bulk_read(libusb_handle, 0x82, INPUT_BUFFER, 1000) except usb.USBNoDataAvailableError: pass