initialize a USB device from pywinusb / python

0

my problem lokks like the one suggested by Will "Simple reading/writing from/to a USB HID device in Python?", but following it I did not get any result. Problem: I am trying to handle the Oregon WMRS200 Meteo station, that implements a usb connection as a HID device. I started adapting/simplifying the example provided 'raw data.py' as listed below

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()

the transfer of the data does not start. I find on the net that the system must be initialized once (after a reset or pw failure) sending a buffer 0x20 0x00 0x08 0x01 0x00 0x00 0x00 0x00 The problem is that I do not know how to send it. doing something like sending a report (as in the example quoted above) dows not get any result.

If after a reset I start and stop once the original oregon sw everything starts working, i.e. the channel is somehow initialized. Installing a sniffer on the communication channel I think I see the byte string going to the device, but still, how can I send it ?? Here is the output of the sniffer with the original sw. I suppose that the byte pattern may be seen in the URB 9-10 going to the device.

[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

From URB 11 - --> the normal transfer starts also with my example script.

I hope I have been sufficiently clear ..... Thanks for any help.

python-2.7
windows-xp
pywinusb
asked on Stack Overflow Apr 3, 2013 by user2202117

1 Answer

0

For sending RAW data (no HID descriptor managed reports), you can use the "device.send_output_report()", pass any list as a parameter (or for better performance, you can pass a ctypes.c_ubyte array.

answered on Stack Overflow Apr 22, 2013 by Rene

User contributions licensed under CC BY-SA 3.0