本人使用python,参考以下网址修改代码 https://github.com/thesacredmoocow/g14control-r2/blob/cc8064b38dbddca551f0301a254b578c508122f5/winusbpy/examples/winusbtest2.py
和
import time
from winusbpy import *
from ctypes import *
from ctypes.wintypes import *
DIGCF_DEFAULT = 0x00000001
DIGCF_PRESENT = 0x00000002
DIGCF_ALLCLASSES = 0x00000004
DIGCF_PROFILE = 0x00000008
DIGCF_DEVICE_INTERFACE = 0x00000010
GENERIC_WRITE = (1073741824)
GENERIC_READ = (-2147483648)
FILE_SHARE_READ = 1
FILE_SHARE_WRITE = 2
OPEN_EXISTING = 3
OPEN_ALWAYS = 4
FILE_ATTRIBUTE_NORMAL = 128
FILE_FLAG_OVERLAPPED = 1073741824
INVALID_HANDLE_VALUE = HANDLE(-1)
PIPE_TYPE_CONTROL = 0
PIPE_TYPE_ISO = 1
PIPE_TYPE_BULK = 2
PIPE_TYPE_INTERRUPT = 3
ERROR_IO_INCOMPLETE = 996
ERROR_IO_PENDING = 997
vid = "1234"
pid = "5678"
path = ""
interface_descriptor = UsbInterfaceDescriptor()
api = WinUSBApi()
byte_array = c_byte * 8
guid = GUID(0xA5DCBF10, 0x6530, 0x11D2, byte_array(0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED))
flags = DWORD(DIGCF_DEVICE_INTERFACE | DIGCF_PRESENT)
i = 0
member_index = DWORD(i)
required_size = c_ulong(0)
sp_device_info_data = SpDevinfoData()
sp_device_interface_data = SpDeviceInterfaceData()
sp_device_interface_detail_data = SpDeviceInterfaceDetailData()
sp_device_interface_data.cb_size = sizeof(sp_device_interface_data)
sp_device_info_data.cb_size = sizeof(sp_device_info_data)
hdev_info = api.exec_function_setupapi("SetupDiGetClassDevs", byref(guid), None, None, flags)
while api.exec_function_setupapi("SetupDiEnumDeviceInterfaces", hdev_info, None, byref(guid), member_index,
byref(sp_device_interface_data)):
api.exec_function_setupapi("SetupDiGetDeviceInterfaceDetail", hdev_info, byref(sp_device_interface_data), None, 0,
byref(required_size), None)
resize(sp_device_interface_detail_data, required_size.value)
sp_device_interface_detail_data.cb_size = sizeof(SpDeviceInterfaceDetailData) - sizeof(WCHAR * 1)
if api.exec_function_setupapi("SetupDiGetDeviceInterfaceDetail", hdev_info, byref(sp_device_interface_data),
byref(sp_device_interface_detail_data), required_size, byref(required_size),
byref(sp_device_info_data)):
path = wstring_at(byref(sp_device_interface_detail_data, sizeof(DWORD)))
print(path)
if is_device(vid, pid, path):
print("PATH: " + path)
break
else:
error_code = api.exec_function_kernel32("GetLastError")
print("Error: " + str(error_code))
i += 1
member_index = DWORD(i)
required_size = c_ulong(0)
resize(sp_device_interface_detail_data, sizeof(SpDeviceInterfaceDetailData))
handle_file = api.exec_function_kernel32("CreateFileW", path, GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_WRITE | FILE_SHARE_READ, None, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, None)
if INVALID_HANDLE_VALUE == handle_file:
print("Error Creating File")
else:
print("No error")
handle_winusb = c_void_p()
result = api.exec_function_winusb("WinUsb_Initialize", handle_file, byref(handle_winusb))
print(result)
if result != 0:
info_type = c_ulong(1)
buff = (c_void_p * 1)()
buff_length = c_ulong(sizeof(c_void_p))
result = api.exec_function_winusb("WinUsb_QueryDeviceInformation", handle_winusb, info_type, byref(buff_length),
buff)
if result != 0:
print("Speed: " + str(buff[0]))
else:
error_code = api.exec_function_kernel32("GetLastError")
print("Error Query Device: " + str(error_code))
""" Interface Settings """
response = api.exec_function_winusb("WinUsb_QueryInterfaceSettings", handle_winusb, c_ubyte(0),
byref(interface_descriptor))
if response == 0:
error_code = api.exec_function_kernel32("GetLastError")
print("Error Query Interface: " + str(error_code))
if response != 0:
print("bLength: " + str(interface_descriptor.b_length))
print("bDescriptorType: " + str(interface_descriptor.b_descriptor_type))
print("bInterfaceNumber: " + str(interface_descriptor.b_interface_number))
print("bAlternateSetting: " + str(interface_descriptor.b_alternate_setting))
print("bNumEndpoints " + str(interface_descriptor.b_num_endpoints))
print("bInterfaceClass " + str(interface_descriptor.b_interface_class))
print("bInterfaceSubClass: " + str(interface_descriptor.b_interface_sub_class))
print("bInterfaceProtocol: " + str(interface_descriptor.b_interface_protocol))
print("iInterface: " + str(interface_descriptor.i_interface))
""" Endpoints information """
i = 0
endpoint_index = c_ubyte(0)
pipe_info = PipeInfo()
while i <= interface_descriptor.b_num_endpoints:
result = api.exec_function_winusb("WinUsb_QueryPipe", handle_winusb, c_ubyte(0), endpoint_index,
byref(pipe_info))
if result != 0:
print("PipeType: " + str(pipe_info.pipe_type))
print("PipeId: " + str(pipe_info.pipe_id))
print("MaximumPacketSize: " + str(pipe_info.maximum_packet_size))
print("Interval: " + str(pipe_info.interval))
i += 1
endpoint_index = c_ubyte(i)
else:
error_code = api.exec_function_kernel32("GetLastError")
print("Error" + str(error_code))
但是我打印这段代码总是得到 0 结果并得到错误 31 我做错了什么
result = api.exec_function_winusb("WinUsb_Initialize", handle_file, byref(handle_winusb))