首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C#中使用LibUsbDotNet与USB设备通信

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

Stack Overflow用户
提问于 2014-01-23 07:28:22
回答 1查看 8.4K关注 0票数 0

0)在Windows7极限32位下工作,安装了linusb-win32-dlevel-过滤器1.2.6.0.

1)我尝试了libusb dotnet的展示信息示例,得到了如下结果,请查看结果(1-结果)。

2)之后,我试图读取(轮询) libusb的示例,但在"ec = reader.Read(readBuffer,1000,out bytesRead)“中得到了错误;错误是”win32Error:不再有字节!“请参阅代码(2-代码)

这个错误意味着什么?我如何解决它?实际上我在usb设备通信方面是新手,如果对使用c#的usb设备通信有什么想法,请帮助我。

1-结果:-

长度:18 DescriptorType:Device BcdUsb:0x0110类:Comm子类:0x00协议:0x00 MaxPacketSize0:64供应商Inc :0x11CA ProductID:0x0241 BcdDevice:0x0100 ManufacturerStringIndex:1 ProductStringIndex:2 SerialStringIndex:3 ConfigurationCount:1 ManufacturerString:VeriFone Inc ProductString:Trident 1.1 SerialString:

长度:9 DescriptorType:Configuration TotalLength:67 InterfaceCount:2 ConfigID:1 StringIndex:0 Attribute:0xC0 MaxPower:25 ConfigString:

长度:7 DescriptorType:终结点16:0x85属性:0x03 MaxPacketSize:16间隔:0 Refresh:0 SynchAddress:0x00

长度:9 DescriptorType:接口接口Interface:1 AlternateID:0 EndpointCount:2 Class:Data子类:0x00协议:0x00 StringIndex:0 InterfaceString:

长度:7 DescriptorType:端点EndpointID:0x81属性:0x02 MaxPacketSize:64 Interval:0 Refresh:0 SynchAddress:0x00

长度:7 DescriptorType:端点EndpointID:0x03属性:0x02 MaxPacketSize:32 Interval:0 Refresh:0 SynchAddress:0x00

2-“法典”:-

公共静态UsbDeviceFinder MyUsbFinder =新UsbDeviceFinder(4554,577);

ErrorCode ec = ErrorCode.None;

代码语言:javascript
复制
        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();
        }
EN

回答 1

Stack Overflow用户

发布于 2014-03-29 08:37:56

注意描述符,您必须将接口设置为1

代码语言:javascript
复制
wholeUsbDevice.ClaimInterface(1)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21301923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档