日期:2014-05-18  浏览次数:20721 次

c# 为何通过短息猫接受不了短信内容
private void button1_Click(object sender, EventArgs e)
  {
  //连接设备
  if(GMS.GSMModemInitNew(comboBox1.Text,comboBox2.Text,null,null,false,textBox2.Text)==false)
  {
  MessageBox.Show("连接失败!"+GMS.GSMModemGetErrorMsg(),"提示",MessageBoxButtons.OK);
  return;
  }
  //接受短信
  string content = GMS.GSMModemSMSReadAll(0);
  if (content == null)
  {
  this.getMessage();
  return;
  }
  content = content.Replace("||", "|");//replace || to |
  string[] str_sp = content.Split('|');//进行分离
  int k = 0;
  string[,] str_Sp = new string[2, str_sp.Length / 2];
  for (int i = 0; i < str_sp.Length / 2; i++)
  {
  for (int j = 0; j < 2; j++)
  {
  str_Sp[j, i] = str_sp[k];
  if (k % 2 != 0)
  this.InsertMessage("insert into RecivedBox(Mobile,Content,reciveTime)values('"+Convert.ToString(str_Sp[0,i])+"'),'"+Convert.ToString(str_Sp[1,i])+"','"+DateTime.Now.ToString()+"')");
  k++;
  }
  }
  this.getMessage();
  }

------解决方案--------------------
C# code

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