日期:2014-05-17  浏览次数:21040 次

C# 利用AT指令开发短信功能

论坛中看到的AT发送短信的代码,可以学习用


namespace ModemImpl {
    public class Modem {
        public bool IsOpen { get { return isOpen; } }
 
        public Modem(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(SendMessageRequest), 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(Message arg) {
            if (arg.Text.Length > 70) {
                throw new ArgumentOutOfRangeException("短信不能超过70个字符");
            }
 
            AddRequestToQueue(new SendMessageRequest(arg).Execute(port));
        }
 
        // ----------------------------------
        private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) {
            string line = string.Empty;
 
            try {
                while (port.BytesToRead > 0) {
                    line = port.ReadLine();
                    if (!string.IsNullOrEmpty(line) && "\r" != line) {
                        Console.WriteLine(line);
                        receiveBuffer.Add(line);
 
                        if (!isInterrupt) {
                            if (IsBeginInterrupt(line)) {
                                if (IsEndInterrupt()) {
                                    EndInterrupt();
                                }
                            } else {
                                string c = line.TrimEnd('\r');
                                if ("OK" == c || "ERROR" == c) {
                                    var response = new ModemResponse(receiveBuffer);
                                    response.Request = requestQueue.Dequeue();
                                    Type t = response.Request.GetType();
                                    if (responseMap.ContainsKey(t)) {
                                        responseMap[t](response);
                                    }
                                    receiveBuffer.Clear();
                                }
                            }
                        } else {
                            if (IsEndInterrupt()) {
                                EndInterrupt();
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                Trace.WriteLine(ex);
            }
        }
 
        private void EndInterrupt() {
            interruptHandler(receiveBuffer);
 
            be