首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c#程序读取usb数据

c#程序读取usb数据
EN

Stack Overflow用户
提问于 2016-03-16 02:32:18
回答 1查看 1.1K关注 0票数 0

嗨,这是我的毕业设计的程序,从usb读取,但即使我连接usb到电脑,它给我每次“设备找不到”,请帮助我

代码:

代码语言:javascript
复制
namespace Grad_pro
{
    public class read_data
    {
        public static UsbDevice MyUsbDevice;

        #region SET YOUR USB Vendor and Product ID!

        public static UsbDeviceFinder MyUsbFinder =
            new UsbDeviceFinder(0x0000, 0x0000); // specify vendor, product id

        #endregion

        public static void Main(string[] args)
        {
            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.");

                // If this is a "whole" usb device (libusb-win32, linux libusb)
                // it will have an IUsbDevice interface. If not (WinUSB) the
                // variable will be null indicating this is an interface of a
                // device.
                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
                    wholeUsbDevice.SetConfiguration(1);

                    // Claim interface
                    wholeUsbDevice.ClaimInterface(1);
                }

                // open read endpoint
                UsbEndpointReader reader =
                    MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep02);

                // open write endpoint
                UsbEndpointWriter writer =
                    MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep03);

                // write data, read data
                int bytesWritten;
                ec = writer.Write(new byte[] { 0x00, 0x00 }, 2000, out bytesWritten); // specify data to send

                if (ec != ErrorCode.None)
                    throw new Exception(UsbDevice.LastErrorString);

                byte[] readBuffer = new byte[1024];
                while (ec == ErrorCode.None)
                {
                    int bytesRead;

                    // If the device hasn't sent data in the last 100 milliseconds,
                    // a timeout error (ec = IoTimedOut) will occur.
                    ec = reader.Read(readBuffer, 100, out bytesRead);

                    if (bytesRead == 0) throw new Exception("No more bytes!");

                    // Write that output to the console.
                    Console.WriteLine(BitConverter.ToString(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)
                    {
                        // If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
                        // it exposes an IUsbDevice interface. If not (WinUSB) the
                        // 'wholeUsbDevice' variable will be null indicating this is
                        // an interface of a device; it does not require or support
                        // configuration and interface selection.
                        IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                        if (!ReferenceEquals(wholeUsbDevice, null))
                        {
                            // Release interface
                            wholeUsbDevice.ReleaseInterface(1);
                        }

                        MyUsbDevice.Close();
                    }
                    MyUsbDevice = null;

                    // Free usb resources
                    UsbDevice.Exit();

                }

                // Wait for user input..
                Console.ReadKey();
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2019-01-02 05:15:29

目前,LibUsbDotNet库似乎正在经历一些变化。我没有幸运地让任何旧版本的库工作。但是,有一个alpha版本确实可以工作。我向他们提交了这个sample,它被接受了。它适用于我正在与之交谈的设备(只要您向libusb dll添加一个物理引用。

但是,我不推荐使用LibUsb。LibUSb只是标准Windows USB和Hid API调用的C包装器。如果你想要一些与Linux跨平台的东西,这是很有用的,但是据我所知,LibUsb不支持其他平台。

我建议直接使用Windows HidUSB API。这些是Usb.Net and Hid.Net的一部分。如果您确实需要保持代码与Linux的跨平台,您仍然可以使用多态性来wrap Linux上的LibUsbDotNet库:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36019355

复制
相关文章

相似问题

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