WinUsbPy: A python wrapper over WinUsb v0.1

WinUsbPy is a python wrapper over WinUsb, it basically allows handling a usb device without the need of installing a kernel driver. Although You still have to install a generic driver. I needed raw access to that library using Python, I found this but it looks more oriented to HID devices. So, as I usually do, I coded my own solution.

Two Layers
WinUsbPy can used in two different ways:
– Using a 1:1 wrapper over WinUsb, which it means the programmer knows about C/C++, WinUsb and ctypes (of course you know python if you are reading this 🙂 )

– Using a high level api with more methods but pretty straightforward because it hides complexities mentioned above

Low Level Api:

#args: arguments of the C++ function called
def exec_function_winusb(self, function_name, *args):
def exec_function_kernel32(self, function_name, *args):
def exec_function_setupapi(self, function_name, *args):

Each function invokes functions from a different dll.
For example if we want to call SetupDiGetClassDevs

from winusbpy import *
from ctypes import *
from ctypes.wintypes import *
from winusbclasses import DIGCF_DEVICE_INTERFACE, DIGCF_PRESENT

api = WinUSBApi()
byte_array = c_byte * 8
guid = GUID(0xA5DCBF10L, 0x6530, 0x11D2, byte_array(0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED))
flags = DWORD(DIGCF_DEVICE_INTERFACE | DIGCF_PRESENT)

hdev_info = api.exec_function_setupapi("SetupDiGetClassDevs", byref(guid), None, None, flags)

High Level Api
Check the README for a complete description, but let’s compare how to send a control request using both apis

#Low level api
api.exec_function_winusb("WinUsb_ControlTransfer", handle_winusb, pkt1, byref(buff), c_ulong(1), byref(c_ulong(0)), None)

#High level api
api.control_transfer(pkt1, buff=[0])

Much simpler!

I wouldn’t dare to call it a stable version so I labeled 0.1. Give it a try if you want! 🙂

Repository
Complete example using low level api.
Complete example using high level api

Advertisements