0%

C# 用VID和PID自動連線serialport

原本C#的serial port function官方範例,是類似這樣的

1
streamport = new SerialPort("COM3", bauadrate);

這種方法每次開啟都還要再去跟改port相當的麻煩,所以就找了別人針對windows寫的function,再返回靜態的port string給SerialPort,效果相當好。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 有錯誤可能是你沒有 using Microsoft.Win32;或其他lib
public static List<string> ComPortNames(String VID, String PID)
{
// 讀取PID和VID
String pattern = String.Format("^VID_{0}.PID_{1}", VID, PID);
Regex _rx = new Regex(pattern, RegexOptions.IgnoreCase);
List<string> comports = new List<string>();
RegistryKey rk1 = Registry.LocalMachine;
RegistryKey rk2 = rk1.OpenSubKey("SYSTEM\\CurrentControlSet\\Enum");
foreach (String s3 in rk2.GetSubKeyNames())
{
RegistryKey rk3 = rk2.OpenSubKey(s3);
foreach (String s in rk3.GetSubKeyNames())
{
if (_rx.Match(s).Success)
{
RegistryKey rk4 = rk3.OpenSubKey(s);
foreach (String s2 in rk4.GetSubKeyNames())
{
RegistryKey rk5 = rk4.OpenSubKey(s2);
RegistryKey rk6 = rk5.OpenSubKey("Device Parameters");
comports.Add((string)rk6.GetValue("PortName"));
// 獲取port name,如有多個相同設備,加入comports array
}
}
}
}
return comports;
}

完整內容可參照下方連結😁
code來源

你的支持是我繼續創作的動力!