获取串口信息
我有一些将串行端口加载到组合框中的代码:
List<String> tList = new List<String>();
comboBoxComPort.Items.Clear();
foreach (string s in SerialPort.GetPortNames())
{
tList.Add(s);
}
tList.Sort();
comboBoxComPort.Items.Add("Select COM port...");
comboBoxComPort.Items.AddRange(tList.ToArray());
comboBoxComPort.SelectedIndex = 0;
我想将端口描述(类似于设备管理器中 COM 端口显示的内容)添加到列表 中,并对列表中的项目进行排序列出索引 0 之后的内容(已解决:参见上面的代码片段)。有人对添加端口描述有什么建议吗?我正在使用 Microsoft Visual C# 2008 Express Edition (.NET 2.0)。如果您有任何想法,我们将不胜感激。谢谢。
I have some code that loads the serial ports into a combo-box:
List<String> tList = new List<String>();
comboBoxComPort.Items.Clear();
foreach (string s in SerialPort.GetPortNames())
{
tList.Add(s);
}
tList.Sort();
comboBoxComPort.Items.Add("Select COM port...");
comboBoxComPort.Items.AddRange(tList.ToArray());
comboBoxComPort.SelectedIndex = 0;
I would like to add the port descriptions (similar to what are shown for the COM ports in the Device Manager) to the list and sort the items in the list that are after index 0 (solved: see above snippet). Does anyone have any suggestions for adding the port descriptions? I am using Microsoft Visual C# 2008 Express Edition (.NET 2.0). Any thoughts you may have would be appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我在这里尝试了很多解决方案,但这些解决方案对我不起作用,只显示了一些端口。但下面显示了他们所有人以及他们的信息。
I tried so many solutions on here that didn't work for me, only displaying some of the ports. But the following displayed All of them and their information.
编辑:抱歉,我太快跳过了你的问题。我现在意识到您正在寻找包含端口名称+端口描述的列表。我已经相应地更新了代码...
使用 System.Management,您可以查询所有端口以及每个端口的所有信息(就像设备管理器...)
示例代码(确保添加对 System.管理):
更多信息请参见:http://msdn. microsoft.com/en-us/library/aa394582%28VS.85%29.aspx
EDIT: Sorry, I zipped past your question too quick. I realize now that you're looking for a list with the port name + port description. I've updated the code accordingly...
Using System.Management, you can query for all the ports, and all the information for each port (just like Device Manager...)
Sample code (make sure to add reference to System.Management):
More info here: http://msdn.microsoft.com/en-us/library/aa394582%28VS.85%29.aspx
使用以下代码片段
执行时会给出以下输出。
不要忘记添加
还添加对
system.Management
的引用(默认情况下不可用)C#
VB
更新:
您还可以检查
而不是
Use following code snippet
It gives following output when executed.
Don't forget to add
Also add reference to
system.Management
(by default it is not available)C#
VB
Update:
You may also check for
instead of
这里的答案都不能满足我的需求。
Muno 的答案是错误,因为它只列出了 USB 端口。
code4life 的答案是错误,因为它列出了除 USB 端口之外的所有端口。 (尽管如此,它有 44 个赞成票!!!)
我的计算机上有一个 EPSON 打印机模拟端口,此处的任何答案均未列出该端口。所以我必须编写自己的解决方案。此外,我想显示更多信息,而不仅仅是标题字符串。我还需要将端口名称与描述分开。
我的代码已在 Windows XP、7、10 和 11 上进行了测试。
端口名称(如“COM1”)必须从注册表读取,因为 WMI 不会为所有 COM 端口提供此信息 (EPSON )。
如果您使用我的代码,您不再需要
SerialPort.GetPortNames()
。我的函数返回相同的端口,但有更多详细信息。为什么微软没有在框架中实现这样的功能?我用很多 COM 端口测试了代码。这是控制台输出:
COM1 是主板上的 COM 端口。
COM 8 和 9 是蓝牙 COM 端口。
COM 25 和 26 是 USB 转 RS232 适配器。
COM 28、29 和 30 是类似 Arduino 的板。
COM 20 和 999 是 EPSON 端口。
注意:如果您在打开蓝牙 COM 端口时遇到
UnauthorizedAccessException
,解决方案可能是删除设备并再次进行配对。None of the answers here satisfies my needs.
The answer from Muno is wrong because it lists ONLY the USB ports.
The answer from code4life is wrong because it lists all EXCEPT the USB ports. (Nevertheless it has 44 up-votes!!!)
I have an EPSON printer simulation port on my computer which is not listed by any of the answers here. So I had to write my own solution. Additionally I want to display more information than just the caption string. I also need to separate the port name from the description.
My code has been tested on Windows XP, 7, 10 and 11.
The Port Name (like "COM1") must be read from the registry because WMI does not give this information for all COM ports (EPSON).
If you use my code you do not need
SerialPort.GetPortNames()
anymore. My function returns the same ports, but with additional details. Why did Microsoft not implement such a function into the framework??I tested the code with a lot of COM ports. This is the Console output:
COM1 is a COM port on the mainboard.
COM 8 and 9 are Buetooth COM ports.
COM 25 and 26 are USB to RS232 adapters.
COM 28 and 29 and 30 are Arduino-like boards.
COM 20 and 999 are EPSON ports.
NOTE: If you get an
UnauthorizedAccessException
when opening a Bluetooth COM port the solution may be remove the device and do the pairing again.有一个 关于同一问题的帖子MSDN:
那篇文章中的链接指向此:
There is a post about this same issue on MSDN:
The link in that post goes to this one:
我结合了以前的答案并使用了 Win32_PnPEntity 类的结构,可以找到 在这里。
得到这样的解决方案:
SerialPortInfo 类:
I combined previous answers and used structure of Win32_PnPEntity class which can be found found here.
Got solution like this:
SerialPortInfo class:
我不太清楚“对索引 0 之后的项目进行排序”是什么意思,但如果您只想对
SerialPort.GetPortNames()
返回的字符串数组进行排序,则可以使用 数组排序。I'm not quite sure what you mean by "sorting the items after index 0", but if you just want to sort the array of strings returned by
SerialPort.GetPortNames()
, you can use Array.Sort.我重写了 Elmue 的答案并使用 Muno 的
SerialPortInfo
类来丰富它。所以它更清晰并提供更多信息。 (.net 6)在
Program.cs
中测试它:I rewrite Elmue's answer and enrich it using Muno's
SerialPortInfo
class. So it is more clear and provides more information. (.net 6)Test it in
Program.cs
:我的一个小变化是,并非所有端口都有描述。
A small change from me, not all ports had descriptions.