UsbLibDotNet示例不起作用

编程入门 行业动态 更新时间:2024-10-27 00:34:51
本文介绍了UsbLibDotNet示例不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嘿大家。 我正在尝试使用C#库UsbLibDotNet与我的隐藏设备进行通信,这是推荐的。 但是我无法举例说明。它总是触发未找到设备。但是,我运行了第一个显示所有隐藏设备信息的示例,它也显示了我的设备。 我无法理解。示例代码如下...

Hey everyone. I'm trying to communicate with my hid device using C# library UsbLibDotNet which is recommended here. But I couldn't make the examples for. it always fire "Device not Found". However, I run the very first examples that show all hid devices information and it shows my device too. I couldn't understand that. The example code is below...

using System; using System.Text; using LibUsbDotNet; using LibUsbDotNet.Main; namespace Examples { internal class ReadPolling { public static UsbDevice MyUsbDevice; #region SET YOUR USB Vendor and Product ID! public static UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(1234, 1234); #endregion public static void Main(string[] args) { ErrorCode ec = ErrorCode.None; try { // Find and open the usb device. MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder); // If the device is open and ready if (MyUsbDevice == null) throw new Exception("Device Not Found."); // If this is a "whole" usb device (libusb-win32, linux libusb-1.0) // it exposes an IUsbDevice interface. If not (WinUSB) the // 'wholeUsbDevice' variable will be null indicating this is // an interface of a device; it does not require or support // configuration and interface selection. IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice; if (!ReferenceEquals(wholeUsbDevice, null)) { // This is a "whole" USB device. Before it can be used, // the desired configuration and interface must be selected. // Select config #1 wholeUsbDevice.SetConfiguration(1); // Claim interface #0. wholeUsbDevice.ClaimInterface(0); } // open read endpoint 1. UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01); byte[] readBuffer = new byte[1024]; while (ec == ErrorCode.None) { int bytesRead; // If the device hasn't sent data in the last 5 seconds, // a timeout error (ec = IoTimedOut) will occur. ec = reader.Read(readBuffer, 5000, out bytesRead); if (bytesRead == 0) throw new Exception(string.Format("{0}:No more bytes!", ec)); Console.WriteLine("{0} bytes read", bytesRead); // Write that output to the console. Console.Write(Encoding.Default.GetString(readBuffer, 0, bytesRead)); } Console.WriteLine("\r\nDone!\r\n"); } catch (Exception ex) { Console.WriteLine(); Console.WriteLine((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message); } finally { if (MyUsbDevice != null) { if (MyUsbDevice.IsOpen) { // If this is a "whole" usb device (libusb-win32, linux libusb-1.0) // it exposes an IUsbDevice interface. If not (WinUSB) the // 'wholeUsbDevice' variable will be null indicating this is // an interface of a device; it does not require or support // configuration and interface selection. IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice; if (!ReferenceEquals(wholeUsbDevice, null)) { // Release interface #0. wholeUsbDevice.ReleaseInterface(0); } MyUsbDevice.Close(); } MyUsbDevice = null; // Free usb resources UsbDevice.Exit(); } // Wait for user input.. Console.ReadKey(); } } } }

我的设备的描述符文件...

And my descriptor file for my device...

<pre> const unsigned int USB_VENDOR_ID = 0x1234; const unsigned int USB_PRODUCT_ID = 0x1234; const char USB_SELF_POWER = 0x80; // Self powered 0xC0, 0x80 bus powered const char USB_MAX_POWER = 50; // Bus power required in units of 2 mA const char HID_INPUT_REPORT_BYTES = 64; const char HID_OUTPUT_REPORT_BYTES = 64; const char USB_TRANSFER_TYPE = 0x03; //0x03 Interrupt const char EP_IN_INTERVAL = 1; const char EP_OUT_INTERVAL = 1; const char USB_INTERRUPT = 1; const char USB_HID_EP = 1; const char USB_HID_RPT_SIZE = 33; /* Device Descriptor */ const struct { char bLength; // bLength - Descriptor size in bytes (12h) char bDescriptorType; // bDescriptorType - The constant DEVICE (01h) unsigned int bcdUSB; // bcdUSB - USB specification release number (BCD) char bDeviceClass; // bDeviceClass - Class Code char bDeviceSubClass; // bDeviceSubClass - Subclass code char bDeviceProtocol; // bDeviceProtocol - Protocol code char bMaxPacketSize0; // bMaxPacketSize0 - Maximum packet size for endpoint 0 unsigned int idVendor; // idVendor - Vendor ID unsigned int idProduct; // idProduct - Product ID unsigned int bcdDevice; // bcdDevice - Device release number (BCD) char iManufacturer; // iManufacturer - Index of string descriptor for the manufacturer char iProduct; // iProduct - Index of string descriptor for the product. char iSerialNumber; // iSerialNumber - Index of string descriptor for the serial number. char bNumConfigurations; // bNumConfigurations - Number of possible configurations } device_dsc = { 0x12, // bLength 0x01, // bDescriptorType 0x0200, // bcdUSB 0x00, // bDeviceClass 0x00, // bDeviceSubClass 0x00, // bDeviceProtocol 8, // bMaxPacketSize0 USB_VENDOR_ID, // idVendor USB_PRODUCT_ID, // idProduct 0x0001, // bcdDevice 0x01, // iManufacturer 0x02, // iProduct 0x00, // iSerialNumber 0x01 // bNumConfigurations }; /* Configuration 1 Descriptor */ const char configDescriptor1[]= { // Configuration Descriptor 0x09, // bLength - Descriptor size in bytes 0x02, // bDescriptorType - The constant CONFIGURATION (02h) 0x29,0x00, // wTotalLength - The number of bytes in the configuration descriptor and all of its subordinate descriptors 1, // bNumInterfaces - Number of interfaces in the configuration 1, // bConfigurationValue - Identifier for Set Configuration and Get Configuration requests 0, // iConfiguration - Index of string descriptor for the configuration USB_SELF_POWER, // bmAttributes - Self/bus power and remote wakeup settings USB_MAX_POWER, // bMaxPower - Bus power required in units of 2 mA // Interface Descriptor 0x09, // bLength - Descriptor size in bytes (09h) 0x04, // bDescriptorType - The constant Interface (04h) 0, // bInterfaceNumber - Number identifying this interface 0, // bAlternateSetting - A number that identifies a descriptor with alternate settings for this bInterfaceNumber. 2, // bNumEndpoint - Number of endpoints supported not counting endpoint zero 0x03, // bInterfaceClass - Class code 0,

} 任何有效的人有这个库之前可能有帮助吗? 我最好的问候...

} Anyone that works with this library before could help maybe? My Best Regards...

推荐答案

你好运行Test_Info.exe这是在prog文件/ Libusb下... 这将给出一个输出。查找vendorid和产品ID。我的设备的那些是 VendorID:0x04D8 ProductID:0xFEAA。将它们转换为十进制,并用这些值替换'1234'。希望它能正常工作 Hi Run Test_Info.exe which is under prog files/Libusb... That will give an output. Find vendorid and product id. Those for my device were VendorID:0x04D8 ProductID:0xFEAA. Convert them to decimal and replace those '1234's with these values. Hope it'll work

当我们无法访问您的硬件以及您如何配置测试环境时,很难给您任何帮助。 It's very hard to give you any help when we do not have access to your hardware and how you have configured your testing environment.

正如您所提到的,这是直接来自LibUsbDotNet的示例。它确实构建并运行您的描述。您很可能没有设置允许您与设备通信的过滤器驱动程序。 在Program Files \LibUsbDotNet \ libusb-win32中文件夹,你应该找到install-filter-win.exe和install-filter.exe。您需要运行其中一个并安装过滤器。这是一个非常简单的过程,但您需要知道您的设备VID和PID是什么,以及它是否有多个接口,哪一个是您需要的接口,并选择它来安装过滤器。 您可以使用命令行install-filter.exe在所有设备上安装过滤器。您不希望系统中的所有USB设备都带有过滤器,但这是确保您看到设备的简便方法。选择以管理员身份运行,打开命令提示符。安装到可以过滤的所有USB设备的命令行是: install-filter -i -ac 您可以使用以下命令删除所有过滤器: install-filter -u -ac 希望这有帮助。 As you mentioned, this is a sample directly from LibUsbDotNet. It does build and run from your description. It is very likely that you have not setup the filter driver that allows you to communicate to your device. In your "Program Files\LibUsbDotNet\libusb-win32" folder, you should find "install-filter-win.exe" and "install-filter.exe". You need to run one of these and install a filter. It is a very simple process but you will need to know what your device VID and PID and if it has multiple interfaces, which one is the one you need and select it to install a filter. You can used the command line, "install-filter.exe" to install filters on all your devices. You will not want to leave your system with all USB devices having a filter but this is an easy way to make sure you see your device. Open a command prompt by choosing to run as Administrator. Command line to install to all usb device that can be filtered is: install-filter -i -ac You can use this to remove all filters also with the following command: install-filter -u -ac Hope this helps.

更多推荐

UsbLibDotNet示例不起作用

本文发布于:2023-11-15 07:06:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1592230.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   不起作用   UsbLibDotNet

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!