C# AT 命令 发送/接收短信

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12290414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:45:36  来源:igfitidea点击:

AT commands Send/receive SMS

c#visual-studio-2010smsnokiaat-command

提问by Ahmed Khakwani

I am new to AT commands. I am using Nokia E71 to send and receive SMS. I am designing an application for sending SMS, but my code is not working.

我是 AT 命令的新手。我正在使用诺基亚 E71 发送和接收短信。我正在设计一个发送短信的应用程序,但我的代码不起作用。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;

namespace AT_commands
    {
    public partial class Form1 : Form
        {
        SerialPort serialPort;
        public Form1()
            {
            InitializeComponent();
            }

        public void Form1_Load(object sender, EventArgs e)
            {
            this.serialPort = new SerialPort();
            this.serialPort.PortName = "COM23";
            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;
            send_sms();
            }
        public bool send_sms()
            {
            label1.Text = "Loaded Successfuly";
            String SMSMessage = "Message to send";
            String CellNumber = "+923333333333";
            String messageToSend = null;
            if (SMSMessage.Length <= 160)
                {
                messageToSend = SMSMessage;
                }
            else
                {
                messageToSend = SMSMessage.Substring(0, 160);
                }
            if (this.IsOpen == true)
                {
                this.serialPort.WriteLine(@"AT" + (char)(13));
                Thread.Sleep(200);
                this.serialPort.WriteLine("AT+CMGF=1" + (char)(13));
                Thread.Sleep(200);
                this.serialPort.WriteLine(@"AT+CMGS=""" + CellNumber + @"""" + (char)(13));
                Thread.Sleep(200);
                this.serialPort.WriteLine(SMSMessage + (char)(26));
                return true;
                }
            return false;
            }
        public void Open()
            {
            if (this.IsOpen == false)
                {
                this.serialPort.Open();
                }
            }

        public void Close()
            {
            if (this.IsOpen == true)
                {
                this.serialPort.Close();
                }
            }

        public bool IsOpen
            {
            get
                {
                return this.serialPort.IsOpen;
                }
            }

        public void Dispose()
            {
            if (this.IsOpen)
                this.Close();
            }
        }
    }

Please help me with this code!

请帮我处理这个代码!

回答by Cdeez

Why dont you open the port connection in the form_load() itself, later you can close it at the end as you did.

为什么不在 form_load() 本身中打开端口连接,稍后您可以像这样做一样在最后关闭它。

And do these too in Form_Load():

并在 Form_Load() 中执行这些操作:

string cmd = "AT";
port.WriteLine(cmd + "\r");
port.Write(cmd + "\r");
port.WriteLine("AT+CMGF=1");

And simplifying the sms sending code:

并简化短信发送代码:

port.WriteLine("AT+CMGS=\"" + PhNumber + "\"");
port.Write(Message + char.ConvertFromUtf32(26));

回答by Jorick Caberio

Here's my code

这是我的代码

using System;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;

namespace CSharp_SMS
{
  public partial class Form_SMS_Sender : Form
  {
    private SerialPort _serialPort;
    public Form_SMS_Sender()
    {
        InitializeComponent();
    }

    private void buttonSend_Click(object sender, EventArgs e)
    {
        string number = textBoxNumber.Text;
        string message = textBoxMessage.Text;

        //Replace "COM7"withcorresponding port name
        _serialPort = new SerialPort("COM7", 115200);   

        Thread.Sleep(1000);

        _serialPort.Open();

        Thread.Sleep(1000);

        _serialPort.Write("AT+CMGF=1\r");

        Thread.Sleep(1000);

        _serialPort.Write("AT+CMGS=\"" + number + "\"\r\n");

        Thread.Sleep(1000);

        _serialPort.Write(message + "\x1A");

        Thread.Sleep(1000);

        labelStatus.Text = "Status: Message sent";

        _serialPort.Close();
        }
    }
}

Here's a link http://circuitfreak.blogspot.com/2013/03/c-programming-sending-sms-using-at.html

这是一个链接http://circuitfreak.blogspot.com/2013/03/c-programming-sending-sms-using-at.html

回答by Awais Mahmood

Try it this way.. You are not opening the connection to the serial port. I have tried it and it is working fine for me.

以这种方式尝试.. 您没有打开与串行端口的连接。我已经试过了,它对我来说很好用。

 private void button1_Click(object sender, EventArgs e)
 {
     this.serialPort = new SerialPort();
     this.serialPort.PortName = "COM5";
     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;
     serialPort.Open();
     send_sms();
 }
 public bool send_sms()
 {
     String SMSMessage = "gsm MESSAGE FROM .NET C#";
     String CellNumber = "+9233333333333";
     String messageToSend = null;
     if (SMSMessage.Length <= 160)
     {
         messageToSend = SMSMessage;
     }
     else
     {
         messageToSend = SMSMessage.Substring(0, 160);
     }
     if (serialPort.IsOpen)
     {
         this.serialPort.WriteLine(@"AT" + (char)(13));
         Thread.Sleep(200);
         this.serialPort.WriteLine("AT+CMGF=1" + (char)(13));
         Thread.Sleep(200);
         this.serialPort.WriteLine(@"AT+CMGS=""" + CellNumber + @"""" + (char)(13));
         Thread.Sleep(200);
         this.serialPort.WriteLine(SMSMessage + (char)(26));
         return true;
         }
     return false;
 }