从pywinusb / python初始化USB设备

时间:2013-04-03 21:05:11

标签: python-2.7 windows-xp pywinusb

我的问题类似于Will所建议的“在Python中简单地读取/写入USB HID设备?”,但是遵循它我没有得到任何结果。问题:我正在尝试处理Oregon WMRS200 Meteo站,它实现了一个usb连接作为HID设备。我开始调整/简化下面列出的'raw data.py'提供的示例

from time import sleep
from msvcrt import kbhit
import pywinusb.hid as hid

def sample_handler(data):
    print("Raw data: {0}".format(data))

def raw_test():
    device = (hid.HidDeviceFilter (vendor_id =0x0fde , product_id = 0xca01). \
                    get_devices())[0]
    device.open()   
    #set custom raw data handler
    device.set_raw_data_handler(sample_handler)
    print("\nWaiting for data...\nPress any (system keyboard) key to stop...")
    while not kbhit() and device.is_plugged():
        #just keep the device opened to receive events
        sleep(0.5)

    device.close()
    return

if __name__ == '__main__':
    raw_test()

数据的传输没有开始。我在网上发现系统必须初始化一次(在重置或pw失败后)发送缓冲区 0x20 0x00 0x08 0x01 0x00 0x00 0x00 0x00 问题是我不知道如何发送它。做一些事情,比如发送报告(如上面引用的例子),得不到任何结果。

如果在重置之后我开始和停止一旦原始的俄勒冈一切开始工作,即通道以某种方式初始化。在通信通道上安装嗅探器我想我看到字节串转到设备,但是,我怎么能发送它? 这是原始sw的嗅探器的输出。我想在URB 9-10中可以看到字节模式进入设备。

[229 ms]  >>>  URB 1 going down  >>> 
-- URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE:
  TransferBufferLength = 00000012
  DescriptorType       = 00000001 (USB_DEVICE_DESCRIPTOR_TYPE)

[234 ms]  <<<  URB 1 coming back  <<< 
-- URB_FUNCTION_CONTROL_TRANSFER:
  TransferFlags        = 0000000b (USBD_TRANSFER_DIRECTION_IN, USBD_SHORT_TRANSFER_OK)
  TransferBufferLength = 00000012
    00000000: 12 01 10 01 00 00 00 08 de 0f 01 ca 02 03 00 01
    00000010: 00 01
  SetupPacket          =
    00000000: 80 06 00 01 00 00 12 00

[234 ms]  >>>  URB 2 going down  >>> 
-- URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE:
  TransferBufferLength = 00000009
  DescriptorType       = 00000002 (USB_CONFIGURATION_DESCRIPTOR_TYPE)

[239 ms]  <<<  URB 2 coming back  <<< 
-- URB_FUNCTION_CONTROL_TRANSFER:
  TransferFlags        = 0000000b (USBD_TRANSFER_DIRECTION_IN, USBD_SHORT_TRANSFER_OK)
  TransferBufferLength = 00000009
    00000000: 09 02 22 00 01 01 00 80 32
  SetupPacket          =
    00000000: 80 06 00 02 00 00 09 00

[239 ms]  >>>  URB 3 going down  >>> 
-- URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE:
  TransferBufferLength = 00000022
  DescriptorType       = 00000002 (USB_CONFIGURATION_DESCRIPTOR_TYPE)

[247 ms]  <<<  URB 3 coming back  <<< 
-- URB_FUNCTION_CONTROL_TRANSFER:
  TransferFlags        = 0000000b (USBD_TRANSFER_DIRECTION_IN, USBD_SHORT_TRANSFER_OK)
  TransferBufferLength = 00000022
    00000000: 09 02 22 00 01 01 00 80 32 09 04 00 00 01 03 00
    00000010: 00 00 09 21 10 01 00 01 22 22 00 07 05 81 03 08
    00000020: 00 01
  SetupPacket          =
    00000000: 80 06 00 02 00 00 22 00

[247 ms]  >>>  URB 4 going down  >>> 
-- URB_FUNCTION_SELECT_CONFIGURATION:
  ConfigurationDescriptor = 0x899fb730 (configure)
  ConfigurationDescriptor : bLength             = 9
  ConfigurationDescriptor : bDescriptorType     = 0x00000002
  ConfigurationDescriptor : wTotalLength        = 0x00000022
  ConfigurationDescriptor : bNumInterfaces      = 0x00000001
  ConfigurationDescriptor : bConfigurationValue = 0x00000001
  ConfigurationDescriptor : iConfiguration      = 0x00000000
  ConfigurationDescriptor : bmAttributes        = 0x00000080
  ConfigurationDescriptor : MaxPower            = 0x00000032
  ConfigurationHandle     = 0x00000000
  Interface[0]: Length            = 36
  Interface[0]: InterfaceNumber   = 0
  Interface[0]: AlternateSetting  = 0

[280 ms]  <<<  URB 4 coming back  <<< 
-- URB_FUNCTION_SELECT_CONFIGURATION:
  ConfigurationDescriptor = 0x899fb730 (configure)
  ConfigurationDescriptor : bLength             = 9
  ConfigurationDescriptor : bDescriptorType     = 0x00000002
  ConfigurationDescriptor : wTotalLength        = 0x00000022
  ConfigurationDescriptor : bNumInterfaces      = 0x00000001
  ConfigurationDescriptor : bConfigurationValue = 0x00000001
  ConfigurationDescriptor : iConfiguration      = 0x00000000
  ConfigurationDescriptor : bmAttributes        = 0x00000080
  ConfigurationDescriptor : MaxPower            = 0x00000032
  ConfigurationHandle     = 0x89a11ab8
  Interface[0]: Length            = 36
  Interface[0]: InterfaceNumber   = 0
  Interface[0]: AlternateSetting  = 0
  Interface[0]: Class             = 0x00000003
  Interface[0]: SubClass          = 0x00000000
  Interface[0]: Protocol          = 0x00000000
  Interface[0]: InterfaceHandle   = 0x8a2a18b0
  Interface[0]: NumberOfPipes     = 1
  Interface[0]: Pipes[0] : MaximumPacketSize = 0x00000008
  Interface[0]: Pipes[0] : EndpointAddress   = 0x00000081
  Interface[0]: Pipes[0] : Interval          = 0x00000001
  Interface[0]: Pipes[0] : PipeType          = 0x00000003 (UsbdPipeTypeInterrupt)
  Interface[0]: Pipes[0] : PipeHandle        = 0x8a2a18cc
  Interface[0]: Pipes[0] : MaxTransferSize   = 0x00001000
  Interface[0]: Pipes[0] : PipeFlags         = 0x00000000

[280 ms]  >>>  URB 5 going down  >>> 
-- URB_FUNCTION_CLASS_INTERFACE:
  TransferFlags          = 00000000 (USBD_TRANSFER_DIRECTION_OUT, USBD_SHORT_TRANSFER_OK)
  TransferBufferLength = 00000000
    no data supplied
  RequestTypeReservedBits = 00000022
  Request                 = 0000000a
  Value                   = 00000000
  Index                   = 00000000

[282 ms]  <<<  URB 5 coming back  <<< 
-- URB_FUNCTION_CONTROL_TRANSFER:
  TransferFlags        = 0000000a (USBD_TRANSFER_DIRECTION_OUT, USBD_SHORT_TRANSFER_OK)
  TransferBufferLength = 00000000
  SetupPacket          =
    00000000: 21 0a 00 00 00 00 00 00

[282 ms]  >>>  URB 6 going down  >>> 
-- URB_FUNCTION_GET_DESCRIPTOR_FROM_INTERFACE:
  TransferBufferLength = 00000062
  DescriptorType       = 00000022 (<illegal descriptor type!>)

[291 ms]  <<<  URB 6 coming back  <<< 
-- URB_FUNCTION_CONTROL_TRANSFER:
  PipeHandle           = 8a2a4198
  TransferFlags        = 0000000b (USBD_TRANSFER_DIRECTION_IN, USBD_SHORT_TRANSFER_OK)
  TransferBufferLength = 00000022
    00000000: 06 00 ff 09 01 a1 01 09 01 15 00 26 ff 00 75 08
    00000010: 95 08 81 00 09 02 15 00 26 ff 00 75 08 95 08 91
    00000020: 02 c0
  SetupPacket          =
    00000000: 81 06 00 22 00 00 62 00

[294 ms]  >>>  URB 7 going down  >>> 
-- URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER:
  TransferFlags        = 00000003 (USBD_TRANSFER_DIRECTION_IN, USBD_SHORT_TRANSFER_OK)
  TransferBufferLength = 00000008
[294 ms]  >>>  URB 8 going down  >>> 
-- URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER:
  TransferFlags        = 00000003 (USBD_TRANSFER_DIRECTION_IN, USBD_SHORT_TRANSFER_OK)
  TransferBufferLength = 00000008

[3330 ms]  <<<  URB 7 coming back  <<< 
-- URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER:
  TransferFlags        = 00000003 (USBD_TRANSFER_DIRECTION_IN, USBD_SHORT_TRANSFER_OK)
  TransferBufferLength = 00000008
    00000000: 01 ff 01 cc 01 87 01 00

[3330 ms]  >>>  URB 9 going down  >>> 
-- URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER:
  TransferFlags        = 00000003 (USBD_TRANSFER_DIRECTION_IN, USBD_SHORT_TRANSFER_OK)
  TransferBufferLength = 00000008
[3330 ms]  >>>  URB 10 going down  >>> 
-- URB_FUNCTION_CLASS_INTERFACE:
  TransferFlags          = 00000000
(USBD_TRANSFER_DIRECTION_OUT, ~USBD_SHORT_TRANSFER_OK)
  TransferBufferLength = 00000008
    00000000: 20 00 08 01 00 4d 80 00
  RequestTypeReservedBits = 00000022
  Request                 = 00000009
  Value                   = 00000200
  Index                   = 00000000

从URB 11 - - &gt;正常传输也从我的示例脚本开始。

我希望我已经足够明确了.....感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

对于发送RAW数据(没有HID描述符管理的报告),您可以使用“device.send_output_report()”,将任何列表作为参数传递(或者为了获得更好的性能,您可以传递ctypes.c_ubyte数组。