2013-07-10 94 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 
} 

} 

这是仅用于测试代码,所以它的工作原理,但有很多的修复和改善。基本上,这将是一个良好的开端为所有正在寻找这样的事情...

欢呼。