首頁>Program>source

使用.Net(C#),如何使用USB設備?

如何檢測USB事件(連線/斷開連線)以及如何与設備通訊(讀/寫)。

是否有本機.Net解決方案?

最新回復
  • 5月前
    1 #

    尚無native(例如系統庫)解決方案.這就是moobaa提到存在SharpUSBLib的原因。

    如果您希望使用自己的USB設備處理程式,可以查看System.IO.Ports的SerialPort類。

  • 5月前
    2 #

    我尝試使用SharpUSBLib並將它弄壞了我的計算機( 需要系統還原).也發生在同一專案的同事身上。

    我在LibUSBDotNet中找到了一種替代方法:http://sourceforge.net/projects/libusbdotnet Hav並没有使用太多,但看起来不錯,並且最近进行了更新(不同於Sharp)。

    編輯:截至2017年2月中,LibUSBDotNet大約在2週前进行了更新.同時SharpUSBLib自2004年以来就没有更新過。

  • 5月前
    3 #

    我使用以下代碼来檢測何時將USB設備从計算機上插入和拔出:

    class USBControl : IDisposable
        {
            // used for monitoring plugging and unplugging of USB devices.
            private ManagementEventWatcher watcherAttach;
            private ManagementEventWatcher watcherRemove;
            public USBControl()
            {
                // Add USB plugged event watching
                watcherAttach = new ManagementEventWatcher();
                //var queryAttach = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2");
                watcherAttach.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
                watcherAttach.Query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2");
                watcherAttach.Start();
                // Add USB unplugged event watching
                watcherRemove = new ManagementEventWatcher();
                //var queryRemove = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 3");
                watcherRemove.EventArrived += new EventArrivedEventHandler(watcher_EventRemoved);
                watcherRemove.Query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 3");
                watcherRemove.Start();
            }
            /// <summary>
            /// Used to dispose of the USB device watchers when the USBControl class is disposed of.
            /// </summary>
            public void Dispose()
            {
                watcherAttach.Stop();
                watcherRemove.Stop();
                //Thread.Sleep(1000);
                watcherAttach.Dispose();
                watcherRemove.Dispose();
                //Thread.Sleep(1000);
            }
            void watcher_EventArrived(object sender, EventArrivedEventArgs e)
            {
                Debug.WriteLine("watcher_EventArrived");
            }
            void watcher_EventRemoved(object sender, EventArrivedEventArgs e)
            {
                Debug.WriteLine("watcher_EventRemoved");
            }
            ~USBControl()
            {
                this.Dispose();
            }
    
        }
    

    您必须確保在關闭應用程式時呼叫Dispose()方法.否則,關闭時在執行時会收到COM物件錯誤。

  • 5月前
    4 #

    .us的#usblib USB庫.NET

  • 5月前
    5 #

    我建議使用LibUSBDotNet,這是我一直在使用的庫 2年。 如果您必须使用USB設備(發送請求,處理响應),則该庫是我能找到的最佳解決方案。

    Pros:

      Has all methods you need to work in synch or asynch mode.

      Source code provided

      Enough samples to start using it straight away.

    Cons:

      Poor documentation (it's common problem for open source projects). Basically, you can find just common description of methods in the CHM help file and that's it. But I still find provided samples and source code is enough for coding. Just sometimes I see a strange behaviour and want to know why it was implemented in this way and can't get even a hint...

      Seems unsupported any more. Last version was issued in Oct 2010. And it's hard to get answers sometimes.

  • iphone:iOS應用程式Display Recorder如何在不使用私有API的情况下記錄螢幕?
  • 如何在應用程式中获取Android手機的UUID?