How to send a message to a USB device

How to send a message to a USB device

I need to be able to send a short set of bytes to a USB device, but don't know how.
I know the vendor and product ID's, and the message string (but I need help ascertaining whether it's an ASCII string or a hex string).

There's a piece of software called usb_modeswitch that seems to do this, but it's linux only.
I've seen a thread somewhere where people have compiled it with mingw, but didn't provide their binaries and I'm not comfortable trying it myself.

Someone mentioned using WinUSB which would be ideal given its native inclusion, but I don't know the interface.
I do have/can get Visual Studio, but I'd need help figuring out what to use from stuff I see online.

A good alternative seems to be PyUSB with libusb-win32 (a port of what usb_modeswitch uses).
Which could be used like this instead with writing instead of reading, but I still need to know whether to send just a string or try to convert to bytes from a hex representation before calling write().

This is the info:

DefaultVendor=046d
DefaultProduct=c261
MessageEndpoint=01
ResponseEndpoint=01
TargetClass=0x03
MessageContent="0f00010142"

Context
If it helps.

I have a Logitech G920 that's been acting up, it refuses to be recognised as device on my machine.

Devices and Printers screenshot

No amount of uninstalling drivers, trying other usb slots, plugging in unpowered, removing LGS works. But strangely enough, it still works on my laptop and the xbox one itself.

I'm on the edge of giving up and reformatting my machine when I come across this thread: post screenshot

Sure enough:

device manager screenshot

That's where I found this post. Some clever people have fixed this issue for themselves on linux, using software made to ask those usb modems into switching between their driver-installing-flashdrive and internet-giving-modem modes.
The above info came from there and I don't know the significance of the '%b/%k', I hope it's a usb_modeswitch-specific thing that isn't necessary.

Now I just need to get it working in windows.
Thanks for any help.

答案1

YES!

I ended up going with pyusb, and because the wheel is a HID device you need to overwrite the driver using Zadig (libusb-win32 was the only one that worked). After also getting both libusb-win32 and libusb1.0 dynamic-link libraries for python to use (I don't know which, I was trying a lot of things) this is the code that worked:

import usb.core
import usb.util
import usb.backend.libusb1
import sys

VENDOR_ID = 0x046D
PRODUCT_ID = 0xC261

BACKEND = usb.backend.libusb1.get_backend(find_library=lambda x: "libusb-1.0.dll")


dev = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID, backend=BACKEND)

if dev is None:
    raise ValueError('Device not found')

dev.write(1, '0f00010142'.decode("hex"))

success

答案2

I stumbled upon another answer. If you have a VirtualBox linux machine, you can connect the USB device through VirtualBox to the machine, then run usb_modeswitch. The "storage" device containing the drivers will disappear, and the actual device will reappear on Windows.

相关内容