首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用C#与WWAN调制解调器上的串行端口通信

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

Stack Overflow用户
提问于 2016-08-24 04:36:13
回答 2查看 1.4K关注 0票数 1

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

代码语言:javascript
复制
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#找到调制解调器的串行端口?

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

-Luke

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-26 04:08:45

所以,我想出了如何解决我的问题。谢谢你Marc的回答,但这对我来说是行不通的。

我最终遵循了这篇堆栈溢出帖子中包含的说明:List all System Modems

以下是工作的代码:

代码语言:javascript
复制
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命令。;)

谢谢你的帮助,卢克

票数 1
EN

Stack Overflow用户

发布于 2016-08-24 05:21:53

卢克

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

代码语言:javascript
复制
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命令可以发送到该端口。希望这能有所帮助。

问候你,马克

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39110229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档