日期:2014-05-18 浏览次数:20759 次
using System; using System.Collections.Generic; using System.Linq; using System.Text; using SMSLibrary; using System.IO.Ports; using System.Text.RegularExpressions; using System.IO; using System.Threading; using System.Diagnostics; namespace SMSImpl { public class SMSModem { public bool IsOpen { get { return isOpen; } } public SMSModem(string portName) { this.portName = portName; Regex r1 = new Regex(@"^\+CMTI\:"); Regex r2 = new Regex(@"^\+CMGR\:"); Regex r3 = new Regex(@"^\+RING\:"); ATBeginHandlerMap.Add(r1, OnBeginCMTI); ATBeginHandlerMap.Add(r2, OnBeginCMGR); ATBeginHandlerMap.Add(r3, OnBeginRING); ATHandlerMap.Add(r1, OnCMTI); ATHandlerMap.Add(r2, OnCMGR); ATHandlerMap.Add(r3, OnRING); responseMap.Add(typeof(SendSMSRequest), OnCMGSResponse); } public void Open() { port = new SerialPort(portName); port.Encoding = Encoding.Default; port.ReadTimeout = 30000; port.WriteTimeout = 30000; port.BaudRate = 9600; port.RtsEnable = true; port.Open(); port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); AddRequestToQueue(new SettingReuqest("AT").Execute(port)); Thread.Sleep(50); AddRequestToQueue(new SettingReuqest("ATE0").Execute(port)); AddRequestToQueue(new SettingReuqest("AT+CMGF=1").Execute(port)); AddRequestToQueue(new SettingReuqest("AT+CNMI=2,1").Execute(port)); AddRequestToQueue(new SettingReuqest("AT+CSMP=49,167,0,8").Execute(port)); isOpen = true; } public void Close() { port.DataReceived -= port_DataReceived; port.Close(); isOpen = false; } public void Send(SMS arg) { if (arg.Text.Length > 70) { throw new Exception("短信超过70个字符!"); } AddRequestToQueue(new SendSMSRequest(a