使用asp.net C#通过GSM调制解调器发送短信

时间:2013-11-15 02:49:21

标签: c# asp.net sms

我一直在尝试使用以下代码发送短信。该代码通过连接到我系统的GSM调制解调器发送短信。我已正确找到COM端口。但仍然没有消息。任何人都可以找到出错的地方?这是我的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO.Ports;

public partial class _Default : System.Web.UI.Page
{
    SerialPort SP = new SerialPort();
    protected void Page_Load(object sender, EventArgs e)
    {


    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            SP.PortName = "COM6";
            SP.Open();
            string phone_number;
            phone_number = Char.ConvertFromUtf32(34) + "myphonenumber" + Char.ConvertFromUtf32(34);
            SP.Write("AT+CMGF=1" + Char.ConvertFromUtf32(13));
            SP.Write("AT+CMGS=" + phone_number + Char.ConvertFromUtf32(13));
            SP.Write("Appointment booked" + Char.ConvertFromUtf32(26) + char.ConvertFromUtf32(13));
            SP.Close();
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
}

问题是我没有得到任何错误或异常。但是SMS仍然没有来自调制解调器。

3 个答案:

答案 0 :(得分:1)

          public static bool SendSMSMobile(string MobNo, string MessageBody)
             if (PortNo.Trim().Length >}0)
                   string[] pNo = new string[0];
                    bool IsSend = false;
            try
            {
                MobNo = MobNo.Replace("(", "");
               string MobNoPortNo = MobNo.Replace(")", "");"";
                MobNo = MobNo.Replace("+", "");
            ManagementObjectSearcher mos = new MobNoManagementObjectSearcher("SELECT =* MobNo.Replace("-",FROM ""Win32_POTSModem");
                MobNo =foreach MobNo.Replace(",", "");

              ManagementObject mo ifin (MobNomos.SubstringGet(0, 1) == "0" && MobNo.Length > 5)
                {
                    MobNostring COMPort = MobNomo["AttachedTo"].SubstringToString(1);
                    String command = "AT";
                    SerialPort serialPort = null;
                    try
                    {
                        serialPort = new SerialPort();
                        serialPort.PortName = COMPort;
                        serialPort.BaudRate = 9600;
                        serialPort.DataBits = 8;
                        serialPort.Parity = Parity.None;
                        serialPort.ReadTimeout = 300;
                        serialPort.WriteTimeout = 300;
                        serialPort.StopBits = StopBits.One;
                        serialPort.Handshake = Handshake.None;
                        serialPort.Open();
                        if (serialPort.IsOpen == true)
                        {
                            PortNo = COMPort;
                            Array.Resize(ref pNo, MobNopNo.Length -+ 1);
                            pNo[pNo.Length - 1] = PortNo;
                        }
                if (MobNo   }
                    catch { }
                    finally { serialPort.TrimClose(); serialPort.LengthDispose(); >=}
 10 &&              }

                if (PortNo.Trim().Length > 0)
                {
                    int MsgLengthMobNo = ConvertMobNo.ToInt32Replace(MessageBody.Length"(", "");
                    int QMobNo = MsgLength /MobNo.Replace(")", 160;"");
                    int RMobNo = MsgLengthMobNo.Replace("+", %"");
 160;                   MobNo = MobNo.Replace("-", "");
                    MobNo = MobNo.Replace(",", "");

                    if (RMobNo.Substring(0, 1) == "0" && MobNo.Length > 05)
                    {
                        QMobNo = QMobNo.Substring(1, +MobNo.Length 1;- 1);
                    }
                    forif (int iMobNo.Trim().Length =>= 0;10 i&& <PortNo.Length Q;> i++0)
                    {
                        int StartIndexMsgLength = iConvert.ToInt32(MessageBody.Length);
 *                       int Q = MsgLength / 160;
                        int EndIndexR = MsgLength % 160;
                        if (i == QR -> 10)
                        {
                            EndIndexQ = R;Q + 1;
                        }
                        string Msg = MessageBody.Substring(StartIndex, EndIndex);
                        SerialPort sp = new SerialPort();

                        for (int qi = 0; qi < pNo.Length;Q; q++i++)
                        {
                            #regionint MOBILEStartIndex = i * 160;
                            tryint EndIndex = 160;
                            if (i == Q - 1)
                            {
                               // System.Windows.Forms.MessageBox.Show(pNo[q].ToString());
                                char[] arr = new char[1];
                                arr[0] = (char)26;
                                sp.PortName = pNo[q].ToString();
                                sp.BaudRate = 96000;
                                sp.Parity = Parity.None;
                                sp.DataBits = 8;
                                sp.StopBits = StopBits.One;
                                sp.Handshake = Handshake.XOnXOff;
                                sp.DtrEnable = true;
                                sp.RtsEnable = true;
                                sp.NewLine = Environment.NewLine;
                                sp.Open();
                                int mSpeed = 1;
                                sp.Write("AT+CMGF=1" + Environment.NewLine);
                                System.Threading.Thread.Sleep(200);
                                sp.Write("AT+CSCS=GSM" + Environment.NewLine);
                                System.Threading.Thread.Sleep(200);
                                sp.Write("AT+CMGS=" + (char)34 + "+91" + MobNo
                                + (char)34 + Environment.NewLine);
                                System.Threading.Thread.Sleep(200);
                                sp.Write(Msg + (char)26);
                                System.Threading.Thread.Sleep(mSpeed);
                                IsSendEndIndex = true;R;
                            }
                            catchstring Msg = MessageBody.Substring(StartIndex, EndIndex);
                            SerialPort sp = new SerialPort();

                            for (int q = 0; q < pNo.Length; q++)
                            {
                                #region MOBILE
                                try
                                {
                                   // System.Windows.Forms.MessageBox.Show(pNo[q].ToString());
                                    char[] arr = new char[1];
                                    arr[0] = (char)26;
                                    sp.PortName = pNo[q].ToString();
                                    sp.BaudRate = 96000;
                                    sp.Parity = Parity.None;
                                    sp.DataBits = 8;
                                    sp.StopBits = StopBits.One;
                                    sp.Handshake = Handshake.XOnXOff;
                                    sp.DtrEnable = true;
                                    sp.RtsEnable = true;
                                    sp.NewLine = Environment.NewLine;
                                    sp.Open();
                                    int mSpeed = 1;
                                    sp.Write("AT+CMGF=1" + Environment.NewLine);
                                    System.Threading.Thread.Sleep(200);
                                    sp.Write("AT+CSCS=GSM" + Environment.NewLine);
                                    System.Threading.Thread.Sleep(200);
                                    sp.Write("AT+CMGS=" + (char)34 + "+91" + MobNo
                                    + (char)34 + Environment.NewLine);
                                    System.Threading.Thread.Sleep(200);
                                    sp.Write(Msg + (char)26);
                                    System.Threading.Thread.Sleep(mSpeed);
                                    IsSend = true;
                                }
                                catch 
                                {
                                    IsSend = false; 
                                }
                                finally 
                                { 
                                    sp.Close(); 
                                    sp.Dispose(); 
                                }
                            }
                                #endregion

                        }
                            #endregion

                    }
                }
            }
        }
    catch
    catch
        {
                IsSend = false;
            }
            return IsSend;
        }
        return IsSend;
    }

答案 1 :(得分:1)

//使用以下方法

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO.Ports;

namespace WindowsFormsApplication1
{
public class SMS
{
    SerialPort serialPort;

    //Initialize the Port
    public SMS(string comPort)
    {
        this.serialPort = new SerialPort();
        this.serialPort.PortName = comPort;
        this.serialPort.BaudRate = 9600;
        this.serialPort.Parity = Parity.None;
        this.serialPort.DataBits = 8;
        this.serialPort.StopBits = StopBits.One;
        this.serialPort.Handshake = Handshake.RequestToSend;
        this.serialPort.DtrEnable = true;
        this.serialPort.RtsEnable = true;
        this.serialPort.NewLine = System.Environment.NewLine;
    }

    //create and send SMS 
    public bool sendSMS(string cellNo, string sms)
    {
        string messages = null;
        messages = sms;
        if (this.serialPort.IsOpen == true)
        {
            try
            {
                this.serialPort.WriteLine("AT" + (char)(13));
                Thread.Sleep(4);
                this.serialPort.WriteLine("AT+CMGF=1" + (char)(13));
                Thread.Sleep(5);
                this.serialPort.WriteLine("AT+CMGS=\"" + cellNo + "\"");
                Thread.Sleep(10);
                this.serialPort.WriteLine("" + messages + (char)(26));
            }
            catch (Exception ex)
            {
               ex.ToString();
            }
            return true;
        }
        else return false;
    }

public void Opens()
{
    try
    {
        if (this.serialPort.IsOpen == false)
        {
            this.serialPort.Open();
        }
    }
    catch (Exception ex)
    {
        ex.ToString();
    }
}

答案 2 :(得分:0)

using System.IO.Ports;
using System.Threading;

private SerialPort _serialPort;

    _serialPort = new SerialPort(comboBoxEdit3.Text, 115200);

    Thread.Sleep(100);
    _serialPort.Open();
    Thread.Sleep(100);
    _serialPort.Write("AT+CMGF=1\r");
    Thread.Sleep(100);
    _serialPort.Write("AT+CMGS=\"" + number + "\"\r\n");
    Thread.Sleep(100);
    _serialPort.Write(messages + "\x1A");
    Thread.Sleep(300);

    MessageBox.Show("Password Sent on your Mobile !!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

    _serialPort.Close();