Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python检测连接到USB的交换机的状态_Python_Usb - Fatal编程技术网

使用python检测连接到USB的交换机的状态

使用python检测连接到USB的交换机的状态,python,usb,Python,Usb,我想问一下,是否有办法检测通过usb连接的交换机的状态。该开关有两种状态,接通和断开。可能在Windows上使用Python 或者,我能够实现一个脚本,它将把开关看作是一个键盘扩展。 提前谢谢你 编辑 #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

我想问一下,是否有办法检测通过usb连接的交换机的状态。该开关有两种状态,接通和断开。可能在Windows上使用Python

<>或者,我能够实现一个脚本,它将把开关看作是一个键盘扩展。 提前谢谢你

编辑

#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

你看过PyUSB了吗?有关PyUSB使用的教程,请参见。如果您还想实现更接近硬件的东西,那么该库的源代码将对您有所帮助

看起来像是另一个用C编写的带有Python绑定的不错的库

编辑
作为对您尝试的代码的响应,看起来您正在使用传统的PyUSB接口。如果您打印(dev),您会发现它显示为
,或者您会发现您使用的是旧版本的库(
)。确保您拥有1.0,并确保您正在使用更新的方法访问设备。例如,
usb.core.find()
将返回一个确实具有
设置配置()的设备。请再次尝试阅读本教程。

您看过PyUSB吗?有关PyUSB使用的教程,请参见。如果您还想实现更接近硬件的东西,那么该库的源代码将对您有所帮助

看起来像是另一个用C编写的带有Python绑定的不错的库

编辑
作为对您尝试的代码的响应,看起来您正在使用传统的PyUSB接口。如果您打印(dev),您会发现它显示为
,或者您会发现您使用的是旧版本的库(
)。确保您拥有1.0,并确保您正在使用更新的方法访问设备。例如,
usb.core.find()
将返回一个确实具有
设置配置()的设备。再次尝试阅读教程。

好的,我现在有了解决方案,我会发布它,但我有一个问题…当我运行代码时,有时它会说设备正忙,它会生成错误,当它工作时。。。它将等待一个中断,但是如果你移动鼠标,它将在屏幕上保持静止,但它将产生一个中断。点击一个按钮也可以这样说,它会产生一个中断,但是鼠标会保持静止,要再次使用它,你需要将它从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鼠标是不合适的。

好的,我现在有了解决方案,我会发布它,但我有一个问题…当我运行代码时,有时它会说设备正忙,它会生成一个错误,当它工作时。。。它将等待一个中断,但是如果你移动鼠标,它将在屏幕上保持静止,但它将产生一个中断。点击一个按钮也可以这样说,它会产生一个中断,但是鼠标会保持静止,要再次使用它,你需要将它从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鼠标是不合适的。

好的,我现在正在尝试pyusb,我可以看到它的名称,连接设备的idVendor和idProduct…但是,我在尝试总线中的总线时出错:devices=bus.devices for dev in devices:try:_name=usb.util.get_string(dev.dev,19,1),除了:continue dev.set_configuration()
#这里我得到的“Device”对象没有属性“set\u configuration”。代码表明设备对象确实有set\u配置方法。需要完整的代码和完整的回溯。您可以编辑原始问题并添加信息。好的,我已经编辑了原始问题并添加了sourceright…我已经完成了您所说的,只是…在使用旧版本之前,我确实使用了新版本,但我遇到了一些错误…现在已修复,好了…它可以工作…例如…我已将set_configuration()设置为鼠标(USB一)它停止工作了:D好的。我想我们正在取得进展…现在…开关是单点击的,我想它不会有驱动程序…那么…我如何检测它何时被按下…?有什么想法吗…?我已经搜索了很多,但是…暂时没有什么。你需要首先确定作为你的开关的实际设备,然后选择端点重新启动我想我可能会读一些“跟我说话亲爱的”中建议的书该教程中关于传输风格的部分,并从那里开始。确定正确的端点似乎是一个重要的步骤。我恐怕这可能是我能够给出的更多方向。好的,我现在正在尝试pyusb,我能够看到连接设备的名称、idVendor和idProduct…但是我在尝试时出错
对于总线中的总线:devices=bus.devices对于设备中的开发人员:try:_name=usb.util.get_字符串(dev.dev,19,1),除了:continue dev.set_configuration()
#这里我得到的“Device”对象没有属性“set\u configuration”。代码表明设备对象确实有set\u配置方法。需要完整的代码和完整的回溯。您可以编辑原始问题并添加该信息。好的,我已经编辑了原始问题并添加了sourceright…我已经完成了你所说的,只是…在使用旧版本之前,我确实使用了新版本,但我得到了一些错误…现在已经修复了,好吗。