无法在C#代码中找到GSM调制解调器

时间:2013-08-16 15:11:27

标签: c# .net visual-studio-2010 serial-port

我正在尝试使用以下代码在C#中找到GSM调制解调器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;

namespace LibUsbDotNet_Test1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] ports = SerialPort.GetPortNames();

            //display ports
            for (int i = 0; i < ports.Length; i++)
            {
                Console.WriteLine(ports[i]);
            }


            //Find Modem
            for (int i = 0; i < ports.Length; i++)
            {
                SerialPort p = new SerialPort(ports[i].Trim());

                if (!p.IsOpen)
                {
                    p.Open();
                }


                /**************nothing below this line get displayed***************************************/

                if (CheckExistingModemOnComPort(p))
                {
                    Console.WriteLine("Modem Found: " + p.PortName);
                }
                else
                {
                    Console.WriteLine("No Modem Found: ");
                }

                p.Close();
            }

            Console.ReadLine();
        }



        private static bool CheckExistingModemOnComPort(SerialPort serialPort)
        {
            if ((serialPort == null) || !serialPort.IsOpen)
                return false;

            // Commands for modem checking
            string[] modemCommands = new string[] { "AT",       // Check connected modem. After 'AT' command some modems autobaud their speed.
                                            "ATQ0" };   // Switch on confirmations
            serialPort.DtrEnable = true;    // Set Data Terminal Ready (DTR) signal 
            serialPort.RtsEnable = true;    // Set Request to Send (RTS) signal

            string answer = "";
            bool retOk = false;
            for (int rtsInd = 0; rtsInd < 2; rtsInd++)
            {
                foreach (string command in modemCommands)
                {
                    serialPort.Write(command + serialPort.NewLine);
                    retOk = false;
                    answer = "";
                    int timeout = (command == "AT") ? 10 : 20;

                    // Waiting for response 1-2 sec
                    for (int i = 0; i < timeout; i++)
                    {
                        Thread.Sleep(100);
                        answer += serialPort.ReadExisting();
                        if (answer.IndexOf("OK") >= 0)
                        {
                            retOk = true;
                            break;
                        }
                    }
                }
                // If got responses, we found a modem
                if (retOk)
                    return true;

                // Trying to execute the commands without RTS
                serialPort.RtsEnable = false;
            }
            return false;
        }
    }


}

方法CheckExistingModemOnComPort由另一个Stack Overflow成员here编写。

但是,这行代码似乎不适用于行

之后
if (!p.IsOpen)
 {
       p.Open();
 }

此部分在代码中进行了注释,因此您可以轻松识别位置。这有什么不对?我也在关闭打开的端口。

修改

这是我得到的输出

COM4
COM3
COM5
COM13
COM12
COM11

当此代码进入

时,这是调试窗口输出
if (!p.IsOpen)
{
         p.Open();
}

enter image description here

我的GSM调制解调器(加密狗)位于COM13

更新

我使用try..catch()块来处理端口打开,但现在我收到消息'No Modem Found:',即使调制解调器的com端口实际上存在于数组中。

0 个答案:

没有答案