通过GSM调制解调器接收短信

时间:2013-07-10 10:12:10

标签: c# gsm modem

我看到在这个论坛上发布了类似的主题,但我根本不明白如何发送AT命令并收到回复。 (几个月前我开始用C#编程。我还是一个n00b,但我正在努力学习它......)。

我需要创建只能通过GSM USB加密狗接收短信的应用程序。到目前为止,我设法创建应用程序,通过可用的COM端口识别和连接调制解调器。现在我需要按AT命令来接收消息并将它们显示到textBox中。我想知道是否有人可以花几分钟时间向我解释这个过程,并用注释修改我的代码,这样我终于可以学习并理解如何使用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();
}

如果有人愿意提供帮助,我会很感激,如果不是,我将不得不自己处理它(可能花了几个小时/天直到我弄明白......)

在这两种情况下,无论如何,谢谢你......干杯。

1 个答案:

答案 0 :(得分: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
}

}

这只是测试的代码,所以它可以工作,但有很多需要修复和改进。基本上,对于所有正在搜索此类内容的人来说,这将是一个不错的开始......

欢呼声。