C# 获取本机连接的所有 串口设备名称 与 串口号
代码:
class Program
{
static void Main(string[] args)
{
GetComList();
}
private static void GetComList()
{try
{
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_PnPEntity"))
{
Console.WriteLine("本机串口:");
var hardInfos = searcher.Get();
int index = 1;
foreach (var hardInfo in hardInfos)
{
if (hardInfo.Properties["Name"].Value != null && hardInfo.Properties["Name"].Value.ToString().Contains("(COM"))
{
String strComName = hardInfo.Properties["Name"].Value.ToString();
Console.WriteLine(index + ":" + strComName);//打印串口设备名称及串口号
index += 1;
}
}
}
Console.ReadKey();
}
catch
{
}
}
}
效果:
鸣谢
转自:https://www.cnblogs.com/yilinyangyu/p/8405705.html
其他学习资料:
串口通信:https://blog.csdn.net/weixin_45023328/article/details/108128883
串口通信数据格式转换:https://blog.csdn.net/weixin_45023328/article/details/108119294