我尝试使用下面的代码通过 WebUSB API 使用 POS 打印机,但失败了.claimInterface()
错误为DOMException: Unable to claim interface
。所有测试均在 Linux 上的 Chrome 80.0.3987.116 上完成。我该如何调试这个?
(更新)我发现这个演示但它会导致同样的错误。
async function start () {
const device = await navigator.usb.requestDevice({
filters: [{
vendorId: 0x04b8,
productId: 0x0202
}]
});
await device.open();
await device.selectConfiguration(device.configurations[0].configurationValue);
await device.claimInterface(device.configurations[0].interfaces[0].interfaceNumber);
}
当然,我在本地主机服务器上运行它(这是“安全上下文”)并调用start()
具有用户手势功能。
我还确认没有其他进程正在使用该设备。
% lsof /dev/bus/usb/001/011
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
chrome 4156 ikr7 393u CHR 189,10 0t0 331233 /dev/bus/usb/001/011
下面是输出lsusb -vs 001:011
.
Bus 001 Device 011: ID 04b8:0202 Seiko Epson Corp. Receipt Printer M129C/TM-T70
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 1.10
bDeviceClass 0
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 8
idVendor 0x04b8 Seiko Epson Corp.
idProduct 0x0202 Receipt Printer M129C/TM-T70
bcdDevice 2.00
iManufacturer 1 EPSON
iProduct 2 EPSON UB-U01III
iSerial 3 20110125210031250M02C
bNumConfigurations 1
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 0x0020
bNumInterfaces 1
bConfigurationValue 1
iConfiguration 0
bmAttributes 0xc0
Self Powered
MaxPower 100mA
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 2
bInterfaceClass 255 Vendor Specific Class
bInterfaceSubClass 255 Vendor Specific Subclass
bInterfaceProtocol 2
iInterface 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x01 EP 1 OUT
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0008 1x 8 bytes
bInterval 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x82 EP 2 IN
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0040 1x 64 bytes
bInterval 0
Device Status: 0x0001
Self Powered
问题是usblp
Linux 内核自动加载的驱动程序。将其加入黑名单即可解决问题。这是我的/etc/modprobe.d/blacklistusblp.conf
file.
blacklist usblp
注意列入黑名单usblp
禁用所有使用的 USB 打印机usblp
驱动程序,但幸运的是,包括我在内的大多数人都使用 CUPS 作为打印机驱动程序。
(更新)发现最好编写一个自动分离内核默认驱动程序的 udev 规则,而不是列入黑名单。这是我的/etc/udev/rules.d/99-escpos.rules
.
SUBSYSTEM=="usb", ATTRS{idVendor}=="04b8", ATTRS{idProduct}=="0202", MODE="0664", GROUP="wheel", RUN+="/bin/sh -c 'echo -n $id:1.0 > /sys/bus/usb/drivers/usblp/unbind && echo -n $id:1.0 > /sys/bus/usb/drivers/usbfs/unbind'"
此规则也卸载usbfs
偶尔会被内核加载usblp
是分离的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)