AT命令用于在c#.Net中通过DWM-156发送短信

时间:2017-06-23 07:50:24

标签: c# winforms gsm

当我第一次发送短信成功发送时,但是当我第二次按下按钮时它不发送消息我混淆了串口打开或关闭或AT命令正常工作的地方

抱歉英语不好

这是我的完整代码

 SerialPort serialPort = new SerialPort();  
    private void Form1_Load(object sender, EventArgs e)
        {
            this.serialPort = new SerialPort();
            this.serialPort.PortName = "COM6";
            this.serialPort.BaudRate = 921600;
            this.serialPort.Parity = Parity.None;
            this.serialPort.DataBits = 8;
            this.serialPort.StopBits = StopBits.One;
            serialPort.WriteBufferSize = 300;
            this.serialPort.Handshake = Handshake.RequestToSend;
            this.serialPort.DtrEnable = true;
            this.serialPort.RtsEnable = true;
            serialPort.Open();

        }
         public bool send_sms()
         {
             String SMSMessage = txtmsg.Text;
             String CellNumber = cellNum.Text;
             String messageToSend = null;
             if (SMSMessage.Length <= 160)
             {
                 messageToSend = SMSMessage;
             }
             else
             {
                 messageToSend = SMSMessage.Substring(0, 160);
             }
             if (serialPort.IsOpen)
             {
                 this.serialPort.Write("AT\r");
                 Thread.Sleep(1000);
                 this.serialPort.Write("AT+CMGF=1\r");
                 Thread.Sleep(1000);
                 this.serialPort.Write("AT+CMGS=\"" + CellNumber + "\"\r\n");
                 Thread.Sleep(1000);
                 this.serialPort.Write(SMSMessage + "\x1A");
                 return true;
             }
             return false;
         }

        private void sendbtn_Click(object sender, EventArgs e)
        {
            send_sms();
        }

1 个答案:

答案 0 :(得分:0)

发送消息后,在下一封消息发送之前延迟3秒。

this.serialPort.Write("AT+CMGS=\"" + CellNumber + "\"\r\n");
this.serialPort.Write(SMSMessage + "\x1A");
Thread.Sleep(3000);

并保持消息长度&lt; 150个字符