2017-02-10 72 views
-1

我已將客戶顯示器與POS應用程序連接起來。我正在使用串行端口類在客戶顯示器上顯示消息。要發送消息,我需要使用com端口瞭解此USB顯示器正在使用。我搜索允許有很多例子,讓所有的comump壺,但我能夠找到任何幫助,以獲得微小的COM端口號,例如COM93,COM01或COM2。確定(啓用USB)消費者顯示器的COM端口

我試過下面的程序,我也試過微軟程序的WMI代碼的創建者。

//獲取串口名稱列表。 string [] ports = SerialPort.GetPortNames();

Console.WriteLine("The following serial ports were found:"); 

    // Display each port name to the console. 
    foreach (string port in ports) 
    { 
     Console.WriteLine(port); 
    } 

Console.ReadLine(); 





     SerialPort sp = new SerialPort(); 
     sp.PortName = "COM93";------------- How i can find this no automatic 
     sp.BaudRate = 9600; 
     sp.Parity = Parity.None; 
     sp.DataBits = 8; 
     sp.StopBits = StopBits.One; 
     sp.Open(); 
     sp.Write("\f"); 
     sp.WriteLine("***Velkommen***"); 
     sp.Close(); 
     sp.Dispose(); 
     sp = null; 

我曾嘗試以下解決方案爲好,用這個也爲了這個,我得到Access denied,所以我需要編輯registory給我點瓦納做我想做的解決方案編程

ManagementObjectSearcher搜索權= new ManagementObjectSearcher(「root \ WMI」,「SELECT * FROM MSSerial_PortName」);

回答

0
public string Get_RegistryInfo(string VID, string PID) 
     { 
      try 
      { 
       RegistryKey rk1 = Registry.LocalMachine; 
       // HKEY_LOCAL_MACHINE 
       RegistryKey rk2 = rk1.OpenSubKey("HARDWARE\\\\DEVICEMAP\\\\SERIALCOMM"); 
       // HKEY_LOCAL_MACHINE\HARDWARE\\\\DEVICEMAP\\\\SERIALCOMM 
       VendorID = VID; 
       ProduktID = PID; 
       string pattern = string.Format("^VID_{0}.PID_{1}", VID, PID); 
       Regex _rx = new Regex(pattern, RegexOptions.IgnoreCase); 
       string rk2_SubKeyNames = null; 
       foreach (string rk2_SubKeyNames_loopVariable in rk2.GetValueNames()) 
       { 
        rk2_SubKeyNames = rk2_SubKeyNames_loopVariable; 
        if (rk2_SubKeyNames == "\\Device\\ProlificSerial0") 
        { 
         COM_Port = rk2.GetValue(rk2_SubKeyNames).ToString();       
        } 
       } 
       return COM_Port; 
      } 
      catch(Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
       return COM_Port; 
      } 

     }