簡體   English   中英

使用 LibUsbDotNet 在 C# 中與 USB 設備通信

[英]Communicate with USB device in C# using LibUsbDotNet

0) 在 Windows7 Ultimate 32 位下工作並安裝 linusb-win32-dl​​evel-filter 1.2.6.0。

1)我嘗試了libusb dotnet的showinfo示例並得到以下結果,[請查看結果(1-結果)]

2) 之后我嘗試讀取(輪詢)libusb 的示例,但在“ec = reader.Read(readBuffer, 1000, out bytesRead);”行出現錯誤錯誤是“win32error:no more byte!” [請看代碼(2-代碼)]

這個錯誤是什么意思,我該如何解決? 實際上我是usb設備通信的新手,如果對使用c#進行usb設備通信有任何想法,請幫助我

1-結果:-

長度:18 描述符類型:設備 BcdUsb:0x0110 類:通信子類:0x00 協議:0x00 MaxPacketSize0:64 供應商 ID:0x11CA 產品 ID:0x0241 BcdDevice:0x0100 制造商字符串索引:1 產品字符串索引:2 序列字符串索引:3 配置計數:1 制造商字符串:VeriFone Inc 產品字符串:Trident USB設備 1.1 序列號:

長度:9 DescriptorType:配置 TotalLength:67 InterfaceCount:2 ConfigID:1 StringIndex:0 Attributes:0xC0 MaxPower:25 ConfigString:

長度:7 描述符類型:端點 EndpointID:0x85 屬性:0x03 MaxPacketSize:16 間隔:0 刷新:0 同步地址:0x00

長度:9 描述符類型:接口接口 ID:1 備用 ID:0 端點計數:2 類:數據子類:0x00 協議:0x00 字符串索引:0 接口字符串:

長度:7 描述符類型:端點 EndpointID:0x81 屬性:0x02 MaxPacketSize:64 間隔:0 刷新:0 同步地址:0x00

長度:7 描述符類型:端點 EndpointID:0x03 屬性:0x02 MaxPacketSize:32 間隔:0 刷新:0 同步地址:0x00

2-代碼:-

公共靜態 UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(4554, 577);

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.");

            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)
                {

                    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();
        }

通過注意您的描述符,您必須將您的接口設置為1

wholeUsbDevice.ClaimInterface(1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM