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

unity工具人

暂无认证

  • 4浏览

    0关注

    205博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

unity工具人 发布时间:2020-08-21 17:08:49 ,浏览量:4

代码:

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

关注
打赏
1656671177
查看更多评论
立即登录/注册

微信扫码登录

0.0358s