1 /************************************************************************************\
2  * CatLog - An SDSG Production                                                     *
3 \************************************************************************************/
4 
5 /*! \file		SelectPortDialog.cs
6  *  \author		Andrew Shurney
7  *  \brief		Presents a dialog to choose from available ports.
8  */
9 
10 using System;
11 using System.Collections.Generic;
12 using System.ComponentModel;
13 using System.Data;
14 using System.Drawing;
15 using System.Linq;
16 using System.Text;
17 using System.Windows.Forms;
18 using System.IO.Ports;
19 using System.Management;
20 using Microsoft.Win32;
21 
22 namespace CatLog
23 {
24     public partial class SelectPortDialog : Form
25     {
26         public class SelectPortItem
27         {
28             public String Text { get; set; }
29             public String Value { get; set; }
30 
ToString()31             public override string ToString()
32             {
33                 return Text;
34             }
35         }
36 
SelectPortDialog(String portName)37         public SelectPortDialog(String portName)
38         {
39             InitializeComponent();
40 
41             Dictionary<String, String> ports = BuildPortNameHash(SerialPort.GetPortNames());
42 
43             foreach (KeyValuePair<String, String> port in ports)
44             {
45                 SelectPortItem item = new SelectPortItem();
46                 item.Text = port.Value;
47                 item.Value = port.Key;
48 
49                 cmbPorts.Items.Add(item);
50 
51                 if(port.Key == portName)
52                     cmbPorts.SelectedItem = item;
53             }
54         }
55 
BuildPortNameHash(String[] portsToMap)56         public static Dictionary<String, String> BuildPortNameHash(String[] portsToMap)
57         {
58             Dictionary<String, String> oReturnTable = new Dictionary<String, String>();
59             MineRegistryForPortName("SYSTEM\\CurrentControlSet\\Enum", oReturnTable, portsToMap);
60             return oReturnTable;
61         }
62 
MineRegistryForPortName(String startKeyPath, Dictionary<String, String> targetMap, String[] portsToMap)63         static void MineRegistryForPortName(String startKeyPath, Dictionary<String, String> targetMap, String[] portsToMap)
64         {
65             if (targetMap.Count >= portsToMap.Length)
66                 return;
67             using (RegistryKey currentKey = Registry.LocalMachine)
68             {
69                 try
70                 {
71                     using (RegistryKey currentSubKey = currentKey.OpenSubKey(startKeyPath))
72                     {
73                         String[] currentSubkeys = currentSubKey.GetSubKeyNames();
74                         if (currentSubkeys.Contains("Device Parameters") && startKeyPath != "SYSTEM\\CurrentControlSet\\Enum")
75                         {
76                             Object portName = Registry.GetValue("HKEY_LOCAL_MACHINE\\" + startKeyPath + "\\Device Parameters", "PortName", null);
77 
78                             if (portName == null || portsToMap.Contains(portName.ToString()) == false)
79                                 return;
80 
81                             Object friendlyPortName = Registry.GetValue("HKEY_LOCAL_MACHINE\\" + startKeyPath, "FriendlyName", null);
82                             String friendlyName = "N/A";
83                             if (friendlyPortName != null)
84                                 friendlyName = friendlyPortName.ToString();
85 
86                             if (friendlyName.Contains(portName.ToString()) == false)
87                                 friendlyName = String.Format("{0} ({1})", friendlyName, portName);
88 
89                             targetMap[portName.ToString()] = friendlyName;
90                         }
91                         else
92                         {
93                             foreach (String strSubKey in currentSubkeys)
94                                 MineRegistryForPortName(startKeyPath + "\\" + strSubKey, targetMap, portsToMap);
95                         }
96                     }
97                 }
98                 catch (Exception)
99                 {
100                     Console.WriteLine("Error accessing key '{0}'.. Skipping..", startKeyPath);
101                 }
102             }
103         }
104 
105         public String Port
106         {
107             get
108             {
109                 SelectPortItem item = cmbPorts.SelectedItem as SelectPortItem;
110 
111                 return item.Value;
112             }
113         }
114 
115         public String PortDesc
116         {
117             get
118             {
119                 SelectPortItem item = cmbPorts.SelectedItem as SelectPortItem;
120                 return item.Text;
121             }
122         }
123     }
124 }
125