日期:2014-05-18 浏览次数:21246 次
//开始 protected override void OnStart(string[] args) { uint MobPort = uint.Parse(XmlHelper.GetValue("Config.xml", "port")); sp.BaudRate = 9600; sp.PortName = "COM" + MobPort.ToString(); //如果串口已经打开 则先关闭 if (sp.IsOpen) sp.Close(); sp.Open(); //初始化设备 if (sp.IsOpen) { sp.Write("ATE0\r"); Thread.Sleep(50); sp.Write("AT+CMGF=0\r");//选择短消息信息格式:0-PDU;1-文本 Thread.Sleep(50); sp.Write("AT+CNMI=2,1\r");//(新sms来时送出脉冲) Thread.Sleep(50); Log.WriteLog("设备就绪!"); //Log.WriteLog("准备开启接收短信"); TCheckNewSms(); //timer_CheckNewSms.Enabled = true; //Log.WriteLog("接收短信成功开启"); } } /// <summary> /// 开启新进程启动检查是否有新短信 /// </summary> private void TCheckNewSms() { Thread t = new Thread(new ThreadStart(CheckNewSms)); t.Start(); } /// <summary> /// 检测 是否收到新短信(收取所有短信息) /// </summary> private void CheckNewSms() { if (sp.IsOpen) { StringBuilder sb = new StringBuilder(); sp.RtsEnable = true;//Added By Tan In 2011-07-28 sp.Write("AT+CMEE=1\r"); Thread.Sleep(300); sp.Write("AT+CMGL=4\r"); Log.WriteLog("执行AT指令:AT+CMGL=4!"); //接收数据 循环读取数据 直至收到“OK”或“ERROR” try { string temp = string.Empty; while (temp.Trim() != "OK" && temp.Trim() != "ERROR") { temp = sp.ReadLine(); Log.WriteLog("Temp:" + temp); sb.Append(temp); } Log.WriteLog(sb.ToString()); } catch (Exception ex) { Log.WriteLog(ex.Message); //throw ex; } Thread.Sleep(300); if (sb.ToString() != "") { Log.WriteLog(sb.ToString()); } } }
#region 发送短息 /// <summary> /// SendMessage函数用于发送短信.参数strMobileNumber设置手机号码,strMsgContent指 /// 定要发送的内容 /// </summary> /// <param name="strMobileNumber">手机号码</param> /// <param name="strMsgContent">要发送的内容</param> /// <returns> 返回指示发送是否成功</returns> public bool SendMessage(string strMobileNumber,string strMsgContent) { SerialPort serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); serialPort.WriteTimeout = 2000; try { serialPort.ReadBufferSize = 900000; if (serialPort.IsOpen) serialPort.Close();