关闭

短信猫发送短信没有回执

465人阅读 评论(0) 收藏 举报

       各位高手帮我看下。这个是用c#通过at指令发送手机短信的程序。

                 短信发出去接收方也收到短信了,但是没有任何的返回信息。串口好像也被一直占着。只有重新启用一下串口才能再次使用。。。。

 

 

 

        SerialPort port;

        //构造并初始化串口
        private void button2_Click(object sender, EventArgs e)
        {
            port = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
            port.ReadTimeout = 20000;
            port.WriteTimeout = 20000;
            port.RtsEnable = true; 
            port.Open();
        }

        private void button1_Click(object sender, EventArgs e1)
        {
            string receiveData = string.Empty;
            int resultId = SendCommand("AT\r", ref receiveData, 3);
            resultId = SendCommand("AT+CMEE=1\r", ref receiveData, 3);
            resultId = SendCommand("AT+CNMI=2,1,0,1,1\r", ref receiveData, 3);
            resultId = SendCommand("AT+CMGF=0\r", ref receiveData, 3);
            resultId = SendCommand("AT+CSCS=\"HEX\"\r", ref receiveData, 3);
            resultId = SendCommand("AT+CMGS=16\r", ref receiveData, 3);
            if (receiveData.ToUpper().Contains(">"))
            {
                resultId = SendCommand("0031000D91688117407190F10000000112" + (char)(26), ref receiveData, 10);
            }
        }

        /// 发送各种命令       
        /// </summary>       
        /// <param name="Sendcmd">命令正文</param>       
        /// <param name="ReceiveData">返回结果</param>      
        /// /// <param name="Overtime">发送超时</param>     
        /// /// <returns>发送标识</returns>   
        private int SendCommand(string Sendcmd, ref string ReceiveData, int Overtime)
        {
            if (port.IsOpen)
            {
                try
                {
                    port.DiscardInBuffer(); //清空接收缓冲区        
                    port.Write(Sendcmd); //写入命令        
                    int num = 0, ret = 0;
                    while (num++ < Overtime)
                    {
                        System.Threading.Thread.Sleep(1000);
                        if (port.BytesToRead > 0) //存在需要读取的数据      
                        {
                            ret = port.BytesToRead;
                            break;
                        }
                        else
                        {
                            System.Threading.Thread.Sleep(1); //延时1秒 等待数据发送到端口上          
                        }
                    }
                    if (ret > 0) //如果要读取的数据存在 读取数据 并且 打开接收        
                    {
                        ReceiveData = port.ReadExisting(); //先读取再激活接收事件 ReadExecting()      
                    }
                    return ret;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return -1;
        }

       

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:757次
    • 积分:22
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档