2012-08-14 42 views
1

以下代碼僅返回三個串行端口(com3,com4和com5)。我想訪問的固件位於USB插頭倍增器上。如何訪問此多路複用器的串行端口,以及如何識別包含我想要發送信息的固件的特定USB?如何找到特定的串行端口?

using System; 
using System.IO.Ports; 

namespace SerialPortExample 
{ 
    class SerialPortExample 
    { 
     public static void Main() 
     { 
      string[] ports = SerialPort.GetPortNames(); 
      Console.WriteLine("The following serial ports were found:"); 
      foreach (string port in ports) 
      { 
       Console.WriteLine(port); 
      } 
      Console.ReadLine(); 
     } 
    } 
} 

非常感謝提前!

+0

這是一個USB驅動器?像H:或者類似的東西? – 2012-08-14 12:53:49

+0

我認爲你需要軟件來先創建USB虛擬COM端口。 – Reniuz 2012-08-14 12:53:50

+0

@DorCohen我怎麼看? – 2012-08-14 12:55:05

回答

2

這是一個相當大的可用性問題,並採取快捷方式和模擬串行端口,可以很容易與他們對接的USB驅動程序引起的。串行端口是非常原始的設備,這使得它們的api非常易於使用。但缺乏對即插即用的任何支持,無法爲他們獲得體面的通知。驅動程序只是選擇一個任意的端口號,由用戶決定它可能是哪一個。試錯的東西。這並沒有成爲一個問題,串口在機器後面板上安裝了一個連接器,該連接器上清楚地標有COM端口名稱。

你所能得到一些里程從WMI的,它可以讓你列舉串口設備與Win32_SerialPort query。你得到的是相當不可預測的,完全取決於驅動程序提供的數據。最好的方法是使用WMI Code Creator實用程序進行試驗,它也可以自動生成您需要的C#代碼。不幸的是,我無法再找到下載位置,這似乎在過去幾周內已被刪除。希望你能找到替代品。

+0

非常感謝你,你有沒有見過這個[ driveinfo linq](http://stackoverflow.com/questions/6003822/how-to-detect-a-usb-device-has-been-plugged-in)替代? – 2012-08-14 13:20:56

+1

仿真可移動驅動器的USB設備與串口仿真器非常不同。驅動器*會*生成即插即用事件。 – 2012-08-14 13:35:45

+0

謝謝手,我猜想使用driveinfo.getdrives()方法可能是最好的嗎?我開始了一個新的[討論](http://stackoverflow.com/questions/11953364/why-is-the-command-prompt-disappearing-after-loading#11953592),以及我在新解決方案中的新奮鬥。 – 2012-08-14 14:09:33

0

下面的代碼做得很好,找到特定的端口:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Management; 
using System.Windows.Forms; 
namespace MyNamespace 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      MyClass x = new MyClass(); 
      var com = x.GetCOMs(); 
      foreach (string port in com) 
      { 
       Console.WriteLine(port); 
      } 
      Console.ReadLine(); 
     } 

    } 

    class MyClass 
    { 
     public List<string> GetCOMs() 
     { 
      List<string> coms = new List<string>(); 
      try 
      { 
       ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", 
       "SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0"); 

       foreach (ManagementObject obj in searcher.Get()) 
       { 
        object captionObj = obj["Caption"]; 
        if (captionObj != null) 
        { 
         string caption = captionObj.ToString(); 
         if (caption.Contains("(COM")) 
         { 
          coms.Add(caption); 
         } 
        } 
       } 

       m_ParseCOMs(ref coms); 
      } 
      catch (ManagementException ex) 
      { 
       MessageBox.Show("An error occurred while querying for WMI data: " + ex.Message); 
       return coms; 
      } 

      return coms; 
     } 

     private void m_ParseCOMs(ref List<string> comPorts) 
     { 
      string[] temp; 
      List<string> temp2 = new List<string>(); 
      int index = 0; 
      foreach (string s in comPorts) 
      { 
       string temp3 = ""; 
       temp = s.Split(' '); 
       temp3 += temp[temp.Length - 1] + " - "; 
       for (int i = 0; i < temp.Length - 1; i++) 
       { 
        temp3 += temp[i] + " "; 
       } 
       temp2.Insert(index, temp3); 
       index++; 
      } 
      comPorts = temp2; 
     } 
    } 
} 
相關問題