使用SMS应用程序库C#发送和接收SMS

时间:2018-08-01 12:41:26

标签: c# sms

我看到类似的主题已发布在该论坛上,但是我根本不了解如何发送AT命令和接收响应。

我需要创建可以通过LTE USB Huawei ME909s-120调制解调器发送和接收SMS消息的应用程序。到目前为止,我设法创建了可通过可用COM端口识别并连接调制解调器的应用程序。现在,我需要推送AT命令以接收消息并将其显示在VS控制台中。我想知道是否有人可以花几分钟时间向我解释该过程,并用注释修改我的代码,以便最终我可以学习和理解如何管理此过程。我需要知道的是,发送SMS时,GSM调制解调器是否接收并存储了此消息?因为我想向谁发送短信给我自动发送回复。

我正在使用在此链接中发现的特定库: https://www.codeproject.com/Articles/38705/Send-and-Read-SMS-through-a-GSM-Modem-using-AT-Com

我尝试向调制解调器发送命令点,这些基本命令例如 AT AT + CMGF = 0 AT + CPMS = ? AT + CIMI AT + COPS?可以工作,但是当我尝试使用发送和读取功能时,Visual Studio会向我显示此异常:

System.ApplicationException:'未从电话接收数据。'

我的代码出现的地方:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO.Ports;
using System.Threading;
using System.Text.RegularExpressions;

namespace SMSapplication
{
public class clsSMS
 {

    #region Open and Close Ports
    //Open Port
    public SerialPort OpenPort(string p_strPortName, int p_uBaudRate, int p_uDataBits, int p_uReadTimeout, int p_uWriteTimeout)
    {
        receiveNow = new AutoResetEvent(false);
        SerialPort port = new SerialPort();

        try
        {           
            port.PortName = p_strPortName;                 //COM5
            port.BaudRate = p_uBaudRate;                   //9600
            port.DataBits = p_uDataBits;                   //8
            port.StopBits = StopBits.One;                  //1
            port.Parity = Parity.None;                     //None
            port.ReadTimeout = p_uReadTimeout;             //300
            port.WriteTimeout = p_uWriteTimeout;           //300
            port.Encoding = Encoding.GetEncoding("iso-8859-1");
            port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            port.Open();
            port.DtrEnable = true;
            port.RtsEnable = true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return port;
    }

    //Close Port
    public void ClosePort(SerialPort port)
    {
        try
        {
            port.Close();
            port.DataReceived -= new SerialDataReceivedEventHandler(port_DataReceived);
            port = null;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    #endregion

    //Execute AT Command
    public string ExecCommand(SerialPort port,string command, int responseTimeout, string errorMessage)
    {
        try
        {

            port.DiscardOutBuffer();
            port.DiscardInBuffer();
            receiveNow.Reset();
            port.Write(command + "\r");

            string input = ReadResponse(port, responseTimeout);
            if ((input.Length == 0) || ((!input.EndsWith("\r\n> ")) && (!input.EndsWith("\r\nOK\r\n"))))
                throw new ApplicationException("No success message was received.");
            return input;
        }
        catch (Exception ex)
        {
            throw ex; // WHERE THE EXCEPTION breaks
        }
    }   

0 个答案:

没有答案