English 中文(简体)
利用LibUsbDotNet在C#中安装的BUS装置
原标题:Communicate with USB device in C# using LibUsbDotNet
  • 时间:2014-01-23 07:28:22
  •  标签:
  • c#
  • c#-4.0

0) 在Windows7终端32轨道下工作,安装了Llinusb-win32级过滤器1.2.6.0。

(1) 我尝试了利布斯布特网的示例,并在结果之后学习。

2) After that i am trying to read (polling) example of libusb but got error on line "ec = reader.Read(readBuffer, 1000, out bytesRead);" and error is "win32error:no more byte!" [Please see code (2-Code) ]

What is this error means and how can i solve it? Actually i am new in usb device communication, please help me if have any idea to usb device communication using c#

页: 1

Length:18 DescriptorType:Device BcdUsb:0x0110 Class:Comm SubClass:0x00 Protocol:0x00 MaxPacketSize0:64 VendorID:0x11CA ProductID:0x0241 BcdDevice:0x0100 ManufacturerStringIndex:1 ProductStringIndex:2 SerialStringIndex:3 ConfigurationCount:1 ManufacturerString:VeriFone Inc ProductString:Trident USB Device 1.1 SerialString:

Length:9 DescriptorType:Configuration TotalLength:67 InterfaceCount:2 ConfigID:1 StringIndex:0 Attributes:0xC0 MaxPower:25 ConfigString:

Length:7 DescriptorType:Endpoint EndpointID:0x85 Attributes:0x03 MaxPacketSize:16 Interval:0 Refresh:0 SynchAddress:0x00

Length:9 DescriptorType:Interface InterfaceID:1 AlternateID:0 EndpointCount:2 Class:Data SubClass:0x00 Protocol:0x00 StringIndex:0 InterfaceString:

Length:7 DescriptorType:Endpoint EndpointID:0x81 Attributes:0x02 MaxPacketSize:64 Interval:0 Refresh:0 SynchAddress:0x00

Length:7 DescriptorType:Endpoint EndpointID:0x03 Attributes:0x02 MaxPacketSize:32 Interval:0 Refresh:0 SynchAddress:0x00

《刑法》:

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("
Done!
");
        }
        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();
        }
问题回答

请注意您的描述,请将<>interface作为1

wholeUsbDevice.ClaimInterface(1)




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签