查看: 6486|回复: 0
打印 上一主题 下一主题

wavecom电信(CDMA MODEM)3G短信设备C#收发中文短信例子

[复制链接]
跳转到指定楼层
楼主
发表于 2017-7-19 11:00:08 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
wavecom CDMA MODEM发送短信,仅支持Q2358C型号电信卡短信设备,Q2438F型号需要去掉AT+CMGF=1指令,因为这个型号不支持这cmgf指令。

发送短信
发送前要先设置 :

  1. private System.IO.Ports.SerialPort  _com= new System.IO.Ports.SerialPort();

  2. _com.Write("AT+WSCL=6,4\r");
  3. _com.Write("AT+CMGF=1\r");

  4. void SendMsgByCDMA(string phone, string msg)
  5.         {
  6.             string tmp = string.Empty;
  7.             //发送的内容转为byte
  8.             byte[] b = Encoding.BigEndianUnicode.GetBytes(msg);
  9.             //cdma使用文本方式发送短信,因此每次只能发70个字符
  10.             int j = b.Length / 140;
  11.             System.Collections.Generic.List<byte[]> c = new System.Collections.Generic.List<byte[]>();
  12.             //短信进行拆分
  13.             for (int i = 0; i <= j; i++)
  14.             {
  15.                 int index = 0;
  16.                 int length = 140;
  17.                 if (b.Length < 140)
  18.                     length = b.Length;
  19.                 if (i == j)
  20.                     length = b.Length - length * i;
  21.                 if (length == 0)
  22.                     continue;
  23.                 if (i != 0)
  24.                     index = i * 140;
  25.                 byte[] temp = new byte[length];
  26.                 Array.Copy(b, index, temp, 0, length);
  27.                 c.Add(temp);
  28.             }
  29.             //CDMA的AT命令手机号码前面不能加86,否则虽然显示成功发送,但短信中心回应错误代码5  
  30.             if (phone.IndexOf("86") == 0)
  31.             {
  32.                 phone = phone.Substring(2);
  33.             }
  34.             try
  35.             {
  36.                 foreach (byte[] item in c)
  37.                 {
  38.                     //设置发送的号码和发送内容字节长度  
  39.                     _com.Write("AT+CMGS=\"" + phone + "\"," + item.Length + "\r");
  40.                     Thread.Sleep(50);
  41.                     
  42.                     //写入  
  43.                     _com.Write(item, 0, item.Length);
  44.                     //写入CTRL+Z结束短信内容,注意在UNICODE模式下需要两个字节,这个也是不能在超级终端下操作的原因,如果write中指明length的话可以不用发送  
  45.                     //byte[] b2 = new byte[] { 0x00, 0x1a };
  46.                     //_com.Write(b2, 0, b2.Length);
  47.                     ///等到运营商反馈收到命令
  48.                     _com.ReadTo("+CDS:");
  49.                     _com.DiscardInBuffer();
  50.                  }
  51.              }
  52.             catch (Exception)
  53.             {
  54.                 throw new Exception("短信发送失败");
  55.             }

  56.         }
复制代码



收短信:

SerialPort类默认的是ascii编码。

使用readerLine方法只要是中文的读出来就是乱码,使用ascii反成字节数组,再转成Unicode都无法解决。

所以就以字节的方式读取缓冲区的内容,分组后字节数组直接转Unicode才正确解码。

  1. public string ReadNewMsgByCdma(int index)
  2.         {
  3.             string result = string.Empty;
  4.             //读消息前先要注销端口消息事件
  5.             _com.DataReceived -= sp_DataReceived;
  6.             try
  7.             {
  8.                 _com.DiscardInBuffer();
  9.                 _com.Write("AT+CMGR=" + index.ToString() + "\r");
  10.                 byte[] buffer = new byte[_com.ReadBufferSize + 1]; //建立缓冲区大小的byte[]
  11.                 //读取缓冲区所有的byte
  12.                 int count = _com.Read(buffer, 0, _com.ReadBufferSize);
  13.                 //去掉空字节
  14.                 Array.Resize<byte>(ref buffer, count);
  15.                 //缓冲区读出来的内容比较多包涵了:\r  \n  \0 | OK 等字符,使用readerLine()读数据 都是\r结尾,所以根据它再分组
  16.                 List<byte[]> tempList = Split(buffer, System.Text.Encoding.ASCII.GetBytes("\r")[0]);
  17.                 ///转码
  18.                 //读出来的短信分组后一般count为4,第3条为短信正文,
  19.                 //非短信正文使用ascii进行读取,System.Text.Encoding.ASCII.GetString(tempList[0]);
  20.                 //短信正文使用Unicode读取 System.Text.Encoding.BigEndianUnicode.GetString(tempList[2]);
  21.             }
  22.             catch (Exception)
  23.             {
  24.                 _com.DataReceived += sp_DataReceived;
  25.                 throw;
  26.             }

  27.             return result;
  28.         }

  29.         /// <summary>
  30.         /// 根据\r对byte进行分组
  31.         /// </summary>
  32.         /// <param name="array"></param>
  33.         /// <param name="splitVale"></param>
  34.         /// <returns></returns>
  35.         List<byte[]> Split(byte[] array, byte splitVale)
  36.         {
  37.             List<byte[]> objList = new List<byte[]>();
  38.             int i = Array.IndexOf(array, splitVale);
  39.             while (i >= 0)
  40.             {
  41.                 byte[] temp = new byte[i + 1];
  42.                 Array.ConstrainedCopy(array, 0, temp, 0, i + 1);

  43.                 byte[] bt = new byte[array.Length - temp.Length - 1];
  44.                 Array.Copy(array, temp.Length + 1, bt, 0, bt.Length);
  45.                 array = bt;
  46.                 objList.Add(temp);
  47.                 i = Array.IndexOf(array, splitVale);
  48.             }

  49.             return objList;
  50.         }
复制代码



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|小黑屋|未来时代科技 ( 粤ICP备12044031号-1

GMT+8, 2024-4-25 09:39 , Processed in 0.048829 second(s), 27 queries .

Powered by WLSD X3.1

© 2013-2014 WLSD Inc.

快速回复 返回顶部 返回列表
 
【电话】(15118131494)
【QQ】 未来时代科技01 售前咨询
【QQ】 未来时代科技02 售后技术
【旺旺】 请问有什么可以帮到您?不在线可留言.
【邮箱】
inextera@sina.com
【地址】 (深圳市龙岗坂田扬马小区)