当前位置 博文首页 > unity工具人的博客:C# 获取本机连接的所有 串口设备名称 与 串

    unity工具人的博客:C# 获取本机连接的所有 串口设备名称 与 串

    作者:[db:作者] 时间:2021-07-20 12:35

    代码:

    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

    cs
    下一篇:没有了