示例#1
0
    def find(cls, path):
        if not path.startswith("usb"):
            return

        log.debug("using libusb-{0}.{1}.{2}".format(*libusb.getVersion()[0:3]))

        usb_or_none = re.compile(r'^(usb|)$')
        usb_vid_pid = re.compile(r'^usb(:[0-9a-fA-F]{4})(:[0-9a-fA-F]{4})?$')
        usb_bus_dev = re.compile(r'^usb(:[0-9]{1,3})(:[0-9]{1,3})?$')
        match = None

        for regex in (usb_vid_pid, usb_bus_dev, usb_or_none):
            m = regex.match(path)
            if m is not None:
                log.debug("path matches {0!r}".format(regex.pattern))
                if regex is usb_vid_pid:
                    match = [int(s.strip(':'), 16) for s in m.groups() if s]
                    match = dict(zip(['vid', 'pid'], match))
                if regex is usb_bus_dev:
                    match = [int(s.strip(':'), 10) for s in m.groups() if s]
                    match = dict(zip(['bus', 'adr'], match))
                if regex is usb_or_none:
                    match = dict()
                break
        else:
            return None

        context = libusb.USBContext()
        try:
            devices = context.getDeviceList(skip_on_error=True)
            vid, pid = match.get('vid'), match.get('pid')
            bus, dev = match.get('bus'), match.get('adr')
            if vid is not None:
                devices = [d for d in devices if d.getVendorID() == vid]
            if pid is not None:
                devices = [d for d in devices if d.getProductID() == pid]
            if bus is not None:
                devices = [d for d in devices if d.getBusNumber() == bus]
            if dev is not None:
                devices = [d for d in devices if d.getDeviceAddress() == dev]
            return [(d.getVendorID(), d.getProductID(), d.getBusNumber(),
                     d.getDeviceAddress()) for d in devices]
        finally:
            context.exit()
示例#2
0
    def find(cls, path):
        if not path.startswith("usb"):
            return

        log.debug("using libusb-{0}.{1}.{2}".format(*libusb.getVersion()[0:3]))
        
        usb_or_none = re.compile(r'^(usb|)$')
        usb_vid_pid = re.compile(r'^usb(:[0-9a-fA-F]{4})(:[0-9a-fA-F]{4})?$')
        usb_bus_dev = re.compile(r'^usb(:[0-9]{1,3})(:[0-9]{1,3})?$')
        match = None

        for regex in (usb_vid_pid, usb_bus_dev, usb_or_none):
            m = regex.match(path)
            if m is not None:
                log.debug("path matches {0!r}".format(regex.pattern))
                if regex is usb_vid_pid:
                    match = [int(s.strip(':'), 16) for s in m.groups() if s]
                    match = dict(zip(['vid', 'pid'], match))
                if regex is usb_bus_dev:
                    match = [int(s.strip(':'), 10) for s in m.groups() if s]
                    match = dict(zip(['bus', 'adr'], match))
                if regex is usb_or_none:
                    match = dict()
                break
        else:
            return None

        context = libusb.USBContext()
        try:
            devices = context.getDeviceList(skip_on_error=True)
            vid, pid = match.get('vid'), match.get('pid')
            bus, dev = match.get('bus'), match.get('adr')
            if vid is not None:
                devices = [d for d in devices if d.getVendorID() == vid]
            if pid is not None:
                devices = [d for d in devices if d.getProductID() == pid]
            if bus is not None:
                devices = [d for d in devices if d.getBusNumber() == bus]
            if dev is not None:
                devices = [d for d in devices if d.getDeviceAddress() == dev]
            return [(d.getVendorID(), d.getProductID(), d.getBusNumber(),
                     d.getDeviceAddress()) for d in devices]
        finally:
            context.exit()
示例#3
0
 def version_info(cls) -> Mapping[str, Optional[str]]:
     ret = {}
     # add libusb
     try:
         import usb1
     except Exception as e:
         ret["libusb.version"] = None
     else:
         ret["libusb.version"] = ".".join(map(str, usb1.getVersion()[:4]))
         try:
             ret["libusb.path"] = usb1.libusb1.libusb._name
         except AttributeError:
             ret["libusb.path"] = None
     # add hidapi
     from importlib.metadata import version
     try:
         ret["hidapi.version"] = version("hidapi")
     except ImportError:
         ret["hidapi.version"] = None
     return ret
示例#4
0
 def testGetVersion():
     """
     Just testing getVersion doesn't raise...
     """
     usb1.getVersion()
示例#5
0
import usb1
import time
import sys
import struct 

VENDOR_ID = 0x04d8
PRODUCT_ID = 0x0052

context = usb1.USBContext()
handle = context.openByVendorIDAndProductID(
    VENDOR_ID, PRODUCT_ID,
    skip_on_error=True,
)
if handle is None:
    print "hui"
    # Device not present, or user is not allowed to access device.
handle.claimInterface(0)
print usb1.getVersion()

BYTES = 10000000
BUFFER_SIZE = (BYTES / 64) * 64

f = open("measurements.msr", 'w')
before = time.time()
data = handle.bulkRead(1, BUFFER_SIZE, 50000)
f.write(data)   
print 'time for 1MByte is: ', time.time() - before
print 'Speed is: ', (BUFFER_SIZE / (time.time() - before))/1000, " [kByte/sec]"
f.close()

示例#6
0
 def testGetVersion():
     """
     Just testing getVersion doesn't raise...
     """
     usb1.getVersion()
示例#7
0
        context.handleEvents()
        #device.read(0x86,rdata)
        #rdata = handle.bulkRead(0x86,size*2)
        #cnt.value = cnt.value + 1
        #print(len(rdata),'received')
        #print('%x' % (rdata[0] & 0x03))
        #print('%x' % rdata[0])
        #print(rdata[0])
        #print(rdata)


if __name__ == '__main__':
    #device.read(0x86,rdata)
    #print(len(rdata),'received')
    #print(rdata)
    print(usb1.getVersion())
    size = 2048
    cnt = multiprocessing.Value('i', 0)
    t_read_data = multiprocessing.Process(target=read_data_thread,
                                          args=(
                                              cnt,
                                              size,
                                          ))
    t_read_data.daemon = True
    t_read_data.start()

    count = 0
    while 1:
        time.sleep(1)
        #print((cnt.value - count)*size/1000)
        #count = cnt.value
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the License along with this library.
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.

import importlib
import logging
import sys
import time
import atexit

from .transport import Transport, ConnectionError

import usb1

_libusb_version = usb1.getVersion()
_libusb_version = (_libusb_version.major, _libusb_version.minor, _libusb_version.micro)

class FakeRead(object):
    # Let's pretend we have a file-like interface
    def __init__(self, func):
        self.func = func

    def read(self, size):
        return self.func(size)

DEVICE_IDS = [
    (0x2B24, 0x0002),  # KeepKey
]

class WebUsbTransport(Transport):