• 获取串口映射的COM端口号


    背景:近期由于项目需要,需要操作短信猫,当短信猫插入电脑后,会根据当前PC状况,映射COM口,这里需动态获取短信猫映射的COM端口号。

    注:用C#实现


    具体代码如下

     1         public enum HardwareEnum
     2         {
     3             Win32_PnPEntity // 所有设备
     4         }
     5 
     6         /// <summary>
     7         /// 获取相应COM口号
     8         /// </summary>
     9         private static string getComInfo(HardwareEnum hardType, string propkey)
    10         {
    11             List<string> deviceslist = new List<string>();
    12             StringBuilder comsb = new StringBuilder();
    13 
    14             try
    15             {
    16                 using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select *from " + hardType))
    17                 {
    18                     var hardInfos = searcher.Get();
    19                     foreach (var hardInfo in hardInfos)
    20                     {
    21 
                      Object deviceprop=hardInfo.Properties[propkey].Value;
                      if (deviceprop!=null&&deviceprop.ToString().Contains("COM"))  //避免出现空设备值异常
    23                         {
    24                             deviceslist.Add(hardInfo.Properties[propkey].Value.ToString());
    25                         }
    26                     }
    27                     searcher.Dispose();
    28                 }
    29                 string[] devicestemps = deviceslist.ToArray();
    30 
    31                 foreach (string device in devicestemps)
    32                 {
    33                     if (device.Contains("AT")) //这里短信猫需操作AT口
    34                     {
    35                         int index = device.IndexOf("(");
    36                         string devicetemp = device.Substring(index + 4); //得到形如"3)"形式
    37                         string comnum = devicetemp.Substring(0, devicetemp.Length - 1); //直接得到相应COM端口号
    38                         comsb.Append(comnum + "*");  //用“*”号隔开,当出现多个的时候
    39                     }
    40                 }
    41 
    42                 string comsbstring=comsb.ToString();
    43                 return comsbstring.Substring(0, comsbstring.Length - 1); //移除最后一个“*”号
    44 
    45             }
    46             catch
    47             {
    48                 return null;
    49             }
    50             finally
    51             {
    52                 deviceslist = null;
    53             }
    54 
    55         }
    56 
    57         /// <summary>
    58         /// 获取COM端口号
    59         /// </summary>
    60         public static string getComNum()
    61         {
    62             string comnums = getComInfo(HardwareEnum.Win32_PnPEntity, "name");
    63 
    64             return comnums; //返回多个COM号的组成,可在这里进行解析,也可在调用时进行解析,这里不做过多赘述
    65         }

    注:1.通过该种方式可以获取实际你所需要操作的COM端口号。

      2.这里通过“*”对COM端口号进行拼接,可以通过解析返回值,判断当前时候连接了多个短信猫,以便做下一步操作。


    by Shawn Chen,2017.6.26日,晚。

  • 相关阅读:
    Beta版本冲刺第二天
    项目冲刺-第十天
    项目冲刺-第五天
    用例图练习
    软件案例分析(微软必应词典客户端)
    第二次作业——结对项目之需求分析与原型模型设计
    git for windows 入门随笔
    软件工程的实践项目的自我目标
    Android的开发环境的发展演变
    我的软件工程实践的总结
  • 原文地址:https://www.cnblogs.com/developer_chan/p/7082970.html
Copyright © 2020-2023  润新知