简体   繁体   English

在C#中使用LibUsbDotNet与HID设备进行交互

[英]Interacting with HID devices using LibUsbDotNet in C#

Simply put: I would like to know if its possible, with LibUsbDotNet, to read and write HID feature reports. 简而言之: 我想知道是否可以通过LibUsbDotNet读写HID功能报告。

In the first place I tried to find my device using vid/pid with following example code 首先,我尝试使用vid / pid和以下示例代码找到我的设备

class Program
    {
        public static UsbDevice MyUsbDevice;
        public static UsbDeviceFinder uFind = new UsbDeviceFinder(1234,4321);

        static void Main(string[] args)
        {
            ErrorCode ec = ErrorCode.None;

            try
            {
                // Find and open the usb device.
                MyUsbDevice = UsbDevice.OpenUsbDevice(uFind);

                // If the device is open and ready
                if (MyUsbDevice == null) throw new Exception("Device Not Found.");

But I would always get Device Not Found. 但是我总是会找到“找不到设备”。

I have checked under "Device manager > Human Interface Devices" my device does exists. 我已经在“设备管理器>人机接口设备”下检查了我的设备是否存在。

It would be helpful if someone can suggest what is the right method to achieve the same in c#. 如果有人可以提出在c#中实现相同目标的正确方法 ,那将是有帮助的。

Previously I tried to achieve the same in python( using PyUsb ) but lack of examples and documentation made me hopeless. 以前,我尝试在python(使用PyUsb)中实现相同的功能,但是由于缺少示例和文档,这使我无望。

Inside the installation folder of libUsbDotNet there is a file named "inf-wizard.exe" run that app and install a "fake driver" used by libUsbDotNet to find the driver. 在libUsbDotNet的安装文件夹中,有一个名为“ inf-wizard.exe”的文件运行该应用程序,并安装libUsbDotNet使用的“假驱动程序”来查找驱动程序。

if you can't find "inf-wizard.exe" try downloading libUsbDotNet from here: http://sourceforge.net/projects/libusbdotnet/ 如果找不到“ inf-wizard.exe”,请尝试从此处下载libUsbDotNet: http : //sourceforge.net/projects/libusbdotnet/

You may have to use the device manager to find the Pid and Vid for the specific driver(even if no driver is found it the Vid and Pid should be displayed) 您可能必须使用设备管理器来找到特定驱动程序的Pid和Vid(即使未找到驱动程序,也应显示Vid和Pid)

After installing convert the Pid and Vid to integers, this site may be helpfull http://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html 安装后,将Pid和Vid转换为整数,此站点可能会有所帮助http://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html

use the converted integers in the code ex: 在代码ex中使用转换后的整数:

//Hex Values: Pid = 4D9; Vid = 1605; 

public static UsbDeviceFinder uFind = new UsbDeviceFinder(1241, 5637);

Windows requires a driver to commuticate with any type of hardware, this process is a lot simpler in linux, see link (connect hack) for a good simplification on how usb communication works. Windows需要一个驱动程序来与任何类型的硬件通信,在Linux中此过程要简单得多,请参阅链接(连接hack)以更好地简化USB通信的工作方式。 (This is linux based, but the core stays the same) (这是基于linux的,但核心保持不变)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM