乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      分享一個GSM短信貓接口程序

       筆錄收藏 2013-04-17

      GSM短信貓編程的難點在與“程序與GSM”通訊是異步的。

                例如:

                  AT+CSQ 查詢信號指令,PC通過RS232串口發(fā)送后,GSM短信貓要等一小會才回答,而且還可能不回答。

      這樣一來就導致編程復雜了。

      下面的程序采用了串口中斷接收、定時器、線程的方式配合。實現(xiàn)了接收短信,并把接收到的短信存在數(shù)據(jù)庫中。然后定期發(fā)送短信

      1. using System;  
      2. using System.Collections.Generic;  
      3. using System.Linq;  
      4. using System.Text;  
      5. using System.Threading;  
      6. using System.Windows.Forms;  
      7.   
      8. namespace SMS  
      9. {  
      10.     public class GSMModem  
      11.     {  
      12.   
      13.         private string protName;  
      14.         private int BaudRate;  
      15.         private System.Collections.ArrayList alRecevingSM;// 短信接收 緩沖區(qū)   
      16.         private System.Collections.ArrayList alSendSM;    // 短信【待】發(fā)送 緩沖區(qū)   
      17.   
      18.         private System.Collections.ArrayList alAuthorizationNumber; //授權(quán)號碼,系統(tǒng)只會回復授權(quán)號碼   
      19.   
      20.         int WORK_STATE;//短信貓工作狀態(tài)     
      21.   
      22.         //錯誤的短信數(shù),主要是未經(jīng)授權(quán)的號碼發(fā)來的短信   
      23.         private int ErrorSMCount = 0;  
      24.         public int _ErrorSMCount  
      25.         {  
      26.             get  
      27.             {  
      28.                 return ErrorSMCount;  
      29.             }  
      30.         }  
      31.         /// <summary>   
      32.         /// 0=自檢   
      33.         /// 1=接收   
      34.         /// 2=發(fā)送   
      35.         /// </summary>   
      36.         public int _WORK_STATE  
      37.         {  
      38.             get  
      39.             {  
      40.                 return WORK_STATE;  
      41.             }  
      42.         }  
      43.   
      44.   
      45.         //接收和發(fā)送定時器不能同時工作,切記   
      46.   
      47.         //短信接收 輪詢 定時器,定時發(fā)送 AT+CGMR={0}   
      48.         System.Windows.Forms.Timer revTimer = new System.Windows.Forms.Timer();  
      49.   
      50.         //短信發(fā)送 隊列 定時器,定時發(fā)送 AT+CMGS={0}   
      51.         System.Windows.Forms.Timer sendTimer = new System.Windows.Forms.Timer();  
      52.   
      53.         //短信貓自檢   
      54.         System.Windows.Forms.Timer selfCheckingTimer = new System.Windows.Forms.Timer();  
      55.   
      56.   
      57.         //短信貓連接設(shè)備端口   
      58.         System.IO.Ports.SerialPort Port = new System.IO.Ports.SerialPort();  
      59.   
      60.         //短信貓串口接收緩沖   
      61.         /*備注:該緩沖變量由串口DataReceived事件負責接收,具體的處理過程由 
      62.          *     Timer 定時器內(nèi)的代碼具體執(zhí)行 
      63.          */  
      64.         string temp_gsm_str_buf = "";  
      65.   
      66.         //   
      67.         int CMGD_MAX_ID = 0;//短信卡最大SM容量,通常情況下設(shè)置為25   
      68.   
      69.         int CMGD_ID = 1;    //當前串口發(fā)送讀取的短信ID號   
      70.   
      71.         int GSM_RST_ACT = 0;  
      72.         int GSM_MAX_RST_ACT = 8;  
      73.   
      74.         int CGMS_Count = 0; //短信發(fā)送數(shù)量   
      75.   
      76.         public int _CGMS_Count  
      77.         {  
      78.             get  
      79.             {  
      80.                 return CGMS_Count;  
      81.             }  
      82.         }  
      83.         public int _CMGD_MAX_ID  
      84.         {  
      85.             get  
      86.             {  
      87.                 return CMGD_MAX_ID;  
      88.             }  
      89.         }  
      90.   
      91.         public int _CMGD_ID  
      92.         {  
      93.             get  
      94.             {  
      95.                 return CMGD_ID;  
      96.             }  
      97.         }  
      98.   
      99.         public int _GSM_RST_ACT  
      100.         {  
      101.             get  
      102.             {  
      103.                 return GSM_RST_ACT;  
      104.             }  
      105.         }  
      106.   
      107.         public int _GSM_MAX_RST_ACT  
      108.         {  
      109.             get  
      110.             {  
      111.                 return GSM_MAX_RST_ACT;  
      112.             }  
      113.         }  
      114.  
      115.         #region 短信貓工作狀態(tài)   
      116.   
      117.         string INF_GSM_CSQ;  
      118.         string INF_GSM_CSCS;  
      119.         string INF_GSM_CSCA;  
      120.         string INF_GSM_CNMI;  
      121.         bool INF_GSM_State;  
      122.   
      123.         public string _INF_GSM_CSQ  
      124.         {  
      125.             get  
      126.             {  
      127.                 return INF_GSM_CSQ;  
      128.             }  
      129.         }  
      130.   
      131.         public string _INF_GSM_CSCS  
      132.         {  
      133.             get  
      134.             {  
      135.                 return INF_GSM_CSCS;  
      136.             }  
      137.         }  
      138.   
      139.         public string _INF_GSM_CSCA  
      140.         {  
      141.             get  
      142.             {  
      143.                 return INF_GSM_CSCA;  
      144.             }  
      145.         }  
      146.   
      147.         public string _INF_GSM_CNMI  
      148.         {  
      149.             get  
      150.             {  
      151.                 return INF_GSM_CNMI;  
      152.             }  
      153.         }  
      154.   
      155.         public bool _INF_GSM_State  
      156.         {  
      157.             get  
      158.             {  
      159.                 return INF_GSM_State;  
      160.             }  
      161.         }  
      162.         #endregion   
      163.   
      164.   
      165.         private int _CmgsSleepTime;  
      166.         public GSMModem(string protName,  
      167.             string BaudRate,   
      168.             int CMGD_MAX_ID,   
      169.             System.Collections.ArrayList RecevingSM,  
      170.             System.Collections.ArrayList SendSM,  
      171.             int revTimerInterval,  
      172.             int sendTimerInterval,  
      173.             int selfCheckingTimerInterval,  
      174.             int CmgsSleepTime,  
      175.            System.Collections.ArrayList alAuthorizationNumber  
      176.             )  
      177.         {  
      178.             _CmgsSleepTime = CmgsSleepTime;//AT+CMGS 指令發(fā)送后,稍微延遲一定時間,讓短信貓做出反應   
      179.   
      180.             this.alAuthorizationNumber = alAuthorizationNumber;//保存授權(quán)號碼   
      181.   
      182.             this.CMGD_MAX_ID = CMGD_MAX_ID;  
      183.   
      184.             this.protName = protName;  
      185.             this.BaudRate = int.Parse(BaudRate);  
      186.             this.alRecevingSM = RecevingSM;  
      187.             this.alSendSM = SendSM;  
      188.   
      189.   
      190.             selfCheckingTimer.Enabled = false;  
      191.   
      192.             revTimer.Interval = revTimerInterval;  
      193.             revTimer.Enabled = false;  
      194.   
      195.             sendTimer.Interval = sendTimerInterval;  
      196.             sendTimer.Enabled = false;  
      197.   
      198.             revTimer.Tick += revTimer_Tick; //掛接短信輪詢 定時器   
      199.             sendTimer.Tick += sendTimer_Tick;  
      200.   
      201.             selfCheckingTimer.Interval = selfCheckingTimerInterval;  
      202.             selfCheckingTimer.Tick += selfCheckingTimer_Tick;  
      203.   
      204.             Port.DataReceived += Port_DataReceived;  
      205.         }  
      206.   
      207.         public bool Start()  
      208.         {  
      209.             try  
      210.             {  
      211.                 Port.PortName = protName;  
      212.                 Port.BaudRate = BaudRate;  
      213.                 Port.Open();//打開串口   
      214.                 //短信貓 先自檢,自檢成功了,才能進行 接收 或 發(fā)送   
      215.                 selfCheckingTimer.Enabled = true;  
      216.   
      217.             }  
      218.             catch (Exception ex)  
      219.             {  
      220.                 MessageBox.Show(ex.Message);  
      221.                 return false;  
      222.             }  
      223.             return false;  
      224.         }  
      225.   
      226.         public bool Stop()  
      227.         {  
      228.   
      229.             sendTimer.Enabled = false;  
      230.             revTimer.Enabled = false;  
      231.             selfCheckingTimer.Enabled = false;  
      232.             Port.Close();  
      233.             CMGD_ID = 1;  
      234.             alRecevingSM.Clear();  
      235.             alSendSM.Clear();  
      236.             ErrorSMCount = 0;  
      237.             return true;  
      238.         }  
      239.   
      240.         void Port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)  
      241.         {  
      242.             System.IO.Ports.SerialPort serialPort = Port;  
      243.   
      244.             int len = serialPort.BytesToRead;  
      245.             byte[] bs = new byte[len];  
      246.             serialPort.Read(bs, 0, len);  
      247.             if (len == 0)  
      248.             {  
      249.                 return;  
      250.             }  
      251.             else  
      252.             {  
      253.                 //接收處理   
      254.                 string r = Encoding.ASCII.GetString(bs, 0, len);  
      255.                 temp_gsm_str_buf += r;  
      256.                 //接收數(shù)據(jù)處理                   
      257.                 //txtGsmRevLog.Invoke(new EventHandler(DoUpdate), r);  //線程處理   
      258.                 Console.Write(r);  
      259.             }  
      260.         }  
      261.   
      262.   
      263.         private bool AuthorizationPhoneNumber(string phoneID)  
      264.         {  
      265.             for (int i = 0; i < alAuthorizationNumber.Count; i++)  
      266.             {  
      267.                 string s = alAuthorizationNumber[i].ToString();  
      268.                 if (phoneID.Equals(s))   
      269.                     return true;              
      270.             }  
      271.   
      272.             return false;  
      273.   
      274.         }  
      275.         bool DoGsmDataReceived()  
      276.         {  
      277.             //短信貓發(fā)回數(shù)據(jù)處理   
      278.             string str = temp_gsm_str_buf;  
      279.             if (str.Trim().StartsWith("+CMGR"))  
      280.             {  
      281.                 int P1 = str.IndexOf("0891");  
      282.                 if (P1 != -1)  
      283.                 {  
      284.                     str = str.Substring(P1);  
      285.                     int P2 = str.IndexOf("\r\n");  
      286.                     if (P2 != -1)  
      287.                     {  
      288.                         string uPDU = str.Substring(0, P2);  
      289.                         string decMsg = PDUDecoding.DecodingMsg(uPDU);  
      290.                         string[] content = decMsg.Split(',');  
      291.                         string stime = content[1].ToString();  
      292.                         string sphoneId = content[0].ToString().Replace("+86""");  
      293.                         string sgsmtxt = content[2].ToString();  
      294.                         //object[] row = { stime, sphoneId, sgsmtxt };   
      295.   
      296.                         if (AuthorizationPhoneNumber(sphoneId))  
      297.                         {  
      298.                             SMDATA sm = new SMDATA();  
      299.                             sm.sTime = stime;  
      300.                             sm.sPoneID = sphoneId;  
      301.                             sm.sContent = sgsmtxt;  
      302.                             sm.pdu = uPDU;  
      303.                             sm.sType = "Admin";  
      304.                             alRecevingSM.Add(sm);  
      305.   
      306.   
      307.                         }  
      308.                         else  
      309.                         {  
      310.                             //未經(jīng)授權(quán)的用戶,10086短信,廣告信息,垃圾短信,用戶手機號碼   
      311.                             string[] gsmCmd = sgsmtxt.Trim().Split('+');  
      312.   
      313.                             string cmd = gsmCmd[0].ToString();  
      314.                             if (cmd == "CX")  
      315.                             {  
      316.                                 if (gsmCmd.Length == 1)  
      317.                                 {  
      318.                                     //僅限 用戶手機號碼   
      319.   
      320.                                     SMDATA sm = new SMDATA();  
      321.                                     sm.sTime = stime;  
      322.                                     sm.sPoneID = sphoneId;  
      323.                                     sm.sContent = "CX";  //讓BI查詢車輛信息   
      324.                                     sm.pdu = uPDU;  
      325.                                     sm.sType = "User";  
      326.                                     alRecevingSM.Add(sm);  
      327.                                 }  
      328.                             }  
      329.                             else  
      330.                             {  
      331.                                 Console.WriteLine("Error:" + stime + " " + sphoneId + " " + sgsmtxt);  
      332.                                 ErrorSMCount++;  
      333.                             }  
      334.                         }  
      335.   
      336.                         return true;  
      337.                     }  
      338.                 }  
      339.             }  
      340.             else  
      341.             {  
      342.   
      343.             }  
      344.   
      345.             return false;  
      346.         }  
      347.   
      348.   
      349.   
      350.         bool DoGsmDataSend()  
      351.         {  
      352.             //短信貓發(fā)回數(shù)據(jù)處理   
      353.             string str = temp_gsm_str_buf;  
      354.             if (str.Trim().IndexOf("+CMGS") > -1)  
      355.             {  
      356.                 temp_gsm_str_buf = "";  
      357.                 CGMS_Count++;  
      358.   
      359.                 return true;  
      360.             }  
      361.             else  
      362.             {  
      363.                 //接收到錯誤信息,可能是短信發(fā)送失敗   
      364.             }  
      365.             temp_gsm_str_buf = "";  
      366.             return false;  
      367.         }  
      368.   
      369.         //短信發(fā)送 指令發(fā)送定時器   
      370.         int SendID;  
      371.         int AT_CMGS_LEN;  
      372.   
      373.         void sendTimer_Tick(object sender, EventArgs e)  
      374.         {  
      375.             WORK_STATE = 2;//設(shè)置短信貓工作模式為 發(fā)送   
      376.             if (DoGsmDataSend())  
      377.             {  
      378.                 alSendSM.RemoveAt(SendID);  
      379.             }  
      380.   
      381.             //沒有發(fā)送的短信了   
      382.             if (alSendSM.Count == 0)  
      383.             {  
      384.                 CMGD_ID = 1;  
      385.                 sendTimer.Enabled = false//關(guān)閉發(fā)送   
      386.                 revTimer.Enabled = true;   //打開接收   
      387.             }  
      388.             else  
      389.             {  
      390.                 SendID = 0;  
      391.   
      392.                 SMDATA sendSM = (SMDATA)alSendSM[SendID];  
      393.                 string sPoneID = sendSM.sPoneID;  
      394.                 string sContent = sendSM.sContent;  
      395.   
      396.                 string msg = sContent;  
      397.                 string pdu = PDUDecoding.GetPDUMsg(sPoneID, msg);  
      398.                 AT_CMGS_LEN = PDUDecoding.getLenght(msg);  
      399.                 GsmWrite(string.Format("AT+CMGS={0}\r", AT_CMGS_LEN.ToString()));                  
      400.                 Thread.Sleep(_CmgsSleepTime);  //500延時,如果短信貓反應慢,設(shè)置為1000   
      401.                 GsmWrite(pdu + "\r");  
      402.   
      403.             }  
      404.   
      405.         }  
      406.   
      407.         //短信輪詢 指令發(fā)送定時器   
      408.         void revTimer_Tick(object sender, EventArgs e)  
      409.         {  
      410.             //定時器每中斷1次,接收1條指令   
      411.   
      412.             WORK_STATE = 1;//設(shè)置短信貓工作模式為 接收             
      413.   
      414.             if (DoGsmDataReceived())  
      415.             {  
      416.                 int old_CMGD_ID = CMGD_ID - 1;  
      417.                 GsmWrite("AT+CMGD=" + old_CMGD_ID.ToString() + "\r");  
      418.                 Thread.Sleep(1000);  
      419.             }  
      420.   
      421.             if (CMGD_ID > CMGD_MAX_ID)  
      422.             {  
      423.                 revTimer.Enabled = false;//停止接收   
      424.                 sendTimer.Enabled = true;//啟動短信發(fā)送                 
      425.             }  
      426.             else  
      427.             {                
      428.   
      429.                 //再讀取下一條             
      430.                 temp_gsm_str_buf = "";                
      431.   
      432.                 GsmWrite("AT+CMGR=" + CMGD_ID.ToString() + "\r");  
      433.   
      434.                 CMGD_ID++;  
      435.   
      436.             }  
      437.         }  
      438.  
      439.  
      440.  
      441.  
      442.  
      443.         #region 短信貓初始信息   
      444.   
      445.   
      446.         private void selfCheckingTimer_Tick(object sender, EventArgs e)  
      447.         {  
      448.             //先處理串口接收的數(shù)據(jù)   
      449.             //Console.WriteLine(temp_gsm_str_buf);   
      450.   
      451.             WORK_STATE = 0;  //設(shè)置工作狀態(tài)為自檢   
      452.   
      453.             string str = "";  
      454.             if (temp_gsm_str_buf.Length > 0)  
      455.             {  
      456.   
      457.                 int CSQ = temp_gsm_str_buf.IndexOf("+CSQ");  
      458.   
      459.                 if (CSQ > -1)  
      460.                 {  
      461.                     str = temp_gsm_str_buf.Substring(CSQ);  
      462.                     string[] content = str.Trim().Split(':');  
      463.                     if (content.Length > 1)  
      464.                     {  
      465.                         INF_GSM_CSQ = content[1].ToString().Replace("OK""").Replace("\r\n""").Replace("\"""");  
      466.   
      467.                         GSM_RST_ACT++;  
      468.                     }  
      469.                     temp_gsm_str_buf = "";  
      470.                 }  
      471.   
      472.                 int CSCS = temp_gsm_str_buf.IndexOf("+CSCS");  
      473.                 if (CSCS > -1)  
      474.                 {  
      475.                     str = temp_gsm_str_buf.Substring(CSCS);  
      476.                     string[] content = str.Trim().Split(':');  
      477.                     if (content.Length > 1)  
      478.                     {  
      479.                         INF_GSM_CSCS = content[1].ToString().Replace("OK""").Replace("\r\n""").Replace("\"""");  
      480.                         GSM_RST_ACT++;  
      481.                     }  
      482.                     temp_gsm_str_buf = "";  
      483.                 }  
      484.   
      485.                 int CSCA = temp_gsm_str_buf.IndexOf("+CSCA");  
      486.                 if (CSCA > -1)  
      487.                 {  
      488.                     str = temp_gsm_str_buf.Substring(CSCA);  
      489.                     string[] content = str.Trim().Split(':');  
      490.                     if (content.Length > 1)  
      491.                     {  
      492.                         INF_GSM_CSCA = content[1].ToString().Replace("OK""").Replace("\r\n""").Replace("\"""");  
      493.                         GSM_RST_ACT++;  
      494.                     }  
      495.                     temp_gsm_str_buf = "";  
      496.                 }  
      497.   
      498.   
      499.                 int CNMI = temp_gsm_str_buf.IndexOf("+CNMI");  
      500.                 if (CNMI > -1)  
      501.                 {  
      502.                     str = temp_gsm_str_buf.Substring(CNMI);  
      503.                     string[] content = str.Trim().Split(':');  
      504.                     if (content.Length > 1)  
      505.                     {  
      506.                         INF_GSM_CNMI = content[1].ToString().Replace("OK""").Replace("\r\n""");  
      507.                         GSM_RST_ACT++;  
      508.                     }  
      509.                     temp_gsm_str_buf = "";  
      510.                 }  
      511.   
      512.             }  
      513.             //   
      514.             switch (GSM_RST_ACT)  
      515.             {  
      516.                 case 0: { GsmWrite("ATE0\r"); GSM_RST_ACT++; break; }  
      517.                 case 1: { GsmWrite("AT+CSQ\r"); break; }  
      518.                 case 2: { GsmWrite("AT+CMGF=1\r"); GSM_RST_ACT++; break; }  
      519.                 case 3: { GsmWrite("AT+CSCS?\r"); break; }  
      520.                 case 4: { GsmWrite("AT+CSCA?\r"); break; }  
      521.                 case 5: { GsmWrite("AT+CMGF=0\r"); GSM_RST_ACT++; break; }  
      522.   
      523.                 //AT+CNMI=0,0,0,0   
      524.                 //case 6: { GsmWrite("AT+CNMI=3,1,0,0\r"); GSM_ACT++; break; }   
      525.                 case 6: { GsmWrite("AT+CNMI=0,0,0,0\r"); GSM_RST_ACT++; break; }  
      526.   
      527.                 case 7: { GsmWrite("AT+CNMI?\r"); break; }  
      528.                 case 8:  
      529.                     {  
      530.                         INF_GSM_State = true;  
      531.                         selfCheckingTimer.Enabled = false;  
      532.   
      533.                         revTimer.Enabled = true;//啟動 接收   
      534.   
      535.                         //btnSendTestSM.Enabled = true;   
      536.                         //checkBox1.Enabled = true;   
      537.   
      538.                         //btnSendTestSM.Enabled = true;   
      539.                         //timer_gsm_REST.Enabled = false;   
      540.                         break;  
      541.                     }  
      542.   
      543.                 default:  
      544.                     {  
      545.                         selfCheckingTimer.Enabled = false;  
      546.   
      547.   
      548.   
      549.                         //timer_gsm_REST.Enabled = false;   
      550.                         INF_GSM_State = false;  
      551.                         MessageBox.Show("短信貓自檢失敗!");  
      552.   
      553.   
      554.                         break;  
      555.                     }  
      556.             }  
      557.         }  
      558.  
      559.         #endregion   
      560.   
      561.         private void GsmWrite(string str)  
      562.         {  
      563.             Port.Write(str);  
      564.             Console.Write(str);  
      565.         }  
      566.   
      567.   
      568.         public int GetSMCount()  
      569.         {  
      570.             return alRecevingSM.Count;  
      571.         }  
      572.     }  

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

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多