2013-07-10 131 views
3

接收短信,我看到類似的主題被張貼在這個論壇上,但我根本不知道如何發送AT命令並接收響應。 (幾個月前我開始用C#編程,我仍然是n00b,但我正在努力學習它......)。通過GSM調制解調器

我需要創建應用程序,它只能通過GSM USB加密狗接收短信。到目前爲止,我設法創建了應用程序,通過可用的COM端口識別並連接調制解調器。現在我需要推動AT命令接收消息並將它們顯示到文本框中。我想知道是否有人可以花幾分鐘的時間向我解釋這個過程,並用註釋修改我的代碼,以便我終於可以學習和理解如何使用serialPort進行通信。我需要知道的是,當發送短信時,該消息是否被GSM調制解調器接收和存儲(並且它被存儲直到我發送一些請求來讀取它們,或者我是否需要發送一些會觸發GSM調制解調器來收集消息的事件來自ISP)?如何把AT命令並接收他們的反應(我只知道這是通過使用的SerialPort對象來完成,但沒有線索怎麼辦呢......)

這是我接受的方法(這我BTW卡... :))

private void receiveMessage() 
{ 
    //commclass is only a class for getting COM port, baud rate and timeout 
    CommClass cc = new CommClass(); 
    cc.setParameters(); 
    serialPort1.PortName = cc.getPort(); 
    serialPort1.BaudRate = cc.getBaud(); 
    serialPort1.ReadTimeout = cc.getTimeout(); 
    serialPort1.Open(); 

    if (!serialPort1.IsOpen) 
    { 
     //MessageBox is written in Croatian language, it is only an alert to check the configuration because port is not opened... 
     MessageBox.Show("Modem nije spojen, molimo provjerite konfiguraciju...!"); 
     //timer1.Stop(); 
    } 
    else 
    { 
     //this.label2.Text = serialPort1.PortName; 
     //this.label2.Visible = true; 
     //this.label3.Visible = true; 
     //this is where I need to place a code for receiving all SMS messages 
     this.serialPort1.Write("AT+CMGL=\"REC UNREAD\""); 
    } 
    serialPort1.Close(); 
} 

如果有人願意幫助,我將不勝感激,如果不是我會通過我自己來處理它(大概花了幾個小時/天,直到我弄明白...)

在這兩種情況下,謝謝你的好意...乾杯。

回答

5

對不起,等待我的回覆,最近有點忙。 簡而言之,這是我從GSM USB加密狗獲取消息的代碼。我希望這將是有用的人......

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Data; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.IO.Ports; 

namespace SMSget 
{ 
public partial class SMSLogPanel : UserControl 
{ 
    SerialPort sp; 
    int datab = 0; 
    bool dtr = false; 
    bool encod; 
    Handshake h; 
    Parity p; 
    int wtimeout = 0; 
    StopBits s; 

    #region default constructor 
    public SMSLogPanel() 
    { 
     InitializeComponent(); 
     this.sp = serialPort1 = new SerialPort(); 
     this.datab = serialPort1.DataBits = 8; 
     this.dtr = serialPort1.DtrEnable = true; 
     this.encod = serialPort1.Encoding.Equals("iso-8859-1"); 
     this.h = serialPort1.Handshake = Handshake.RequestToSend; 
     this.p = serialPort1.Parity = Parity.None; 
     this.wtimeout = serialPort1.WriteTimeout = 300; 
     this.s = serialPort1.StopBits = StopBits.One; 
     checkLink(); 
    } 
    #endregion 
    #region checking communication and setting user controls... 
    private void checkLink() 
    { 
     GetValues value = new GetValues(); 
     string com = value.getPort(); 
     int baud = value.getBaud(); 
     int timeot = value.getTimeout(); 
     serialPort1.PortName = com; 
     serialPort1.BaudRate = baud; 
     serialPort1.ReadTimeout = timeot; 

     serialPort1.Open(); 
     if (serialPort1.IsOpen) 
     { 
      label1.Visible = true; 
     } 
     else 
     { 
      MessageBox.Show("Komunikacija sa modemom se ne može uspostaviti, molimo postavite novu konfiguraciju...!"); 
      this.Controls.Clear(); 
      SMSConfigPanel cfg = new SMSConfigPanel(); 
      cfg.Show(); 
      this.Controls.Add(cfg); 
     } 
     serialPort1.Close(); 
    } 
    #endregion 
    #region panel load method 
    private void SMSLogPanel_Load(object sender, EventArgs e) 
    { 
     setGSM(); 
    } 
    #endregion 
    #region execute serialport handler 
    public void getMessage() 
    { 
     if (serialPort1.IsOpen) 
     { 
      serialPort1.DataReceived += new SerialDataReceivedEventHandler(getResponse); 
     } 
     else 
     { 
      MessageBox.Show("Nije moguće zaprimiti poruku, komunikacijski port nije otvoren...1"); 
      return; 
     } 
    } 
    #endregion 
    #region get response from modem 
    public void getResponse(object sender, SerialDataReceivedEventArgs e) 
    { 
     SerialPort serPort = (SerialPort)sender; 
     string input = serPort.ReadExisting(); 

     if (input.Contains("+CMT:")) 
     { 

      if (input.Contains("AT+CMGF=1")) 
      { 
       string[] message = input.Split(Environment.NewLine.ToCharArray()).Skip(7).ToArray(); 
       textBox1.Text = string.Join(Environment.NewLine, message); 
      } 
      this.Invoke((MethodInvoker)delegate 
      { 
       textBox1.Text = input; 
      }); 
     } 
     else 
     { 
      return; 
     } 
    } 
    #endregion 
    #region initialize GSM 
    private void setGSM() 
    { 
     serialPort1.Open(); 

     if (!serialPort1.IsOpen) 
     { 
      MessageBox.Show("Problem u komunikaciji sa modemom, port nije otvoren...!"); 
     } 
     serialPort1.Write("AT+CMGF=1" + (char)(13)); 
     serialPort1.Write("AT+CNMI=1,2,0,0,0" + (char)(13)); 
    } 
    #endregion 
    #region setiranje timer-a... 
    private void timer1_Tick_1(object sender, EventArgs e) 
    { 
     timer1.Stop(); 
     getMessage(); 
     timer1.Start(); 
    } 
    #endregion 
} 

} 

這是僅用於測試代碼,所以它的工作原理,但有很多的修復和改善。基本上,這將是一個良好的開端爲所有正在尋找這樣的事情...

歡呼。