简体   繁体   English

通过C#中的串行端口发送SMS

[英]Sending SMS Through Serial port in c#

I am using Serial port to send sms through a GSM/Modem .My code is working with single sms. 我正在使用串行端口通过GSM /调制解调器发送短信。我的代码适用于单个短信。 The problem arise when I try to send sms in bulk. 当我尝试批量发送短信时会出现问题。 Then no sms are sent and no exceptions are generated. 这样就不会发送任何短信,也不会生成任何异常。

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.IO.Ports;
 using System.ComponentModel;
 using System.Windows.Forms;
 namespace program.cs
 {
    public class Class1
 {
    public string[] strarray = new string[10];
    SerialPort serialport1 = new SerialPort();
    string com = "COM8";
    int mybaudrate = 9600;


    //this.strarray =System.IO.Ports.SerialPort.GetPortNames();


    public void getports()
    {
        this.strarray = System.IO.Ports.SerialPort.GetPortNames();

    }

    public bool connetport() 
    {
        bool Isopen;
        serialport1.Close();
        try
        {
            if (!this.serialport1.IsOpen) 
            {
                this.serialport1.PortName = com;

                this.serialport1.Open();
                this.serialport1.BaudRate = mybaudrate;
                this.serialport1.StopBits = System.IO.Ports.StopBits.One;
                this.serialport1.Parity = System.IO.Ports.Parity.None;
                this.serialport1.Handshake = System.IO.Ports.Handshake.None;

                 Isopen= serialport1.IsOpen;




            }

            Isopen = true;


            }

        catch (Exception ex)
        {
            Isopen = false;
            throw ex;
        }
        return Isopen;   
    }

    public void sendsms() 
    {
        try
        {

            if (this.serialport1.IsOpen)
            {

                // to send bulk sms


                serialport1.BaseStream.Flush();

                int loop = 0;
                int howmany = 0;

                howmany = 200;



                while (loop < howmany)
                {

                    System.Threading.Thread.Sleep(3500);

                    string cb = char.ConvertFromUtf32(26);
                    this.serialport1.Write("AT+CMGF=1\r");
                   this.serialport1.Write("AT+CSCA=servicecenter\r   
                   \n");//Ufone              Service Center                    
                    this.serialport1.Write("AT+CMGS=\"" + "03468916446" + "\"\r\n");// 
                    this.serialport1.Write("hello" + cb);//message text
                    message sending

                    System.Threading.Thread.Sleep(3500);

                    loop++;


                }
                MessageBox.Show("Message Sent Number" + loop);

                serialport1.Close();

            }
        }
        catch (Exception ex) 
        {
            serialport1.Close();
            throw ex;
        }


    }







}

} }

Class1 above is used to connect and send sms 上面的Class1用于连接和发送短信


Code For Main Program 主程序代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    bool check;
    Class1 obj = new Class1();
    private void button1_Click(object sender, EventArgs e)
    {
        check = obj.connetport();
        MessageBox.Show("Connecton Status" + check);
        obj.sendsms();
        MessageBox.Show("have completed");
    }
}

I have invested a lot of time on solving this problem but without success. 我花了很多时间解决这个问题,但没有成功。 Your help will be appreciated 您的帮助将不胜感激

I would suggest using a tool like Portmon ( http://technet.microsoft.com/en-us/sysinternals/bb896644.aspx ) to make sure your messages are being recieved by the modem. 我建议使用类似Portmon的工具( http://technet.microsoft.com/zh-cn/sysinternals/bb896644.aspx )以确保调制解调器接收到您的消息。

If they are you may want to check with your carrier as a lot of them blackhole bulk sms messages. 如果是这样,您可能希望与您的运营商联系,因为它们中有很多都是黑洞批量短信消息。 (to avoid spammers and the like) (以避免垃圾邮件发送者等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM