C#如何使用LibUsbDotNet实现USB通信的示例详解

发布时间:2022-05-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了C#如何使用LibUsbDotNet实现USB通信的示例详解脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
网上C#usb通信的资料比较少, 基本上都是基于LibUsbDotNet 和 CyUsb, 关于打印机设备的还有一个OPOS。

本篇文章基于LibUsbDotNet。

  1. 下载并安装 LibUsbDotNet 安装文件。

  2. 运行Filter Wizard, Install a device filter。 安装需要通信的USB设备

  

  3. 建一个简单的控制台项目,进行测试, 下图为打印需要通信设备的信息。

  

相关代码:  

引用  


using LibUsbDotNet;
using LibUsbDotNet.Main;
using LibUsbDotNet.Info;

PRintUsbInfo


 public static void PrintUsbInfo()
        {
            UsbDevice usbDevice = null;
            UsbregDeviceList allDevices = UsbDevice.AllDevices;

            Console.WrITeLine("Found {0} devices", allDevices.Count);

            foreach (UsbRegistry usbRegistry in allDevices)
            {
                Console.WriteLine("Got device: {0}\r\n", usbRegistry.FullName);

                if (usbRegistry.OPEn(out usbDevice))
                {
                    Console.WriteLine("Device Information\r\n------------------");

                    Console.WriteLine("{0}", usbDevice.Info.ToString());

                    Console.WriteLine("VID & PID: {0} {1}", usbDevice.Info.Descriptor.VendorID, usbDevice.Info.Descriptor.ProductID);

                    Console.WriteLine("\r\nDevice configuration\r\n--------------------");
                    foreach (UsbConfiginfo usbConfigInfo in usbDevice.Configs)
                    {
                        Console.WriteLine("{0}", usbConfigInfo.ToString());

                        Console.WriteLine("\r\nDevice interface list\r\n---------------------");
                        ReadOnlyCollection<UsbInterfaceinfo> interfaceList = usbConfigInfo.InterfaceInfoList;
                        foreach (UsbInterfaceInfo usbInterfaceInfo in interfaceList)
                        {
                            Console.WriteLine("{0}", usbInterfaceInfo.ToString());

                            Console.WriteLine("\r\nDevice endpoint list\r\n--------------------");
                            ReadOnlyCollection<UsbEndpointInfo> endpointList = usbInterfaceInfo.EndpointInfoList;
                            foreach (UsbEndpointInfo usbEndpointInfo in endpointList)
                            {
                                Console.WriteLine("{0}", usbEndpointInfo.ToString());
                            }
                        }
                    }
                    usbDevice.Close();
                }
                Console.WriteLine("\r\n----- Device information finished -----\r\n");
            }
        }

调用


public static void Main(string[] args)
        {
            PrintUsbInfo();

            // Wait for user input..
            Console.ReadKey();
        }

以上就是C#如何使用LibUsbDotNet实现USB通信的示例详解的详细内容,更多请关注脚本宝典其它相关文章!

脚本宝典总结

以上是脚本宝典为你收集整理的C#如何使用LibUsbDotNet实现USB通信的示例详解全部内容,希望文章能够帮你解决C#如何使用LibUsbDotNet实现USB通信的示例详解所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:csharpcsharp