2017-06-23 61 views
0

當我第一次發送短消息它發送成功但當我按下按鈕第二次它不發送消息我混淆了串口打開或關閉或AT命令在哪裏正常工作在c#.net中通過DWM-156發送短消息的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(); 
     } 
+0

相反 「\ X1A」 的嘗試使用(炭)26:https://social.msdn.microsoft.com/Forums/vstudio/en-US/2c3947e6-cc03-4822-a70e-3cba86747ced/how-to-send-ctrlz-through-serial-in-c-?forum = csharpgeneral&https://stackoverflow.com/questions/7188566/sending-ctrlz-to- a-serial-port – PaulF

+0

可能需要檢查調制解調器的響應,並檢查RTS線是否已置位(可能是調制解調器上的LED)。 – PaulF

回答

0

發送消息後,就把3秒延遲下一條消息發送之前。

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

,並保持該消息長度< 150個字符