在c#中找到gsm调制解调器端口

时间:2011-08-21 14:59:14

标签: c# port

我想遍历可用的端口:     System.IO.Ports.SerialPort.GetPortNames() 查找gsm调制解调器是否使用了某个端口。 请问。

2 个答案:

答案 0 :(得分:6)

我在申请中做了类似的任务:

  1. 要检查调制解调器是否连接到特定端口,您可以将AT命令发送到此端口。 如果我们在当前COM端口上找到调制解调器,则此函数返回true:

    private 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;
    }
    
  2. 在下一阶段,我们可以从调制解调器收集一些数据。 我使用了以下命令:

    • ATQ0 - 启用确认(每次请求都收到确认)
    • ATE0 - 启用echo
    • ATI - 获取调制解调器详细信息
    • ATI3 - 获取扩展调制解调器详细信息(并非所有调制解调器都支持此命令)

答案 1 :(得分:0)

                // Check each Availble COM port
                foreach (string l_sport in l_available_ports)
                {
                    GlobalVars.g_serialport = GlobalFunc.OpenPort(l_sport, Convert.ToInt32(this.cboBaudRate.Text), Convert.ToInt32(this.cboDataBits.Text), Convert.ToInt32(this.txtReadTimeOut.Text), Convert.ToInt32(this.txtWriteTimeOut.Text));
                    if (GlobalVars.g_serialport.IsOpen)
                    {
                        GlobalVars.g_serialport.WriteLine("AT\r");
                        Thread.Sleep(500);
                        string l_response = GlobalVars.g_serialport.ReadExisting();
                        if (l_response.IndexOf("OK") >= 0)
                        {
                            GlobalVars.g_serialport.WriteLine("AT+CMGF=1\r");
                            Thread.Sleep(500);
                            string l_response1 = GlobalVars.g_serialport.ReadExisting();
                            if (l_response1.IndexOf("OK") >= 0)
                            {
                                GlobalVars.g_PhoneNo = txt_PhNum.Text;
                                MessageBox.Show("Connected Successfully", "Connection", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                lblConnectionStatus.Text = "Phone Connected Successfully.";
                                btnOK.Enabled = false;
                                btnDisconnect.Enabled = true;

                                GlobalVars.g_serialport.WriteLine("AT+CGSN\r");
                                Thread.Sleep(500);
                                string l_imei = GlobalVars.g_serialport.ReadExisting();
                                Console.WriteLine("Modem IMEI:" + l_imei);
                                if (l_imei.IndexOf("OK", 1) > 0)
                                {
                                    l_imei = l_imei.Replace("AT+CGSN\r\r\n", null);
                                    l_imei = l_imei.Replace("\r\n\r\nOK\r\n", null);
                                    lbl_ModemIMEI.Text = l_imei;
                                }
                                else
                                {
                                    lblConnectionStatus.Text = "Phone Connected Successfully. Error reading IMEI.";
                                }
                                EnableSMSNotification(GlobalVars.g_serialport);

                                break;
                            }
                            else
                            {
                                Console.WriteLine("No AT+CMGF cmd response");
                            }
                        }
                        else
                        {
                            Console.WriteLine("No AT cmd response");
                        }
                    }
                    else
                    {
                        Console.WriteLine("No Phone At:" + l_sport);
                    }
                }