GSM模块发送短信失败,该如何处理

GSM模块发送短信失败
菜鸟求教 GSM模块 发送短信 发送四个字或四个字母可以发送成功 但是 发送的比较多的或者一个字 话就会报 533错误 不知道怎么回事 有木有会的朋友帮忙解答一下谢谢了



    //button事件中调用
        private void button3_Click(object sender, EventArgs e)
        {
            string msg;
            string sendphone = txtphone.Text;
            string sendMsg = txtSend.Text;
            string len = ""; 
            string logSendMsg = PDUdecoding.EncodingOther(sendMsg);
            string pcPhone = PDUdecoding.DecodingPhone(sendphone);

            try
            {
                int count = 0;
                if (sendMsg.Length % 60 == 0)
                {
                    count = sendMsg.Length / 60;
                }
                else
                {
                    count = sendMsg.Length / 60 + 1;
                }
                for (int i = 1; i <= count; i++)
                {

      msg = PDUdecoding.EncodingSMS("", pcPhone, count, i, logSendMsg, out len);
             //   comm.Write("AT+CMGF=0 \r");                   
               //  Thread.Sleep(100);
                    string ln = "AT+CMGS=" + len + " \r";
                    Thread.Sleep(100);
                    comm.Write(ln);
                    Thread.Sleep(100);
                    string val = comm.ReadExisting();
                    byte[] buf = new byte[] { 0x1a };
                    comm.Write(msg);
                    Thread.Sleep(100);
                    comm.Write(buf, 0, buf.Length); 
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }



下面这些是网上搜的转换字符的方法,调用之后转换字符串


  /// <summary>
    /// 针对国内短信编码(USC2)
    /// </summary>
    public class PDUdecoding
    {

//下面这些是网上搜的转换字符的方法,调用之后转换字符串
        public readonly static int MAX_CHAR_COUNT = 70;//最长可发送汉字个数

        /// <summary>
        /// 奇偶互换并补F
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private static string ParityChange(string value)
        {
            string result = string.Empty;
            int length = value.Length;
            for (int i = 1; i < length; i += 2)//奇偶互换
            {
                result += value[i];
                result += value[i - 1];
            }

            if (!(length % 2 == 0))//不是偶数则加上F,与最后一位互换
            {
                result += 'F';
                result += value[length - 1];
            }
            return result;
        }

        /// <summary>
        /// 短信内容编码
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        /// <remarks>
        /// 采用Big-Endian 字节顺序的 Unicode 格式编码,将高低位互换
        /// 将转换后的短信内容存进字节数组
        /// 去掉在进行Unicode格式编码中,两个字节中的"-",例如:00-21,变成0021
        /// 将整条短信内容的长度除2,保留两位16进制数
        /// </remarks>
        public static string Encoding(string value)
        {
            Encoding encoding = System.Text.Encoding.BigEndianUnicode;
            string result = string.Empty;
            byte[] bytes = encoding.GetBytes(value);

            for (int i = 0; i < bytes.Length; i++)
            {
                result += BitConverter.ToString(bytes, i, 1);
            }

            return result;
        }
        public static string EncodeingAddLength(string value)
        {
            string result = Encoding(value);
            return String.Format("{0:X2}{1}", result.Length / 2, result);
        }
        /// <summary>
        /// 短信中心号码编码
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string DecodingCenter(string phone)
        {
            string result = string.Empty;
            result = ParityChange(phone);
            result = String.Format("91{0}", result);//加上91
            result = String.Format("{0:X2}{1}", result.Length / 2, result);
            return result;
        }

        /// <summary>
        /// 接收短信手机号码编码
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static string DecodingPhone(string phone)
        {
            string result = string.Empty;

            if (null == phone || 0 == phone.Length)
            {
                return result;
            }

            if ('+' == phone[0])
            {
                phone = phone.TrimStart('+');
            }

            if (!(phone.Substring(0, 2) == "86"))//补86
            {
                phone = String.Format("86{0}", phone);
            }

            return ParityChange(phone);
        }

        /// <summary>
        /// 整个短信的编码
        /// </summary>
        /// <param name="center"></param>
        /// <param name="phone"></param>
        /// <param name="content"></param>
        /// <param name="length">要发送内容的长度,由两部分组成,接收手机号加上要发送的内容</param>
        /// <returns></returns>
        public static string EncodingSMS(string center, string phone, string content, out string length)
        {
            center = DecodingCenter(center);
            string result = String.Format("{0}11000D91{1}000800{2}", center, DecodingPhone(phone), EncodeingAddLength(content));
            length = String.Format("{0:D2}", result.Length / 2 - center.Length / 2);//获取短信内容加上手机号码长度
            return result;
        }

        /// <summary>
        /// 超长短信
        /// </summary>
        /// <param name="center"></param>
        /// <param name="phone"></param>
        /// <param name="content"></param>
        /// <param name="count"></param>
        /// <param name="i"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string EncodingSMS(string center, string phone, string content, int count, int i, out string length)
        {
            if (content.Length <= PDUdecoding.MAX_CHAR_COUNT)
            {
                return PDUdecoding.EncodingSMS(center, phone, content, out length);
            }
            else
            {
                if (count - 1 == i)
                {
                    content = content.Substring(i * (PDUdecoding.MAX_CHAR_COUNT - 6));
                }
                else
                {
                    content = content.Substring(i * (PDUdecoding.MAX_CHAR_COUNT - 6), PDUdecoding.MAX_CHAR_COUNT - 6);
                }

                center = DecodingCenter(center);
                content = Encoding(content);

                string result = "";

                DateTime tm = DateTime.Now; 
                result = string.Format("005100{0}{1}0008A7{2:X2}05000304{3:D2}{4:D2}{5}",
                                        center,
                                        DecodingPhone(phone),
                                        (content.Length + 12) / 2,
                                        count,
                                        i + 1,
                                        content);
                 
                length = String.Format("{0:D2}", result.Length / 2 - 1);//获取短信内容加上手机号码长度

                return result;
            }
        }

        public static int GetMaxEncodeCharCount()
        {
            return PDUdecoding.MAX_CHAR_COUNT * 2 - 6;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="center">短信中心号码</param>
        /// <param name="phone">接收人号码</param>
        /// <param name="count">条数每条70个字</param>
        /// <param name="i">第多少条</param>
        /// <param name="content">内容</param>
        /// <param name="length">长度</param>
        /// <returns></returns>
        public static string EncodingSMS(string center, string phone, int count, int i, string content, out string length)
        {
            string message = string.Empty;
            int msgcount = PDUdecoding.GetMaxEncodeCharCount() * 2;//固定
            if (i == count)
            {
                message = content.Substring((i - 1) * msgcount); //共可发送134个编码
            }
            else
            {
                message = content.Substring((i - 1) * msgcount, msgcount);
            }
            
            string result = String.Format("0011000D91{0}0008A008{1:D2}", phone, message);
            //length = String.Format("{0:D2}", result.Length / 2 - center.Length / 2);//获取短信内容加上手机号码长度
            length = String.Format("{0:D2}", result.Length / 2 - 1);
            return result;
        }
        public static string EncodingOther(string content)
        {
            string result = string.Empty;
            byte[] bytes = System.Text.Encoding.BigEndianUnicode.GetBytes(content); 
            foreach (char item in content)
            {
                bytes = System.Text.Encoding.BigEndianUnicode.GetBytes(new char[1] { item });
                if (bytes.Length < 2)
                    continue;

                //
                if (0x80 == (Asc(item) & 0x80))//汉字
                {
                    result = string.Format("{0}{1:X2}", result, bytes[0]);
                    result = string.Format("{0}{1:X2}", result, bytes[1]);
                }
                else
                {
                    result = string.Format("{0}00{1:X2}", result, bytes[1]);
                }
            }
            return result;
        }

        private static int Asc(char item)
        {
            byte[] bytes = System.Text.Encoding.Default.GetBytes(new char[1] { item });

            if (bytes.Length < 2)
                return bytes[0];

            return bytes[0] * 256 + bytes[1] - 65535;
        }
    }






------解决思路----------------------
哇塞 我居然可以在asp.net版本看到AT命令...
------解决思路----------------------
我虽然没用过GSM模块 不过AT到是听过一些..

比如 AT+RST 或者其他之类的一些命令..

所以我建议你先看看AT命令是如何使用的这里

然后在使用串口工具来测试下..

最终把很长的那个byte装成方法....
------解决思路----------------------
发送失败原因有几个:
1、AT+CMGS后跟的参数与实现PDU编码长度不匹配
2、PDU编码本身出错
3、延时时间,发送AT指令后要等待应答,这个时间不能过短

你的程序里面发送延时只有100毫秒在某些情况下是不够的,可以试着加长。AT+CMGS后的延时在超过2秒算超时,发送PDU编码后可能会有长达30秒的响应时间。另外发PDU和0x1A之间不需要延时


可以将你生成的编码验证一下看是否正确,以下两个网站对你有帮助
http://www.diafaan.com/sms-tutorials/gsm-modem-tutorial/online-sms-pdu-decoder/
http://www.smartposition.nl/resources/sms_pdu.html
------解决思路----------------------
检查PDU码。提示什么错误信息?
------解决思路----------------------
在拼接pdu的时候   A008那里
 
          //  string result = String.Format("0011000D91{0}0008A008{1:D2}", phone, message);//这个地方修改一下修改成
     
          string result = String.Format("0011000D91{0}0008A0{2}{1:D2}", phone, message)//08改成要发送的短信的字符长度16进制的试试