我想询问是否有办法检测通过usb连接的交换机的状态。开关有2种状态,开启和关闭。可能是Python,在Windows上。
或者,我能否实现一个将开关视为键盘扩展的脚本。
提前谢谢!
修改
#import usb.core
#import usb.util
import usb
# find our device
#dev = usb.core.find(find_all=True)
busses = usb.busses()
# was it found?
#if dev is None:
# raise ValueError('Device not found')
for bus in busses:
devices = bus.devices
for dev in devices:
try:
_name = usb.util.get_string(dev.dev, 19, 1)
except:
continue
#dev.set_configuration()
#cfg = dev.get_active_configuration()
#interface_number = cfg[(0,0)].bInterfaceNumber
#5alternate_settting = usb.control.get_interface(interface_number)
print "Device name:",_name
print "Device:", dev.filename
print " idVendor:",hex(dev.idVendor)
print " idProduct:",hex(dev.idProduct)
for config in dev.configurations:
print " Configuration:", config.value
print " Total length:", config.totalLength
print " selfPowered:", config.selfPowered
print " remoteWakeup:", config.remoteWakeup
print " maxPower:", config.maxPower
print
答案 0 :(得分:0)
你看过PyUSB了吗?有关PyUSB使用的教程,请参阅http://pyusb.sourceforge.net/docs/1.0/tutorial.html。如果您想要实现更接近硬件的东西,那么该库的源代码将对您有所帮助。
http://libhid.alioth.debian.org/看起来像是用C语言编写的另一个体面的库,带有Python绑定。
为了回应您尝试过的代码,您似乎正在使用旧版PyUSB界面。如果您打印(开发),您会发现它显示为<usb.legacy.Device object at 0x1dac210>
,或者您会发现您使用的是旧版本的库(<usb.Device object at 0x13e6810>
)。确保您拥有1.0并确保使用较新的方法来访问设备。它将类似于<usb.core.Device object at 0x1e0c3d0>
例如,usb.core.find()
将为您提供确实具有set_configuration()
的设备。尝试再次学习本教程。
答案 1 :(得分:0)
好的,我现在有了解决方案,我会发布它,但我有一个问题......当我运行代码时,有时会说设备很忙,它会产生错误,当它工作时..它会等待一个中断,但是如果你移动鼠标,它会在屏幕上保持静止,但会产生一个中断。单击按钮也可以这样说,它会产生一个中断,但鼠标会保持静止并再次使用它,你需要从usb中取出它并再次放入。
import usb.core
import usb.util
#import usb
# find our device
dev = usb.core.find(find_all=True)
#the second device it finds is my mouse
device = dev[2]
#print device
#physical device call: 5
#usb HID call: 3
_name = usb.util.get_string(device, 19, 1)
print _name
#we take the first configuration of the device
device.set_configuration()
print "Config set..."
#we access the configuration we've found
cfg = device.get_active_configuration()
#we access the intherface with number 0 and alternate setting with number 0
interface_number = cfg[(0,0)].bInterfaceNumber
alternate_setting = usb.control.get_interface(device,interface_number)
#we find the alterng settings for interface_number and altering_setting
intf = usb.util.find_descriptor(cfg, bInterfaceNumber = interface_number,\
bAlternateSetting = alternate_setting)
#Finds the first IN endpoint
ep = usb.util.find_descriptor(
intf,
# match the first IN endpoint
custom_match = \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_IN
)
#inorder for you to detect a state from the device, it has to be(for mouse, moved,
#clicked)
#otherwise it generates error
#make use of the error, if the mouse isn't pushed, do nothing and wait, if pushed...
#print the state
#and exit from the loop
print "Waiting for signal..."
#device.detach_kernel_driver(0)
#click of the scroll button has array('B', [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
#0, 0]) signal
while True:
try:
print ep.read(16)
print "Received!"
break
except:
continue
#assert ep is not 0
#_name=device.ctrl_transfer(bmRequestType=33, bRequest=11, wValue=0x0300)
#print _name
所以我猜它首先删除鼠标的驱动程序,然后它与设备对话,然后我点击按钮产生中断,然后......我怎么说,再次使用你的驱动程序和结束程序..因为每次重新输入usb鼠标是不方便的。