https://github.com/pbatard/libwdi/wiki/Zadig
https://zadig.akeo.ie/
给usb 设备 设置驱动
pyusb 访问 读写 usb 设备,需要 通过驱动,驱动可以 通过Zadig 指定
Bus Hound 6.01 capture on Windows Vista Service Pack 1 (x64). Complements of www.perisoft.net
Device - Device ID (followed by the endpoint for USB devices)
(9) USB 打印支持
(22) 智能卡筛选器驱动程序
(24) 没有连接打印机
Phase - Phase Type
OUT Data out transfer
Data - Hex dump of the data transferred
Descr - Description of the phase
Cmd... - Position in the captured data
Device Phase Data Description Cmd.Phase.Ofs(rep)
------ ----- ------------------------------------------------------------------------------------------------------ -------------------------------- ------------------
9.2 OUT 0d 1b 40 ..@ 1.1.0
9.2 OUT 20 20 20 20 20 20 d6 c7 cd a8 bb db c1 aa d3 cd b7 d1 cf b5 cd b3 bd bb d2 d7 c6 be d6 a4 0a ......................... 2.1.0
9.2 OUT 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d -------------------------- 3.1.0
2d 2d 2d 2d 2d 2d 2d 0a 0a -------.. 3.1.32
9.2 OUT 20 20 20 20 20 20 45 54 43 bf a8 ba c5 3a 20 31 32 33 34 35 36 37 38 39 39 30 0a 0a ETC....: 12345678990.. 4.1.0
9.2 OUT 20 20 20 20 20 20 b3 b5 c5 c6 3a 20 20 20 c2 b3 41 32 53 31 46 32 0a ....: ..A2S1F2. 5.1.0
9.2 OUT 20 20 20 20 20 20 b3 b5 d0 cd 3a 20 20 20 20 0a ....: . 6.1.0
9.2 OUT 20 20 20 20 20 20 bd f0 b6 ee 3a 20 20 20 20 31 d4 aa 0a 0a ....: 1.... 7.1.0
9.2 OUT 20 20 20 20 20 20 bd bb d2 d7 b1 e0 ba c5 3a 20 20 31 32 33 34 35 36 37 38 39 30 0a ........: 1234567890. 8.1.0
9.2 OUT 20 20 20 20 20 20 d3 cd d5 be b1 e0 ba c5 3a 20 20 31 31 31 31 31 31 31 0a ........: 1111111. 9.1.0
9.2 OUT 20 20 20 20 20 20 d3 cd d5 be c3 fb b3 c6 3a 20 20 0a ........: . 10.1.0
9.2 OUT 20 20 20 20 20 20 bd bb d2 d7 ca b1 bc e4 3a 20 20 32 30 31 39 2d 30 31 2d 30 31 0a 0a ........: 2019-01-01.. 11.1.0
9.2 OUT 0a 0a 0a 0a .... 12.1.0
9.2 OUT 1b 64 03 .d. 13.1.0
9.2 OUT 1b 6d .m 14.1.0
打印机 切纸(可以使用)
import usb.core
import usb.util
import time
""" Search device on USB tree and set it as escpos device """
device = usb.core.find(idVendor=0x8866, idProduct=0x0100)
if device is None:
print("Device not found or cable not plugged in.")
check_driver = None
try:
check_driver = device.is_kernel_driver_active(0)
print("11111\n")
except NotImplementedError:
print("22222\n")
pass
if check_driver:
try:
print("333333\n")
device.detach_kernel_driver(0)
print("444444\n")
except usb.core.USBError as e:
print("5555555\n", str(e))
if check_driver is not None:
print("Could not detatch kernel driver: {0}".format(str(e)))
try:
device.set_configuration()
device.reset()
except usb.core.USBError as e:
print("Could not set configuration: {0}".format(str(e)))
try:
data_lst = [0x1b, 0x6d, 0x03]
data = device.write(0x02, data_lst, 1000) #write(endpoint, data, timeout = None)
print(data)
except Exception as e:
print(e)
运行log:
22222
3
运行分析:
device.is_kernel_driver_active(0)
这个没有返回对应的信息
C:\Windows\system32\libusb-1.0.dll
DEVICE ID 8866:0100 on Bus 001 Address 011 =================
bLength : 0x12 (18 bytes)
bDescriptorType : 0x1 Device
bcdUSB : 0x110 USB 1.1
bDeviceClass : 0x0 Specified at interface
bDeviceSubClass : 0x0
bDeviceProtocol : 0x0
bMaxPacketSize0 : 0x40 (64 bytes)
idVendor : 0x8866
idProduct : 0x0100
bcdDevice : 0x100 Device 1.0
iManufacturer : 0x1 ZHU HAI SUNCSW Receipt Printer Co.,Ltd.
iProduct : 0x2 Gprinter GP-L80180
iSerialNumber : 0x3 0001B0000000
bNumConfigurations : 0x1
CONFIGURATION 1: 100 mA ==================================
bLength : 0x9 (9 bytes)
bDescriptorType : 0x2 Configuration
wTotalLength : 0x20 (32 bytes)
bNumInterfaces : 0x1
bConfigurationValue : 0x1
iConfiguration : 0x0
bmAttributes : 0x80 Bus Powered
bMaxPower : 0x32 (100 mA)
INTERFACE 0: Printer ===================================
bLength : 0x9 (9 bytes)
bDescriptorType : 0x4 Interface
bInterfaceNumber : 0x0
bAlternateSetting : 0x0
bNumEndpoints : 0x2
bInterfaceClass : 0x7 Printer
bInterfaceSubClass : 0x1
bInterfaceProtocol : 0x2
iInterface : 0x0
ENDPOINT 0x81: Bulk IN ===============================
bLength : 0x7 (7 bytes)
bDescriptorType : 0x5 Endpoint
bEndpointAddress : 0x81 IN
bmAttributes : 0x2 Bulk
wMaxPacketSize : 0x40 (64 bytes)
bInterval : 0xa
ENDPOINT 0x2: Bulk OUT ===============================
bLength : 0x7 (7 bytes)
bDescriptorType : 0x5 Endpoint
bEndpointAddress : 0x2 OUT
bmAttributes : 0x2 Bulk
wMaxPacketSize : 0x40 (64 bytes)
bInterval : 0xa
C:\Windows\system32\libusb-1.0.dll
可以运行代码:
import usb.core
import usb.util
import sys
class find_class(object):
def __init__(self, class_):
self._class = class_
def __call__(self, device):
# first, let's check the device
if device.bDeviceClass == self._class:
return True
# ok, transverse all devices to find an
# interface that matches our class
for cfg in device:
# find_descriptor: what's it?
intf = usb.util.find_descriptor(
cfg,
bInterfaceClass=self._class
)
if intf is not None:
return True
return False
printers = usb.core.find(find_all=1, custom_match=find_class(7),idVendor = 0x8866)
for item in printers:
print(item)
import ctypes
from ctypes import util, cdll
import os
import sys
if sys.platform == 'win32':
libname = ctypes.util.find_library('libusb-1.0')
else:
libname = ctypes.util.find_library('usb-1.0')
if libname is None:
raise OSError('USB library could not be found')
print (libname)
如果先出现系统无法识别的话
可以参考
https://stackoverflow.com/questions/33972145/pyusb-on-windows-8-1-no-backend-available-how-to-install-libusb
I downloaded latest libusb windows binaries from here: http://sourceforge.net/projects/libusb/files/libusb-1.0/libusb-1.0.20/libusb-1.0.20.7z/download then I copied libusb1.dll according to this: when you are on 64-bit Windows, MS64\dll\libusb-1.0.dll must be copied into C:\Windows\System32 and (for running 32-bit applications that use libusb) MS32\dll\libusb-1.0.dll must be copied into C:\Windows\SysWOW64. and then I copied libusb1.lib from binaries to lib dir in python directory and now pyusb works for me. hope it helps.