我尝试使用以下代码通过 WebUSB API 使用 POS 打印机,但在 .claimInterface()
处失败,并出现错误 DOMException: 无法声明接口(interface)
。所有测试均在 Linux 上的 Chrome 80.0.3987.116 上完成。我该如何调试这个?
(更新)我发现this demo但它会导致同样的错误。
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);
}
当然,我在本地主机服务器(即 "secure context" )上运行它并使用用户手势调用 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
最佳答案
问题出在 Linux 内核自动加载的 usblp
驱动程序上。将其加入黑名单即可解决问题。这是我的 /etc/modprobe.d/blacklistusblp.conf
文件。
blacklist usblp
请注意,将 usblp
列入黑名单会禁用所有使用 usblp
驱动程序的 USB 打印机,但幸运的是,包括我在内的大多数人都使用 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
分离时,内核偶尔会加载该文件。
关于javascript - WebUSB - "Unable to claim interface"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60790994/