小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

C# at命令發(fā)送&&接收短信測(cè)試程序

 昵稱10504424 2013-02-19

此文轉(zhuǎn)載,出處不明。

  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.             {  
  60.                 int number = int.Parse(msg.Substring(msg.IndexOf("\"SM\",") + 5));  
  61.                 int number7 = int.Parse(textBox7.Text);  
  62.                 textBox8.Text = msg.Substring(msg.IndexOf("\"SM\",") + 5);  
  63.                 if (number7 < number)  
  64.                 {  
  65.                     textBox7.Enabled = true;  
  66.                     textBox7.Text = number.ToString();  
  67.                     textBox7.Enabled = false;  
  68.                 }  
  69.                 return;  
  70.             }  
  71.             if (msg.Length > 33)  
  72.             {  
  73.                 string telnum = "+" + DecodeTelnum(msg);  
  74.                 string msgDestination = DecodeMessage(msg);  
  75.                 string time = DecodeTime(msg);  
  76.                 textBox1.Text = telnum;  
  77.                 textBox4.Text = msgDestination;  
  78.                 textBox5.Text = time;  
  79.             }  
  80.             else  
  81.             {  
  82.                 {  
  83.                     textBox1.Text = "";  
  84.                     textBox4.Text = "";  
  85.                     textBox5.Text = "";  
  86.                 }  
  87.                 int index1 = msg.IndexOf("+CMGL:");  
  88.                 int index2 = msg.IndexOf(",");  
  89.                 if (index1 != -1)  
  90.                 {  
  91.                     string strTemp = msg.Substring(index1 + 6, index2 - index1 - 6);  
  92.                     if (textBox7.Text.Trim().CompareTo(strTemp) > 0)  
  93.                     {  
  94.                         textBox7.Enabled = true;  
  95.                         textBox7.Text = strTemp.Trim();  
  96.                         textBox7.Enabled = false;  
  97.                     }  
  98.                 }  
  99.                 textBox6.Text = msg;  
  100.             }  
  101.             //{  
  102.             //    //string telnum = DecodeTelnum(msg);  
  103.             //    //string msgDestination = DecodeMessage(msg);  
  104.             //    //textBox1.Text = msgDestination;  
  105.             //    //    textBox1.Text = msg;  
  106.             //    //else  
  107.             //    //    textBox1.Text = textBox1.Text + Environment.NewLine + " " + msg;  
  108.             //    //button1.Enabled = true;  
  109.             //}  
  110.             //if (msg.Length > 5)  
  111.             //    {  
  112.             //        numberRead = msg.Substring(msg.LastIndexOf(',') + 1);  
  113.             //    }  
  114.         }  
  115.         /// <summary>  
  116.         /// 將電話號(hào)碼奇偶位對(duì)換進(jìn)行編碼  
  117.         /// </summary>  
  118.         /// <param name="str"></param>  
  119.         /// <returns></returns>  
  120.         private string EncodeTelnum(string str)  
  121.         {  
  122.             //源電話號(hào)碼  
  123.             string strSource = "86" + str;  
  124.             if (strSource.Length % 2 == 1)//如果為奇數(shù),補(bǔ)F  
  125.             //變換號(hào)碼  
  126.             char[] strchar = strSource.ToCharArray();  
  127.             char temp;  
  128.             if (strchar.Length % 2 == 1)  
  129.             else  
  130.             {  
  131.                 for (int i = 0; i < strchar.Lengthi = i + 2)  
  132.                 {  
  133.                     temp = strchar[i];  
  134.                     strchar[i] = strchar[i + 1];  
  135.                     strchar[i + 1] = temp;  
  136.                 }  
  137.             }  
  138.             string strDestination = new string(strchar);  
  139.             return strDestination;  
  140.    
  141.         }  
  142.         /// <summary>  
  143.         /// 中文英文混合編碼  
  144.         /// </summary>  
  145.         /// <param name="str"></param>  
  146.         /// <returns></returns>  
  147.         private string EncodeUnicode(string str)  
  148.         {  
  149.             byte[] tmpSmsText = Encoding.Unicode.GetBytes(str);//轉(zhuǎn)換成UTF-16編碼  
  150.             string strDestination = Convert.ToString(tmpSmsText.Length, 16);//將長(zhǎng)度表示為16進(jìn)制數(shù)(以字符串形式)  
  151.             if (strDestination.Length == 1)  
  152.                 strDestination = "0" + strDestination;  
  153.             else  
  154.                 if (strDestination.Length == 0)  
  155.                     strDestination = "00";  
  156.             for (int i = 0; i < tmpSmsText.Length; i += 2) //高低字節(jié)對(duì)調(diào)   
  157.             {  
  158.                 string strTemp = Convert.ToString(tmpSmsText[i + 1], 16);  
  159.                 if (strTemp.Length == 1)  
  160.                     strTemp = "0" + strTemp;  
  161.                 else  
  162.                     if (strTemp.Length == 0)  
  163.                         strTemp = "00";  
  164.                 strDestination += strTemp;  
  165.                 strTemp = Convert.ToString(tmpSmsText[i], 16);  
  166.                 if (strTemp.Length == 1)  
  167.                     strTemp = "0" + strTemp;  
  168.                 else  
  169.                     if (strTemp.Length == 0)  
  170.                         strTemp = "00";  
  171.                 strDestination += strTemp;  
  172.             }  
  173.             return strDestination;  
  174.         }  
  175.         /// <summary>  
  176.         /// 漢字PDU編碼  
  177.         /// </summary>  
  178.         /// <param name="str"></param>  
  179.         /// <returns></returns>  
  180.         private string EncodeMessage(string str)  
  181.         {  
  182.             string strDestination;  
  183.             if (str.Length * 2 < 16)  
  184.                 strDestination = "0" + Convert.ToString(str.Length * 2, 16);  
  185.             else  
  186.                 if (str.Length * 2 < 256)  
  187.                     strDestination = Convert.ToString(str.Length * 2, 16);  
  188.                 else  
  189.                 {  
  190.                 }  
  191.             foreach (char item in str)  
  192.             {  
  193.                 int i = item;  
  194.                 strDestination += Convert.ToString(i, 16);  
  195.             }  
  196.             return strDestination;  
  197.         }  
  198.         /// <summary>  
  199.         /// 英文編碼  
  200.         /// </summary>  
  201.         /// <param name="str"></param>  
  202.         /// <returns></returns>  
  203.         private string EncodeEnglish(string str)  
  204.         {  
  205.             string strDestination = Convert.ToString(str.Length, 16);  
  206.             if (str.Length < 16)  
  207.                 strDestination = "0" + strDestination;  
  208.    
  209.             for (int i = 0; i < str.Length; i++)  
  210.             {  
  211.                 if (i < (str.Length - 1))  
  212.                 {  
  213.                     if (i % 8 != 7)  
  214.                         strDestination += EncodeLetter(str[i], str[i + 1], i % 8);  
  215.                     else  
  216.                         continue;  
  217.                 }  
  218.                 else  
  219.    
  220.             }  
  221.             return strDestination;  
  222.    
  223.         }  
  224.         /// <summary>  
  225.         /// 單個(gè)字符編碼  
  226.         /// </summary>  
  227.         /// <param name="a1"></param>  
  228.         /// <param name="a2"></param>  
  229.         /// <param name="i"></param>  
  230.         /// <returns></returns>  
  231.         private string EncodeLetter(char a1, char a2, int i)  
  232.         {  
  233.             string stra1 = Convert.ToString(a1, 2);  
  234.             char[] strChara1 = new char[8];  
  235.             int j = 0k = 0;  
  236.             //賦值  
  237.             for (j = stra1.Length - 1, k = 0; k < 8; j--)  
  238.             {  
  239.                 if (j >= 0)  
  240.                     strChara1[k++] = stra1[j];  
  241.                 else  
  242.             }  
  243.             //右移i位  
  244.    
  245.             for (j = 0k = i; j < 8; )  
  246.             {  
  247.                 if (k < 8)  
  248.                     strChara1[j++] = strChara1[k++];  
  249.                 else  
  250.             }  
  251.    
  252.             char[] strChara2 = new char[8];  
  253.             //賦值  
  254.             if (a2 == '\0')  
  255.             {  
  256.                 for (int m = 0; m < 8; m++)  
  257.             }  
  258.             else  
  259.             {  
  260.                 string stra2 = Convert.ToString(a2, 2);  
  261.                 for (j = stra2.Length - 1, k = 0; k < 8; j--)  
  262.                 {  
  263.                     if (j >= 0)  
  264.                         strChara2[k++] = stra2[j];  
  265.                     else  
  266.                 }  
  267.             }  
  268.             for (int s = 0; s < i + 1; s++)  
  269.             {  
  270.                 strChara1[7 - i + s] = strChara2[s];  
  271.             }  
  272.             strChara1 = Inversion(strChara1);  
  273.             stra1 = new string(strChara1);  
  274.             int inta1 = Convert.ToInt32(stra1, 2);  
  275.             string strDestion = Convert.ToString(inta1, 16);  
  276.             if (strDestion.Length == 1)  
  277.                 strDestion = "0" + strDestion;  
  278.             else  
  279.                 if (strDestion.Length == 0)  
  280.                     strDestion = "00";  
  281.             return strDestion;  
  282.         }  
  283.         /// <summary>  
  284.         /// 逆置字符數(shù)組  
  285.         /// </summary>  
  286.         /// <param name="a1"></param>  
  287.         /// <returns></returns>  
  288.         private char[] Inversion(char[] a1)  
  289.         {  
  290.             char[] a2 = new char[a1.Length];  
  291.             for (int i = 0j = a1.Length - 1; i < a1.Length; i++, j--)  
  292.             {  
  293.                 a2[j] = a1[i];  
  294.             }  
  295.             return a2;  
  296.         }  
  297.    
  298.         //關(guān)閉串口  
  299.         private void button1_Click(object sender, EventArgs e)  
  300.         {  
  301.             textBox1.Text = EncodeTelnum(textBox2.Text.Trim().ToString());  
  302.             serialPort1.Close();  
  303.             textBox1.Text = "";  
  304.             textBox4.Text = "";  
  305.             textBox5.Text = "";  
  306.             textBox6.Text = "";  
  307.             button1.Enabled = false;  
  308.             button2.Enabled = true;  
  309.             button3.Enabled = false;  
  310.             button6.Enabled = false;  
  311.             button5.Enabled = false;  
  312.             button4.Enabled = false;  
  313.             button7.Enabled = false;  
  314.             button8.Enabled = false;  
  315.             button9.Enabled = false;  
  316.             button10.Enabled = false;  
  317.             label1.Text = serialPort1.IsOpen.ToString();  
  318.         }  
  319.         //打開串口  
  320.         private void button2_Click(object sender, EventArgs e)  
  321.         {  
  322.             //serialPort1.NewLine = Environment.NewLine;  
  323.             serialPort1.BaudRate = 115200;  
  324.             serialPort1.PortName = "com1";  
  325.             try  
  326.             {  
  327.                 serialPort1.Open();  
  328.             }  
  329.             catch  
  330.             {  
  331.                 return;  
  332.             }  
  333.             textBox1.Text = "";  
  334.             textBox1.Text = EncodeMessage(textBox3.Text);  
  335. cmgf=1");//Text模式發(fā)送英文  
  336. cmgf=0\x0a");//PDU模式發(fā)送  
  337. cnmi=2,1");//  
  338.             //int count = 14 + EncodeUnicode(textBox3.Text).Length / 2;//PDU模式混發(fā)  
  339. cmgs=" + count);//PDU模式發(fā)送  
  340.             //string message = "0011000D91" + EncodeTelnum(textBox2.Text.Trim()) + "000800" + EncodeUnicode(textBox3.Text.Trim()).ToUpper() + "\x01a";//PDU模式混發(fā)  
  341.             //serialPort1.WriteLine(message);//PDU模式發(fā)送  
  342.             label1.Text = serialPort1.IsOpen.ToString();  
  343.             button2.Enabled = false;  
  344.             button1.Enabled = true;  
  345.             button3.Enabled = true;  
  346.             button6.Enabled = true;  
  347.             button7.Enabled = true;  
  348.             button8.Enabled = true;  
  349.             button9.Enabled = true;  
  350.             button10.Enabled = true;  

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多