分享

C# at命令发送&&接收短信测试程序

 昵称10504424 2013-02-19

此文转载,出处不明。

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.    
  10. namespace SPAT  
  11. {  
  12.    
  13.     delegate void del_ShowMsg(string msg);  
  14.    
  15.     public partial class Form1 : Form  
  16.     {  
  17.         public Form1()  
  18.         {  
  19.             InitializeComponent();  
  20.             button1.Enabled = false;  
  21.             button3.Enabled = false;  
  22.             button4.Enabled = false;  
  23.             button5.Enabled = false;  
  24.             button6.Enabled = false;  
  25.             button7.Enabled = false;  
  26.             textBox7.Enabled = true;  
  27.             textBox7.Text = "0";  
  28.             textBox8.Text = "0";  
  29.             textBox7.Enabled = false;  
  30.             textBox10.Enabled = false;  
  31.             button8.Enabled = false;  
  32.             button9.Enabled = false;  
  33.             button10.Enabled = false;  
  34.    
  35.         }  
  36.    
  37.         private void Form1_Load(object sender, EventArgs e)  
  38.         {  
  39.         }  
  40.    
  41.         private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)  
  42.         {  
  43.             if (serialPort1.IsOpen)  
  44.                 Invoke(new del_ShowMsg(ShowMsg), serialPort1.ReadLine());  
  45.             else  
  46.                 return;  
  47.         }  
  48.         private void ShowMsg(string msg)  
  49.         {  
  50.             textBox9.Text += msg;  
  51.             int index = msg.IndexOf("+CSCA:");  
  52.             if (index != -1)  
  53.             {  
  54.                 textBox10.Enabled = true;  
  55.                 textBox10.Text = msg.Substring(index + 11, 11);  
  56.                 textBox10.Enabled = false;  
  57.                 return;  
  58.             }  
  59.             if (msg.IndexOf("\"SM\",") != -1)  
  60.             {  
  61.                 int number = int.Parse(msg.Substring(msg.IndexOf("\"SM\",") + 5));  
  62.                 int number7 = int.Parse(textBox7.Text);  
  63.                 MessageBox.Show("有新信息,位置为:" + msg.Substring(msg.IndexOf("\"SM\",") + 5));  
  64.                 textBox8.Text = msg.Substring(msg.IndexOf("\"SM\",") + 5);  
  65.                 if (number7 < number)  
  66.                 {  
  67.                     textBox7.Enabled = true;  
  68.                     textBox7.Text = number.ToString();  
  69.                     textBox7.Enabled = false;  
  70.                 }  
  71.                 return;  
  72.             }  
  73.             if (msg.Length > 33)  
  74.             {  
  75.                 string telnum = "+" + DecodeTelnum(msg);  
  76.                 string msgDestination = DecodeMessage(msg);  
  77.                 string time = DecodeTime(msg);  
  78.                 textBox1.Text = telnum;  
  79.                 textBox4.Text = msgDestination;  
  80.                 textBox5.Text = time;  
  81.             }  
  82.             else  
  83.             {  
  84.                 if (msg.Equals("Error"))  
  85.                 {  
  86.                     textBox1.Text = "";  
  87.                     textBox4.Text = "";  
  88.                     textBox5.Text = "";  
  89.                 }  
  90.                 int index1 = msg.IndexOf("+CMGL:");  
  91.                 int index2 = msg.IndexOf(",");  
  92.                 if (index1 != -1)  
  93.                 {  
  94.                     string strTemp = msg.Substring(index1 + 6, index2 - index1 - 6);  
  95.                     if (textBox7.Text.Trim().CompareTo(strTemp) > 0)  
  96.                     {  
  97.                         textBox7.Enabled = true;  
  98.                         textBox7.Text = strTemp.Trim();  
  99.                         textBox7.Enabled = false;  
  100.                     }  
  101.                 }  
  102.                 textBox6.Text = msg;  
  103.             }  
  104.             //if (msg != "")  
  105.             //{  
  106.             //    //string telnum = DecodeTelnum(msg);  
  107.             //    //string msgDestination = DecodeMessage(msg);  
  108.             //    //textBox1.Text = msgDestination;  
  109.             //    //if (textBox1.Text.Trim().Equals(""))  
  110.             //    //    textBox1.Text = msg;  
  111.             //    //else  
  112.             //    //    textBox1.Text = textBox1.Text + Environment.NewLine + " " + msg;  
  113.             //    //button1.Enabled = true;  
  114.             //}  
  115.             //if (msg.Length > 5)  
  116.             //    if (msg.Substring(0, 5).Equals("+CMTI"))  
  117.             //    {  
  118.             //        numberRead = msg.Substring(msg.LastIndexOf(',') + 1);  
  119.             //    }  
  120.         }  
  121.         /// <summary>  
  122.         /// 将电话号码奇偶位对换进行编码  
  123.         /// </summary>  
  124.         /// <param name="str"></param>  
  125.         /// <returns></returns>  
  126.         private string EncodeTelnum(string str)  
  127.         {  
  128.             //源电话号码  
  129.             string strSource = "86" + str;  
  130.             if (strSource.Length % 2 == 1)//如果为奇数,补F  
  131.                 strSource += "F";  
  132.             //变换号码  
  133.             char[] strchar = strSource.ToCharArray();  
  134.             char temp;  
  135.             if (strchar.Length % 2 == 1)  
  136.                 MessageBox.Show("error", "Error");  
  137.             else  
  138.             {  
  139.                 for (int i = 0; i < strchar.Lengthi = i + 2)  
  140.                 {  
  141.                     temp = strchar[i];  
  142.                     strchar[i] = strchar[i + 1];  
  143.                     strchar[i + 1] = temp;  
  144.                 }  
  145.             }  
  146.             string strDestination = new string(strchar);  
  147.             return strDestination;  
  148.    
  149.         }  
  150.         /// <summary>  
  151.         /// 中文英文混合编码  
  152.         /// </summary>  
  153.         /// <param name="str"></param>  
  154.         /// <returns></returns>  
  155.         private string EncodeUnicode(string str)  
  156.         {  
  157.             byte[] tmpSmsText = Encoding.Unicode.GetBytes(str);//转换成UTF-16编码  
  158.             string strDestination = Convert.ToString(tmpSmsText.Length, 16);//将长度表示为16进制数(以字符串形式)  
  159.             if (strDestination.Length == 1)  
  160.                 strDestination = "0" + strDestination;  
  161.             else  
  162.                 if (strDestination.Length == 0)  
  163.                     strDestination = "00";  
  164.             for (int i = 0; i < tmpSmsText.Length; i += 2) //高低字节对调   
  165.             {  
  166.                 string strTemp = Convert.ToString(tmpSmsText[i + 1], 16);  
  167.                 if (strTemp.Length == 1)  
  168.                     strTemp = "0" + strTemp;  
  169.                 else  
  170.                     if (strTemp.Length == 0)  
  171.                         strTemp = "00";  
  172.                 strDestination += strTemp;  
  173.                 strTemp = Convert.ToString(tmpSmsText[i], 16);  
  174.                 if (strTemp.Length == 1)  
  175.                     strTemp = "0" + strTemp;  
  176.                 else  
  177.                     if (strTemp.Length == 0)  
  178.                         strTemp = "00";  
  179.                 strDestination += strTemp;  
  180.             }  
  181.             return strDestination;  
  182.         }  
  183.         /// <summary>  
  184.         /// 汉字PDU编码  
  185.         /// </summary>  
  186.         /// <param name="str"></param>  
  187.         /// <returns></returns>  
  188.         private string EncodeMessage(string str)  
  189.         {  
  190.             string strDestination;  
  191.             if (str.Length * 2 < 16)  
  192.                 strDestination = "0" + Convert.ToString(str.Length * 2, 16);  
  193.             else  
  194.                 if (str.Length * 2 < 256)  
  195.                     strDestination = Convert.ToString(str.Length * 2, 16);  
  196.                 else  
  197.                 {  
  198.                     MessageBox.Show("所发信息数据量太大", "Error");  
  199.                     return "Error";  
  200.                 }  
  201.             foreach (char item in str)  
  202.             {  
  203.                 int i = item;  
  204.                 strDestination += Convert.ToString(i, 16);  
  205.             }  
  206.             return strDestination;  
  207.         }  
  208.         /// <summary>  
  209.         /// 英文编码  
  210.         /// </summary>  
  211.         /// <param name="str"></param>  
  212.         /// <returns></returns>  
  213.         private string EncodeEnglish(string str)  
  214.         {  
  215.             string strDestination = Convert.ToString(str.Length, 16);  
  216.             if (str.Length < 16)  
  217.                 strDestination = "0" + strDestination;  
  218.    
  219.             for (int i = 0; i < str.Length; i++)  
  220.             {  
  221.                 if (i < (str.Length - 1))  
  222.                 {  
  223.                     if (i % 8 != 7)  
  224.                         strDestination += EncodeLetter(str[i], str[i + 1], i % 8);  
  225.                     else  
  226.                         continue;  
  227.                 }  
  228.                 else  
  229.                     strDestination += EncodeLetter(str[i], '\0', i % 8);  
  230.    
  231.             }  
  232.             return strDestination;  
  233.    
  234.         }  
  235.         /// <summary>  
  236.         /// 单个字符编码  
  237.         /// </summary>  
  238.         /// <param name="a1"></param>  
  239.         /// <param name="a2"></param>  
  240.         /// <param name="i"></param>  
  241.         /// <returns></returns>  
  242.         private string EncodeLetter(char a1, char a2, int i)  
  243.         {  
  244.             string stra1 = Convert.ToString(a1, 2);  
  245.             char[] strChara1 = new char[8];  
  246.             int j = 0k = 0;  
  247.             //赋值  
  248.             for (j = stra1.Length - 1, k = 0; k < 8; j--)  
  249.             {  
  250.                 if (j >= 0)  
  251.                     strChara1[k++] = stra1[j];  
  252.                 else  
  253.                     strChara1[k++] = '0';  
  254.             }  
  255.             //右移i位  
  256.    
  257.             for (j = 0k = i; j < 8; )  
  258.             {  
  259.                 if (k < 8)  
  260.                     strChara1[j++] = strChara1[k++];  
  261.                 else  
  262.                     strChara1[j++] = '0';//高位用0补足  
  263.             }  
  264.    
  265.             char[] strChara2 = new char[8];  
  266.             //赋值  
  267.             if (a2 == '\0')  
  268.             {  
  269.                 for (int m = 0; m < 8; m++)  
  270.                     strChara2[m] = '0';  
  271.             }  
  272.             else  
  273.             {  
  274.                 string stra2 = Convert.ToString(a2, 2);  
  275.                 for (j = stra2.Length - 1, k = 0; k < 8; j--)  
  276.                 {  
  277.                     if (j >= 0)  
  278.                         strChara2[k++] = stra2[j];  
  279.                     else  
  280.                         strChara2[k++] = '0';  
  281.                 }  
  282.             }  
  283.             for (int s = 0; s < i + 1; s++)  
  284.             {  
  285.                 strChara1[7 - i + s] = strChara2[s];  
  286.             }  
  287.             strChara1 = Inversion(strChara1);  
  288.             stra1 = new string(strChara1);  
  289.             int inta1 = Convert.ToInt32(stra1, 2);  
  290.             string strDestion = Convert.ToString(inta1, 16);  
  291.             if (strDestion.Length == 1)  
  292.                 strDestion = "0" + strDestion;  
  293.             else  
  294.                 if (strDestion.Length == 0)  
  295.                     strDestion = "00";  
  296.             return strDestion;  
  297.         }  
  298.         /// <summary>  
  299.         /// 逆置字符数组  
  300.         /// </summary>  
  301.         /// <param name="a1"></param>  
  302.         /// <returns></returns>  
  303.         private char[] Inversion(char[] a1)  
  304.         {  
  305.             char[] a2 = new char[a1.Length];  
  306.             for (int i = 0j = a1.Length - 1; i < a1.Length; i++, j--)  
  307.             {  
  308.                 a2[j] = a1[i];  
  309.             }  
  310.             return a2;  
  311.         }  
  312.    
  313.         //关闭串口  
  314.         private void button1_Click(object sender, EventArgs e)  
  315.         {  
  316.             textBox1.Text = EncodeTelnum(textBox2.Text.Trim().ToString());  
  317.             serialPort1.Close();  
  318.             textBox1.Text = "";  
  319.             textBox4.Text = "";  
  320.             textBox5.Text = "";  
  321.             textBox6.Text = "";  
  322.             button1.Enabled = false;  
  323.             button2.Enabled = true;  
  324.             button3.Enabled = false;  
  325.             button6.Enabled = false;  
  326.             button5.Enabled = false;  
  327.             button4.Enabled = false;  
  328.             button7.Enabled = false;  
  329.             button8.Enabled = false;  
  330.             button9.Enabled = false;  
  331.             button10.Enabled = false;  
  332.             label1.Text = serialPort1.IsOpen.ToString();  
  333.         }  
  334.         //打开串口  
  335.         private void button2_Click(object sender, EventArgs e)  
  336.         {  
  337.             //serialPort1.NewLine = Environment.NewLine;  
  338.             serialPort1.BaudRate = 115200;  
  339.             serialPort1.PortName = "com1";  
  340.             try  
  341.             {  
  342.                 serialPort1.Open();  
  343.             }  
  344.             catch  
  345.             {  
  346.                 MessageBox.Show("端口已经被占用!");  
  347.                 return;  
  348.             }  
  349.             textBox1.Text = "";  
  350.             textBox1.Text = EncodeMessage(textBox3.Text);  
  351.             //serialPort1.WriteLine("at+cmgf=1");//Text模式发送英文  
  352.             serialPort1.Write("at+cmgf=0\x0a");//PDU模式发送  
  353.             //serialPort1.WriteLine("at+cnmi=2,1");//  
  354.             //int count = 14 + EncodeUnicode(textBox3.Text).Length / 2;//PDU模式混发  
  355.             //serialPort1.WriteLine("at+cmgs=" + count);//PDU模式发送  
  356.             //string message = "0011000D91" + EncodeTelnum(textBox2.Text.Trim()) + "000800" + EncodeUnicode(textBox3.Text.Trim()).ToUpper() + "\x01a";//PDU模式混发  
  357.             //serialPort1.WriteLine(message);//PDU模式发送  
  358.             label1.Text = serialPort1.IsOpen.ToString();  
  359.             button2.Enabled = false;  
  360.             button1.Enabled = true;  
  361.             button3.Enabled = true;  
  362.             button6.Enabled = true;  
  363.             button7.Enabled = true;  
  364.             button8.Enabled = true;  
  365.             button9.Enabled = true;  
  366.             button10.Enabled = true;  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多