GSM短信貓編程的難點(diǎn)在與“程序與GSM”通訊是異步的。
例如:
AT+CSQ
查詢信號(hào)指令,PC通過RS232串口發(fā)送后,GSM短信貓要等一小會(huì)才回答,而且還可能不回答。

這樣一來(lái)就導(dǎo)致編程復(fù)雜了。
下面的程序采用了串口中斷接收、定時(shí)器、線程的方式配合。實(shí)現(xiàn)了接收短信,并把接收到的短信存在數(shù)據(jù)庫(kù)中。然后定期發(fā)送短信 - using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Windows.Forms;
-
- namespace SMS
- {
- public class GSMModem
- {
-
- private string protName;
- private int BaudRate;
- private System.Collections.ArrayList alRecevingSM;
- private System.Collections.ArrayList alSendSM;
-
- private System.Collections.ArrayList alAuthorizationNumber;
-
- int WORK_STATE;
-
-
- private int ErrorSMCount = 0;
- public int _ErrorSMCount
- {
- get
- {
- return ErrorSMCount;
- }
- }
-
-
-
-
-
- public int _WORK_STATE
- {
- get
- {
- return WORK_STATE;
- }
- }
-
-
-
-
-
- System.Windows.Forms.Timer revTimer = new System.Windows.Forms.Timer();
-
-
- System.Windows.Forms.Timer sendTimer = new System.Windows.Forms.Timer();
-
-
- System.Windows.Forms.Timer selfCheckingTimer = new System.Windows.Forms.Timer();
-
-
-
- System.IO.Ports.SerialPort Port = new System.IO.Ports.SerialPort();
-
-
-
-
-
- string temp_gsm_str_buf = "";
-
-
- int CMGD_MAX_ID = 0;
-
- int CMGD_ID = 1;
-
- int GSM_RST_ACT = 0;
- int GSM_MAX_RST_ACT = 8;
-
- int CGMS_Count = 0;
-
- public int _CGMS_Count
- {
- get
- {
- return CGMS_Count;
- }
- }
- public int _CMGD_MAX_ID
- {
- get
- {
- return CMGD_MAX_ID;
- }
- }
-
- public int _CMGD_ID
- {
- get
- {
- return CMGD_ID;
- }
- }
-
- public int _GSM_RST_ACT
- {
- get
- {
- return GSM_RST_ACT;
- }
- }
-
- public int _GSM_MAX_RST_ACT
- {
- get
- {
- return GSM_MAX_RST_ACT;
- }
- }
-
- #region 短信貓工作狀態(tài)
-
- string INF_GSM_CSQ;
- string INF_GSM_CSCS;
- string INF_GSM_CSCA;
- string INF_GSM_CNMI;
- bool INF_GSM_State;
-
- public string _INF_GSM_CSQ
- {
- get
- {
- return INF_GSM_CSQ;
- }
- }
-
- public string _INF_GSM_CSCS
- {
- get
- {
- return INF_GSM_CSCS;
- }
- }
-
- public string _INF_GSM_CSCA
- {
- get
- {
- return INF_GSM_CSCA;
- }
- }
-
- public string _INF_GSM_CNMI
- {
- get
- {
- return INF_GSM_CNMI;
- }
- }
-
- public bool _INF_GSM_State
- {
- get
- {
- return INF_GSM_State;
- }
- }
- #endregion
-
-
- private int _CmgsSleepTime;
- public GSMModem(string protName,
- string BaudRate,
- int CMGD_MAX_ID,
- System.Collections.ArrayList RecevingSM,
- System.Collections.ArrayList SendSM,
- int revTimerInterval,
- int sendTimerInterval,
- int selfCheckingTimerInterval,
- int CmgsSleepTime,
- System.Collections.ArrayList alAuthorizationNumber
- )
- {
- _CmgsSleepTime = CmgsSleepTime;
-
- this.alAuthorizationNumber = alAuthorizationNumber;
-
- this.CMGD_MAX_ID = CMGD_MAX_ID;
-
- this.protName = protName;
- this.BaudRate = int.Parse(BaudRate);
- this.alRecevingSM = RecevingSM;
- this.alSendSM = SendSM;
-
-
- selfCheckingTimer.Enabled = false;
-
- revTimer.Interval = revTimerInterval;
- revTimer.Enabled = false;
-
- sendTimer.Interval = sendTimerInterval;
- sendTimer.Enabled = false;
-
- revTimer.Tick += revTimer_Tick;
- sendTimer.Tick += sendTimer_Tick;
-
- selfCheckingTimer.Interval = selfCheckingTimerInterval;
- selfCheckingTimer.Tick += selfCheckingTimer_Tick;
-
- Port.DataReceived += Port_DataReceived;
- }
-
- public bool Start()
- {
- try
- {
- Port.PortName = protName;
- Port.BaudRate = BaudRate;
- Port.Open();
-
- selfCheckingTimer.Enabled = true;
-
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- return false;
- }
- return false;
- }
-
- public bool Stop()
- {
-
- sendTimer.Enabled = false;
- revTimer.Enabled = false;
- selfCheckingTimer.Enabled = false;
- Port.Close();
- CMGD_ID = 1;
- alRecevingSM.Clear();
- alSendSM.Clear();
- ErrorSMCount = 0;
- return true;
- }
-
- void Port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
- {
- System.IO.Ports.SerialPort serialPort = Port;
-
- int len = serialPort.BytesToRead;
- byte[] bs = new byte[len];
- serialPort.Read(bs, 0, len);
- if (len == 0)
- {
- return;
- }
- else
- {
-
- string r = Encoding.ASCII.GetString(bs, 0, len);
- temp_gsm_str_buf += r;
-
-
- Console.Write(r);
- }
- }
-
-
- private bool AuthorizationPhoneNumber(string phoneID)
- {
- for (int i = 0; i < alAuthorizationNumber.Count; i++)
- {
- string s = alAuthorizationNumber[i].ToString();
- if (phoneID.Equals(s))
- return true;
- }
-
- return false;
-
- }
- bool DoGsmDataReceived()
- {
-
- string str = temp_gsm_str_buf;
- if (str.Trim().StartsWith("+CMGR"))
- {
- int P1 = str.IndexOf("0891");
- if (P1 != -1)
- {
- str = str.Substring(P1);
- int P2 = str.IndexOf("\r\n");
- if (P2 != -1)
- {
- string uPDU = str.Substring(0, P2);
- string decMsg = PDUDecoding.DecodingMsg(uPDU);
- string[] content = decMsg.Split(',');
- string stime = content[1].ToString();
- string sphoneId = content[0].ToString().Replace("+86", "");
- string sgsmtxt = content[2].ToString();
-
-
- if (AuthorizationPhoneNumber(sphoneId))
- {
- SMDATA sm = new SMDATA();
- sm.sTime = stime;
- sm.sPoneID = sphoneId;
- sm.sContent = sgsmtxt;
- sm.pdu = uPDU;
- sm.sType = "Admin";
- alRecevingSM.Add(sm);
-
-
- }
- else
- {
-
- string[] gsmCmd = sgsmtxt.Trim().Split('+');
-
- string cmd = gsmCmd[0].ToString();
- if (cmd == "CX")
- {
- if (gsmCmd.Length == 1)
- {
-
-
- SMDATA sm = new SMDATA();
- sm.sTime = stime;
- sm.sPoneID = sphoneId;
- sm.sContent = "CX";
- sm.pdu = uPDU;
- sm.sType = "User";
- alRecevingSM.Add(sm);
- }
- }
- else
- {
- Console.WriteLine("Error:" + stime + " " + sphoneId + " " + sgsmtxt);
- ErrorSMCount++;
- }
- }
-
- return true;
- }
- }
- }
- else
- {
-
- }
-
- return false;
- }
-
-
-
- bool DoGsmDataSend()
- {
-
- string str = temp_gsm_str_buf;
- if (str.Trim().IndexOf("+CMGS") > -1)
- {
- temp_gsm_str_buf = "";
- CGMS_Count++;
-
- return true;
- }
- else
- {
-
- }
- temp_gsm_str_buf = "";
- return false;
- }
-
-
- int SendID;
- int AT_CMGS_LEN;
-
- void sendTimer_Tick(object sender, EventArgs e)
- {
- WORK_STATE = 2;
- if (DoGsmDataSend())
- {
- alSendSM.RemoveAt(SendID);
- }
-
-
- if (alSendSM.Count == 0)
- {
- CMGD_ID = 1;
- sendTimer.Enabled = false;
- revTimer.Enabled = true;
- }
- else
- {
- SendID = 0;
-
- SMDATA sendSM = (SMDATA)alSendSM[SendID];
- string sPoneID = sendSM.sPoneID;
- string sContent = sendSM.sContent;
-
- string msg = sContent;
- string pdu = PDUDecoding.GetPDUMsg(sPoneID, msg);
- AT_CMGS_LEN = PDUDecoding.getLenght(msg);
- GsmWrite(string.Format("AT+CMGS={0}\r", AT_CMGS_LEN.ToString()));
- Thread.Sleep(_CmgsSleepTime);
- GsmWrite(pdu + "\r");
-
- }
-
- }
-
-
- void revTimer_Tick(object sender, EventArgs e)
- {
-
-
- WORK_STATE = 1;
-
- if (DoGsmDataReceived())
- {
- int old_CMGD_ID = CMGD_ID - 1;
- GsmWrite("AT+CMGD=" + old_CMGD_ID.ToString() + "\r");
- Thread.Sleep(1000);
- }
-
- if (CMGD_ID > CMGD_MAX_ID)
- {
- revTimer.Enabled = false;
- sendTimer.Enabled = true;
- }
- else
- {
-
-
- temp_gsm_str_buf = "";
-
- GsmWrite("AT+CMGR=" + CMGD_ID.ToString() + "\r");
-
- CMGD_ID++;
-
- }
- }
-
-
-
-
-
- #region 短信貓初始信息
-
-
- private void selfCheckingTimer_Tick(object sender, EventArgs e)
- {
-
-
-
- WORK_STATE = 0;
-
- string str = "";
- if (temp_gsm_str_buf.Length > 0)
- {
-
- int CSQ = temp_gsm_str_buf.IndexOf("+CSQ");
-
- if (CSQ > -1)
- {
- str = temp_gsm_str_buf.Substring(CSQ);
- string[] content = str.Trim().Split(':');
- if (content.Length > 1)
- {
- INF_GSM_CSQ = content[1].ToString().Replace("OK", "").Replace("\r\n", "").Replace("\"", "");
-
- GSM_RST_ACT++;
- }
- temp_gsm_str_buf = "";
- }
-
- int CSCS = temp_gsm_str_buf.IndexOf("+CSCS");
- if (CSCS > -1)
- {
- str = temp_gsm_str_buf.Substring(CSCS);
- string[] content = str.Trim().Split(':');
- if (content.Length > 1)
- {
- INF_GSM_CSCS = content[1].ToString().Replace("OK", "").Replace("\r\n", "").Replace("\"", "");
- GSM_RST_ACT++;
- }
- temp_gsm_str_buf = "";
- }
-
- int CSCA = temp_gsm_str_buf.IndexOf("+CSCA");
- if (CSCA > -1)
- {
- str = temp_gsm_str_buf.Substring(CSCA);
- string[] content = str.Trim().Split(':');
- if (content.Length > 1)
- {
- INF_GSM_CSCA = content[1].ToString().Replace("OK", "").Replace("\r\n", "").Replace("\"", "");
- GSM_RST_ACT++;
- }
- temp_gsm_str_buf = "";
- }
-
-
- int CNMI = temp_gsm_str_buf.IndexOf("+CNMI");
- if (CNMI > -1)
- {
- str = temp_gsm_str_buf.Substring(CNMI);
- string[] content = str.Trim().Split(':');
- if (content.Length > 1)
- {
- INF_GSM_CNMI = content[1].ToString().Replace("OK", "").Replace("\r\n", "");
- GSM_RST_ACT++;
- }
- temp_gsm_str_buf = "";
- }
-
- }
-
- switch (GSM_RST_ACT)
- {
- case 0: { GsmWrite("ATE0\r"); GSM_RST_ACT++; break; }
- case 1: { GsmWrite("AT+CSQ\r"); break; }
- case 2: { GsmWrite("AT+CMGF=1\r"); GSM_RST_ACT++; break; }
- case 3: { GsmWrite("AT+CSCS?\r"); break; }
- case 4: { GsmWrite("AT+CSCA?\r"); break; }
- case 5: { GsmWrite("AT+CMGF=0\r"); GSM_RST_ACT++; break; }
-
-
-
- case 6: { GsmWrite("AT+CNMI=0,0,0,0\r"); GSM_RST_ACT++; break; }
-
- case 7: { GsmWrite("AT+CNMI?\r"); break; }
- case 8:
- {
- INF_GSM_State = true;
- selfCheckingTimer.Enabled = false;
-
- revTimer.Enabled = true;
-
-
-
-
-
-
- break;
- }
-
- default:
- {
- selfCheckingTimer.Enabled = false;
-
-
-
-
- INF_GSM_State = false;
- MessageBox.Show("短信貓自檢失敗!");
-
-
- break;
- }
- }
- }
-
- #endregion
-
- private void GsmWrite(string str)
- {
- Port.Write(str);
- Console.Write(str);
- }
-
-
- public int GetSMCount()
- {
- return alRecevingSM.Count;
- }
- }
- }
|