4

我看到这个论坛上也有类似的话题,但是我根本不明白如何发送AT命令并接收响应。(几个月前我开始用 C# 编程。我仍然是 n00b,但我正在努力学习它......)。

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

如果有人愿意提供帮助,我将不胜感激,否则我将不得不自己处理(可能花了几个小时/几天,直到我弄清楚......)

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

4

1 回答 1

6

抱歉,一直在等我的回复,最近很忙。简而言之,这是我从 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
}

}

这只是用于测试的代码,因此它可以工作,但还有很多需要修复和改进的地方。基本上,对于所有正在搜索这样的东西的人来说,这将是一个很好的开始......

干杯。

于 2014-07-16T19:13:19.503 回答