Serial Port

This is written C and I am trying to implement it in C++. This will identify the comport by device ID.

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62



List<string> ComPortNames(String VID, String PID)
{


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())
{
tbxDateTime.Text = ("1");
RegistryKey rk3 = rk2.OpenSubKey(s3);
foreach (String s in rk3.GetSubKeyNames())
{
if (_rx.Match(s).Success)
{
tbxDateTime.Text = ("2");
RegistryKey rk4 = rk3.OpenSubKey(s);
foreach (String s2 in rk4.GetSubKeyNames())
{
tbxDateTime.Text = ("3");
RegistryKey rk5 = rk4.OpenSubKey(s2);
RegistryKey rk6 = rk5.OpenSubKey("Device Parameters");
comports.Add((string)rk6.GetValue("PortName"));
}
}
}
}
//tbxDateTime.Text = ("out");
return comports;
}







List<string> names = ComPortNames("2008", "1001");
if (names.Count > 0)
{
foreach (String s in SerialPort.GetPortNames())
{

if (names.Contains(s))
// Console.WriteLine("My TransCORE port is " + s + "and is" + m);
tbxOpenStat.Text = ("My TransCORE port is " + s);

}
}
else

//Console.WriteLine("No COM ports found");

tbxOpenStat.Text = ("No COM ports found");


I'm a little confused at what you've written. I'm not sure whether you're saying this is the C code or the C++ code. It's not C++ because there's many aspects that aren't in C like templates and the string container type. Also, this code from what I can tell is more specifically C++/CLI which is Microsoft's implementation of the .NET framework in the C++ language. Just thought I'd mention it.

You need to be more specific in what you're problem is and what you need help with. It's not entirely clear from what you've written.
Yes you are correct; this code is written in C++/CLI. Thank you for pointing this out. I am looking for something similar that I can implement in C++. I am trying to identify a serial port used by a specific device from it's PID, VID.
You can find some serial port classes and libraries on the internet. It depends on whether you're wanting cross-platform code or Windows code. There are many libraries on the internet that can achieve both these. Or another option is to call Win32 functions directly which you can find many tutorials on the internet on how to do it.

Hope this helps. I feel that I'm not being much help at the moment.
Topic archived. No new replies allowed.