您当前的位置: 首页 >  c#

C# 获取本机连接的所有 串口设备名称 与 串口号

发布时间:2020-08-21 17:08:49 ,浏览量:5

代码:

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

关注
打赏
1688896170
查看更多评论

暂无认证

  • 5浏览

    0关注

    115984博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.2273s