Esempio n. 1
0
 def _close(self):
     dev = self._dev
     release_interface(dev, self._intNum)
     dispose_resources(dev)
     self._dev = None
     # release 'Endpoints' objects for prevent undeleted 'Device' resource
     self._epOut = self._epIn = None
Esempio n. 2
0
    def __del__(self):
        """Cleanup a PtpUsbTransport structure."""

        try:
            release_interface(self.__device, self.__usb_interface)
            del self.__usb_handle
        except:
            pass
Esempio n. 3
0
def cleanup():
    print "Cleaning up"
    stop_warning()
    interface = 0
    # release the device
    util.release_interface(device_handle, interface)
    # reattach the device to the OS kernel
    device_handle.attach_kernel_driver(interface)
Esempio n. 4
0
 def releaseInterface(self):
     r"""Release an interface previously claimed with claimInterface."""
     util.release_interface(self.dev, self.__claimed_interface)
     self.__claimed_interface = -1
Esempio n. 5
0
    def barcodeReaderThreat(self):
        # import required libraries for barcode scanner
        if self.OnRaspberry:
            from usb import core as usb_core
            from usb import util as usb_util
        else:
            from Functions.GPIOEmulator.ShotmachineIOEmulator import usb_core_emu
            usb_core = usb_core_emu()

        # make usb connection to scanner
        #if self.EnableBarcodeScanner:
        self.device = usb_core.find(idVendor=self.barcode_vencor_id, idProduct=self.barcode_product_id)
        self.usbEndpointEmu = namedtuple("usbEndpointEmu", "bEndpointAddress wMaxPacketSize")

        try:
            while self.run:
                # on raspberry, find actual scanner
                if self.OnRaspberry:
                    if self.device is None:
                        # No scanner found
                        self.logger.error("No barcode scanner found, is it connected and turned on?")
                        connected = False
                        break
                    else:
                        # Found scanner, give it some time to start
                        connected = True
                        time.sleep(5)
                    if connected:
                        # claim the device and it's interface
                        configuration = self.device.get_active_configuration()
                        if self.device.is_kernel_driver_active(0):
                            # fisrt detach scanner from os before it can be claimed
                            self.device.detach_kernel_driver(0)
                        endpoint = self.device[0][(1, 0)][0]
                        self.device.set_configuration()
                        self.logger.info("Barcode reader ready, start scanning")
                else:
                    # Barcode scanner in emulator mode
                    self.logger.info("Barcode scanner in emulation mode")
                    connected = True
                    endpoint = self.usbEndpointEmu(bEndpointAddress = None, wMaxPacketSize=None)

                while self.run and connected and self.OnRaspberry:
                    # check usb connection for messages and convert to barcode
                    try:
                        data = self.device.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize)
                        read_string = ''.join(chr(e) for e in data)
                        try:
                            read_number = int(read_string)
                        except:
                            read_number = None
                        if read_number != None:
                            self.logger.info("Barcode scanned: " + str(read_number))
                            self.ToMainQueue.put("Barcode:" + str(read_number))

                    except usb_core.USBError as e:
                        # error in checking messsages from scanner
                        data = None
                        if e.errno == 110: 
                            # timeout, just keep checking
                            continue
                        if e.errno == 19:
                            # Connection lost, try to reconnect 
                            self.logger.warning("Connection to barode scanner lost, closed connection if software")
                            connected = False
                            break
                            
                while self.run and connected and not self.OnRaspberry:
                    # Emulator barcode scanner
                    try:
                        data = self.device.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize)
                        read_string = ''.join(chr(e) for e in data)
                        try:
                            read_number = int(read_string)
                        except:
                            read_number = None
                        if read_number != None:
                            self.logger.info("Barcode scanned: " + str(read_number))
                            self.ToMainQueue.put("Barcode:" + str(read_number))
                    except Exception as e:
                        if str(e) == 'Timeout':
                            continue
                        else:
                            self.logger.error("Warning in barcode reader: " + e)
                            raise
        finally:
            # close connection to barcode scanner on closing
            if connected and self.OnRaspberry:
                usb_util.release_interface(self.device, 0)
            self.logger.info("Closed barcode scanner reader")
Esempio n. 6
0
 def releaseInterface(self):
     r"""Release an interface previously claimed with claimInterface."""
     util.release_interface(self.dev, self.__claimed_interface)
     self.__claimed_interface = -1
Esempio n. 7
0
def closePort(ifc):
    # Release interface
    util.release_interface(dev, ifc)
Esempio n. 8
0
# So, to workaround the problem I'm trying to save and restore the TLS state
# between the prototype invocation. This works fine with one exception. As soon as
# you close the last file descriptor associated with a USB device, the kernel automatically
# resets the device, effectively killing the established TLS state.
#
# This script helps to work around this last problem. It keeps an open descriptor which
# prevents kernel from resetting the device configuration. It does not interfere with
# the main process and does not hold the claim on the inface. It just sits there
# doing nothing until you decide to quit.
#
# The same can be achived by running something like "read 4</dev/bus/usb/001/011" from
# a command line, but in this case you need to figure out what is the current bus/device
# number yourself.

import usb.core
from usb.util import claim_interface, release_interface
from time import sleep

dev = usb.core.find(idVendor=0x138a, idProduct=0x0097)

# make sure we at least opened device descriptor
claim_interface(dev, 0)

sleep(0.2)

# release the iface, but keep the device open
release_interface(dev, 0)

# sit here, until the user press enter
raw_input()