簡體   English   中英

使用python檢測連接到USB的開關的狀態

[英]Detect the state of a switch connected to USB with python

我想問一下是否有一種方法可以檢測通過USB連接的交換機的狀態。 開關有2個狀態,開和關。 在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用法的教程,請參見http://pyusb.sourceforge.net/docs/1.0/tutorial.html 如果您也想實現與硬件更接近的東西,該庫的源代碼將為您提供幫助。

http://libhid.alioth.debian.org/看起來像是另一個用C語言編寫且帶有Python綁定的不錯的庫。

編輯

響應您嘗試的代碼,您似乎正在使用舊版PyUSB接口。 如果您打印(dev),您會發現它像<usb.legacy.Device object at 0x1dac210>一樣顯示,或者您發現您正在使用舊版本的庫( <usb.Device object at 0x13e6810> )。 確保您擁有1.0,並確保您正在使用更新的方法來訪問設備。 類似於<usb.core.Device object at 0x1e0c3d0>例如, usb.core.find()將為您提供確實具有set_configuration() 再次嘗試完成本教程。

好的,我現在有解決方案,我將發布它,但是我有一個問題...當我運行代碼時,有時它說設備很忙,並且會生成錯誤,並且在工作時會...等待中斷,但是如果您移動鼠標,它將在屏幕上保持靜止,但會產生中斷。 可以說單擊按鈕也一樣,它會產生一個中斷,但是鼠標將保持靜止並再次使用,您需要將其從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鼠標。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM