使用LibUsbDotNet在C#中与USB设备通信

时间:2014-01-23 07:28:22

标签: c# c#-4.0

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

1)我尝试了libusb dotnet的showinfo示例,得到了以下结果,[请看结果(1-Result)]

2)之后我试图读取(轮询)libusb的例子,但在线路上出现错误“ec = reader.Read(readBuffer,1000,out bytesRead);” 错误是“win32error:没有更多的字节!” [请参阅代码(2-Code)]

这个错误意味着什么,我该如何解决? 其实我是usb设备通讯的新手,如果对使用c#的usb设备通信有任何想法请帮帮我

1 - 结果: -

长度:18 描述符类型:设备 BcdUsb:0x0110 类别:通讯 亚纲:为0x00 协议:为0x00 MaxPacketSize0:64 卖方ID:0x11CA 产品编号:0x0241 BcdDevice:0100 ManufacturerStringIndex:1 ProductStringIndex:2 SerialStringIndex:3 ConfigurationCount:1 ManufacturerString:VeriFone Inc. ProductString:Trident USB Device 1.1 SerialString:

长度:9 描述符类型:配置 TotalLength:67 InterfaceCount:2 ConfigID来:1 StringIndex:0 属性:将0xC0 牛魔王:25 ConfigString:

长度:7 描述符类型:端点 的EndpointId:0x85 属性:×03 MaxPacketSize:16 间隔:0 刷新:0 SynchAddress:0×00

长度:9 描述符类型:接口 InterfaceID:1 AlternateID:0 EndpointCount:2 类别:数据 亚纲:为0x00 协议:为0x00 StringIndex:0 InterfaceString:

长度:7 描述符类型:端点 的EndpointId:0×81 属性:0×02 MaxPacketSize:64 间隔:0 刷新:0 SynchAddress:0×00

长度:7 描述符类型:端点 的EndpointId:0×03 属性:0×02 MaxPacketSize:32 间隔:0 刷新:0 SynchAddress:0×00

2-代码: -

public static 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 个答案:

答案 0 :(得分:0)

注意您的描述符,您必须将界面设置为 1

wholeUsbDevice.ClaimInterface(1)