示例#1
0
def __pollDevice(latch):

  # wait for data from the device using the asynchronous API. See
  # http://libusb.sourceforge.net/api-1.0/
  pollobj = select.poll()
  pollfds = LibUSB.get_pollfds()
  LOGGER.debug("retreived poll file descriptors: %s" % pollfds)
  for (fd, events) in pollfds:
    pollobj.register(fd, events)

  while True:
    latch.open()
    try:
      timeoutInMillis = None
      t0 = datetime.datetime.now()
      nextTimeout = LibUSB.get_next_timeout()
      if nextTimeout is not None:
        (secs, usecs) = nextTimeout
        timeoutInMillis = secs * 1000.0 + usecs * 0.001
        if LOGGER.isEnabledFor(logging.DEBUG):
          LOGGER.debug("next timeout: %(timeoutInMillis)f" % locals())
      pollresult = pollobj.poll(timeoutInMillis) # milliseconds
      t1 = datetime.datetime.now()
      dt = t1 - t0
      elapsedTimeInMillis = dt.seconds * 1000.0 + dt.microseconds * 0.001
      if LOGGER.isEnabledFor(logging.DEBUG):
        LOGGER.debug("poll result: %(pollresult)s, timeout: %(timeoutInMillis)s, elapsed: %(elapsedTimeInMillis)f" % locals())
      if (len(pollresult) > 0) or (timeoutInMillis is None) or (elapsedTimeInMillis > timeoutInMillis):
        LibUSB.handle_events_timeout()
    except Exception,e:
      exc_type, exc_value, exc_traceback = sys.exc_info()
#        msg = traceback.format_exception_only(exc_type, exc_value);
      msg = traceback.format_exception(exc_type, exc_value, exc_traceback);
      LOGGER.error("\n".join(msg))