Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用LibUsbDotNet与C中的USB设备通信_C#_C# 4.0 - Fatal编程技术网

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

C# 使用LibUsbDotNet与C中的USB设备通信,c#,c#-4.0,C#,C# 4.0,0在Windows7 Ultimate 32位和已安装的linusb-win32-dlevel-filter 1.2.6.0下工作 1我尝试了libusb dotnet的showinfo示例,得到了以下结果,[请查看结果1-result] 2之后,我试图读取libusb的轮询示例,但在ec=reader.ReadreadBuffer,1000,out bytesRead行上出现错误; 错误是win32error:没有更多字节![请参见代码2-代码] 这个错误是什么意思?我如何解决它? 其实我是一

0在Windows7 Ultimate 32位和已安装的linusb-win32-dlevel-filter 1.2.6.0下工作

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

2之后,我试图读取libusb的轮询示例,但在ec=reader.ReadreadBuffer,1000,out bytesRead行上出现错误; 错误是win32error:没有更多字节![请参见代码2-代码]

这个错误是什么意思?我如何解决它? 其实我是一个新的usb设备通信,请帮助我,如果有任何想法usb设备通信使用c

1-结果:-

长度:18 描述符类型:设备 BcdUsb:0x0110 类别:通信 子类:0x00 协议:0x00 MaxPacketSize0:64 供应商ID:0x11CA 产品ID:0x0241 BCD设备:0x0100 制造商序列索引:1 ProductStringIndex:2 序列号索引:3 配置计数:1 制造商字符串:VeriFone公司 ProductString:Trident USB设备1.1 串行字符串:

长度:9 描述符类型:配置 总长度:67 接口计数:2 配置ID:1 字符串索引:0 属性:0xC0 最大功率:25 配置字符串:

长度:7 描述符类型:端点 端点ID:0x85 属性:0x03 最大包装尺寸:16 间隔:0 刷新:0 同步地址:0x00

长度:9 描述符类型:接口 接口ID:1 交替ID:0 端点计数:2 类别:数据 子类:0x00 协议:0x00 字符串索引:0 接口线:

长度:7 描述符类型:端点 端点ID:0x81 属性:0x02 MaxPacketSize:64 间隔:0 刷新:0 同步地址:0x00

长度:7 描述符类型:端点 端点ID:0x03 属性:0x02 最大包装尺寸:32 间隔:0 刷新:0 同步地址:0x00

2-代码:-

公共静态UsbDeviceFinder MyUsbFinder=新的UsbDeviceFinder 4554577

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)