【发布时间】:2016-06-06 11:50:05
【问题描述】:
我正在尝试从条形码扫描仪中获取价值,这在 Ubuntu 上使用 pyusb。经过调查,我发现条码扫描器需要接收激活数据才能扫描条码。我找到了这个数据,修改了我的 rules.d 文件以检测我的设备,然后我运行这个代码通过 USB 发送数据:
import usb.core
import usb.util
# find our device
dev = usb.core.find(idVendor=0x05f9, idProduct=0x1203)
# was it found?
if dev is None:
raise ValueError('Device not found')
# Attach and detach the usb
if dev.is_kernel_driver_active(0):
dev.detach_kernel_driver(0)
# set the active configuration. With no arguments, the first
# configuration will be the active one
dev.set_configuration()
# get an endpoint instance
cfg = dev.get_active_configuration()
intf = cfg[(0,0)]
ep = usb.util.find_descriptor(
intf,
# match the first OUT endpoint
custom_match = \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_OUT)
assert ep is not None
# write the data
ep.write('\x0b')
我收到以下错误:
usb.core.USBError: [Errno 16] Resource busy
但通常以下代码必须使设备可用:
# Attach and detach the usb
if dev.is_kernel_driver_active(0):
dev.detach_kernel_driver(0)
我在另一台设备(打印机)上尝试了这段代码,它正在工作。
你对这个问题有什么想法吗?
【问题讨论】:
-
首先我不得不说我不熟悉PyUSB,只熟悉libusb。所以我不知道这是否适用于异常,但如果不是,我想知道 dev.detach_kernel_driver(0) 是否被执行,如果是,它会返回什么,因为它可能会返回错误。此外,Errno16 发生在哪一行也会很有趣。
标签: python usb reverse-engineering libusb pyusb