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

4

我看到这个论坛上有类似的帖子,但我不知道如何发送AT指令并接收响应。(我几个月前开始学习C#编程,虽然还是新手,但我在努力学习...)

我需要创建一个应用程序,它只能通过GSM USB dongle接收短信消息。到目前为止,我已经成功创建了一个应用程序,可以识别和连接可用的COM端口的调制解调器。现在我需要推送AT指令以接收消息并在textBox中显示它们。我想知道是否有人可以花几分钟向我解释这个过程,并修改我的代码并加上注释,以便我最终学会和理解如何使用serialPort进行通信。当SMS发送时,我需要知道什么?这条信息是否由GSM调制解调器接收并存储(并且存储直到我发送一些请求来读取它们,或者我需要发送某些事件来触发GSM调制解调器从ISP收集消息)?如何推送AT指令并接收它们的响应(我只知道这是通过使用serialPort对象完成的,但不知道如何实现...)。

这是我用于接收的方法(我卡住了... :))

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个回答

6

非常抱歉让你等待我的回复,最近很忙。 简而言之,这是我从GSM USB dongle获取消息的代码。我希望它对某些人有用...

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
}

}

这只是用于测试的代码,虽然它可以工作,但还有很多需要修复和改进的地方。基本上,对于所有正在寻找类似功能的人来说,这将是一个不错的起点...

干杯。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接