如何使用 C# 与 WWAN 调制解调器上的串行端口通信

How to Communicate With Serial Port on a WWAN Modem Using C#

我用 C# 编写了一个程序,允许我与计算机内部的串行端口通信(使用 AT 命令)。 查找端口的代码如下所示:

ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\CIMV2",
                "SELECT * FROM Win32_PnPEntity");

            //This loops through the results from the searcher
            foreach (ManagementObject queryObj in searcher.Get())
            {
                //If it finds the port, 
                if (queryObj["Caption"].ToString().Contains("##### Wireless AT"))
                {
                    //it writes it to the file
                    sw.WriteLine("serial port : {0}", queryObj["Caption"] + "\n");
                    sw.Flush();
                }

这段代码与我们的旧调制解调器配合得很好,它搜索 COM 端口并找到 AT 无线命令端口。这是我最终将 AT 命令发送到的端口。下面是我搜索的端口的设备管理器的两张图片

问题是,我们正在使用更新的调制解调器推出我们的计算机,这些工作方式不同...

新调制解调器不使用设备管理器中物理端口列表的串行端口。此外,串行端口未显示在 Win32_PnpEntity 搜索中...串行端口列在调制解调器属性下。

我的问题是,如何使用 C# 找到调制解调器的串口?

如果有什么方法可以详细说明,请告诉我。

-卢克

卢克,

您必须更改查询管理树的方式。

Using System.IO.Ports;
Using System.Managment;
Using System;
private void SetPort()
    {
        string[] allPorts = SerialPort.GetPortNames();
        bool found = false;
        SerialPort port;

        for (int i = 0; i < allPorts.Length; i++)
        {
            var searcher = new ManagementObjectSearcher("root\WMI", "SELECT * FROM MSSerial_PortName");
            foreach (ManagementObject queryObj in searcher.Get())
            {
                string instanceName = queryObj["InstanceName"].ToString();

                if (instanceName.IndexOf("Your Modem String", StringComparison.Ordinal) > -1)
                {
                    string portName = queryObj["PortName"].ToString();
                    port = new SerialPort(portName, 9600, Parity.None, 8, StopBits.One);
                    found = true;
                    break;
                }
            }

            if (found) break;
        }
    }

在循环中,代码创建了一个串行端口,您的 AT 命令可以发送到该端口。 希望这有帮助。

此致, 马克

所以,我想出了解决问题的方法。谢谢 Marc 的回答,但它对我来说并不奏效。

我最终遵循了 Stack Overflow post 中包含的说明:List all System Modems

这是有效的代码:

using System.IO.Ports;
using System.Management;
using System;
using System.Collections.Generic;
using System.Linq;
using ROOT.CIMV2.Win32;

namespace Modem
{
    class Program
    {

    public static string portName;
    public static SerialPort _serialPort;

    static void Main(string[] args)
    {

        foreach (POTSModem modem in POTSModem.GetInstances())
        {
            if (modem.Description.Contains("WWAN"))
            {
                portName = modem.AttachedTo;

                SetPort();

                break;
            }
        }

     }


 public static void SetPort()
    {
        //Assign the port to a COM address and a Baud rate
        _serialPort = new SerialPort(portName, 9600);

        //Open the connection
        _serialPort.Open();

        // Makes sure serial port is open before trying to write
        try
        {
            //If the port is not open
            if (!(_serialPort.IsOpen))
                //Open it
                _serialPort.Open();

        }

        catch
        {
            Console.WriteLine("ERROR! Couldn't open serial port...");
            Console.ReadKey();
        }

        try
        {
            //Here it executes a command on the modem
            _serialPort.Write("ATI\r");


            //Retrieves the output, setting the value to a string
            string indata = _serialPort.ReadExisting();

            Console.WriteLine(indata);
        }

        catch
        {
            Console.WriteLine("ERROR GETTING INFO!!!");
            Console.ReadKey();
        }

     }
  }
}

它就像一个魅力!现在我只需要弄清楚我将不得不与新调制解调器一起使用的所有新 AT 命令。 ;)

感谢您的帮助, 卢克