2011-08-21 30 views
7

我想遍歷可用端口: System.IO.Ports.SerialPort.GetPortNames() 查找gsm調制解調器是否使用端口。 請任何想法。在c#中找到gsm調制解調器端口#

+0

我想你知道如何使用這個設備(我不知道)溝通 - 應該有一些簡單的操作(比如,要求設備版本/序列號)您可以發送到每一個端口找設備 – Carsten

+0

我可以將AT命令發送到未連接到調制解調器的端口嗎? – Dohamsg

+0

當然 - 爲什麼不,你不會看到任何反應,恕我直言,所有 – Carsten

回答

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 - 開關上確認(接收對每個請求OK)
    • ATE0 - 交換機上回聲
    • ATI - 獲得調制解調器細節
    • ATI3 - 獲得擴展調制解調器詳細信息(並非所有調制解調器都支持此命令)
+0

我無法獲得此答案 –

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); 
        } 
       }