zoukankan      html  css  js  c++  java
  • 中国电信CDMA无线上网卡短信收发

    发送短信
     /// <summary>
            
    /// 发送短信
            
    /// </summary>
            
    /// <param name="phone">The phone.</param>
            
    /// <param name="message">The message.</param>
            public static void SendMessageBySendPhone(string phone, string message)
            {
                BaseCode.BaseTerm.phone.MessageSending 
    = true;
                
    string text =@"AT^HCMGS=""" + phone + @"""";
                
    string text2 = message;

                BaseTerm.term.SendString(text);
                BaseTerm.term.SendCtrl(
    "CR");
                Thread.Sleep(
    300);
                
    byte[] msgbytes = Encoding.Unicode.GetBytes(text2);
                
    byte[] msgbytesrnd = new byte[msgbytes.Length];
                
    for (int i = 1; i <= msgbytes.Length; i = i + 2)
                {
                    msgbytesrnd[i 
    - 1= msgbytes[i];
                    msgbytesrnd[i] 
    = msgbytes[i - 1];
                }

                BaseTerm.term.SendBytes(msgbytesrnd);
                BaseTerm.term.SendString(
    "");
                BaseTerm.term.SendCtrl(
    "CR");
            }
     
    有短信进入
     //todo:有短信进入
                    _reg = new Regex(@"\+CMTI:[\s]*""(?<Mem>[A-Z a-z 0-9_]+)""[\s]*,[\s]*(?<Index>\d+)");
                    MatchCollection match 
    = _reg.Matches(cmd);
                    
    foreach (Match m in match)
                    {
                        Console.WriteLine(m.Groups[
    "Mem"].Value + "," + m.Groups["Index"].Value);
                        
    if (ShowInfo != null)
                            ShowInfo(
    this"新短信,存储位置:" + m.Groups["Mem"].Value + " 索引号:" + m.Groups["Index"].Value);

                        
    if (MessageCNMIEvent != null)
                            MessageCNMIEvent(
    this, Convert.ToInt32(m.Groups["Index"].Value), m.Groups["Mem"].Value);

                        MessageCommands.GetMessageCMD(m.Groups[
    "Index"].Value, 1);
                    }
    显示短信内容
      //todo:显示短信内容
                    /*
                     * 取值说明
                     * <index> 整型值,在存储器中的位置
                     * <mode> 短信状态的修改模式:
                     * 0 改变短信为已读状态
                     * 1 不改变短信的读取状态
                     * <callerID> 短消息发送方的号码
                     * <format> 表示该短消息的编码格式,取值参考^HCMGW
                     * <year, month, day, hour, minute,second>为接收到短消息时的年、月、日、小时、分钟和
                     * 秒。
                     * <Length> 接收到短消息的长度。
                     * <lang> 语言,取值参考^HCMGW
                     * <prt> 短信优先级,取值参考^HCMGW
                     * <Prv> 保密级别
                     * 0 Normal;
                     * 1 Restricted;
                     * 2 Confidential;
                     * 3 Secret
                     * <type> 短信类型
                     * 0 Normal;
                     * 1 CPT;
                     * 2 Voice Mail;
                     * 3 SMS Report
                     * <stat> 短信类型,取值参考^HCMGW
                     * <Msg> 接收到的短消息。
                     * <ctrl-Z>:标识一条短信息内容的结束,编码方式为非UNICOD方式下字符为:'0x1A',
                     * 编码方式为UNICOD方式下字符为:'0x001A'
                     
    */
                    
    //^HCMGR:07713997270,2010,02,28,10,43,50,0,6,34,2,0,0,1

                    _reg 
    = new Regex(
                        
    @"\r\n\^HCMGR:[\s]*(?<Caller>\d*)[\s]*,[\s]*(?<Year>\d*)[\s]*,[\s]*(?<Month>\d*)[\s]*,[\s]*(?<Day>\d*)[\s]*,[\s]*(?<Hour>\d*)[\s]*,[\s]*(?<Minute>\d*)[\s]*,[\s]*(?<Second>\d*)[\s]*,[\s]*(?<Lang>\d*)[\s]*,[\s]*(?<Format>\d*)[\s]*,[\s]*(?<Length>\d*)[\s]*,[\s]*(?<Prt>\d*)[\s]*,[\s]*(?<Prv>\d*)[\s]*,[\s]*(?<Type>\d*)[\s]*,[\s]*(?<Stat>\d*)\r\n",
                        RegexOptions.IgnoreCase);

                    MatchCollection match 
    = _reg.Matches(cmd);
                    
    foreach (Match m in match)
                    {
                        
    string format = m.Groups["Format"].Value;

                        
    int start = 0;
                        
    int end = 0;
                        
    for (int i = 0; i < bytes.Length; i++)
                        {
                            
    if (bytes[i].ToString() == "10" && start == 0 && i > 1)
                            {
                                start 
    = i;
                            }
                            
    if (bytes[i].ToString() == "26" && end == 0)
                            {
                                end 
    = i;
                            }
                        }
                        
    int len = bytes.Length - 9 - start;
                        
    if (len % 2 != 0) len = len - 1;
                        var msgbytes 
    = new byte[len];
                        
    //Console.WriteLine("未截取:" + BitConverter.ToString(bytes));
                        Array.Copy(bytes, start + 1, msgbytes, 0, len);
                        
    //Console.WriteLine("转换前:" + BitConverter.ToString(msgbytes));
                        if (format == "6")
                        {
                            
    for (int i = 1; i <= msgbytes.Length; i = i + 2)
                            {
                                
    byte temp = msgbytes[i - 1];
                                msgbytes[i 
    - 1= msgbytes[i];
                                msgbytes[i] 
    = temp;
                            }
                        }
                        
    if (MessageHCMGREvent != null)
                            MessageHCMGREvent(
    this, m.Groups["Caller"].Value,
                                              Convert.ToDateTime(m.Groups[
    "Year"].Value + "-" + m.Groups["Month"].Value +
                                                                 
    "-" +
                                                                 m.Groups[
    "Day"].Value + " " + m.Groups["Hour"].Value + ":" +
                                                                 m.Groups[
    "Minute"].Value + ":" + m.Groups["Second"].Value),
                                              Encoding.Unicode.GetString(msgbytes));
                        
    //Console.WriteLine("转换后:"+BitConverter.ToString(msgbytes));
                        if (ShowInfo != null)
                            ShowInfo(
    this, m.Groups["Caller"].Value + "短信内容:" + Encoding.Unicode.GetString(msgbytes));
                        
    if (ShowInfo != null)
                            ShowInfo(
    this"字节内容:" + BitConverter.ToString(msgbytes));
                    }

     其实短信内容解码很简单,AT串口采用的是ASCII编码,短信的内容采用的是Unicode编码,并且每个字符的高低位互换了。根据规范截取出短信的内容的byte[],进行高低位互换,在进行Encoding.Unicode.GetString,发短信反之。

  • 相关阅读:
    linux上部署docker+tomcat服务,并部署项目
    docker使用Dockerfile把springboot项目jar包生成镜像并运行
    springboot配置log4j
    mysql常用函数
    java处理csv文件上传示例
    中国城市区号脚本-mysql
    java微信公众号支付示例
    java导出csv格式文件
    mysql时间相加函数DATE_ADD()
    centos分区
  • 原文地址:https://www.cnblogs.com/Tranquilty/p/1801192.html
Copyright © 2011-2022 走看看