webo001 / gsmmodem

Automatically exported from code.google.com/p/gsmmodem
1 stars 0 forks source link

多个手机向短信平台发送数字1,然后有短信平台判断只要发来的是数字1,那么就返回数字2,如果两个手机同时发送,则会报错(SendMsg的foreach时)。 #11

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
需求:多个手机向短信平台发送数字1,然后短信平台判断只�
��发来的是数字1,那么就返回数字2
如果多个手机发送时间间隔长,下列代码没问题,可以运行��
�如果多个手机一起发(两个手机就能测试出来),则在SendMsg
的foreach时会报错。

#region 串口接收 事件
        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            PDUEncoding pe = new PDUEncoding();
            string temp = serialPort1.ReadLine();//读取一行
            if (temp.Length > 8)
            {
                int int_NewMsgIndex = Convert.ToInt32(temp.Split(',')[1]);//获取短信号
                string str_NewMsg_Ori = ReadMsgByIndex(int_NewMsgIndex);//获取短信原始内容
                string str_NewMsg = pe.PDUDecoder(str_NewMsg_Ori).SmsContent;//短信解码内容
                string str_Phone = pe.PDUDecoder(str_NewMsg_Ori).PhoneNumber;//电话号码

                if (temp.Substring(0, 6) == "+CMTI:")//收到短信标记
                  {
                    //发送1,返回2
                    if (str_NewMsg.Trim() == "1")
                    {
                        SendMsg(str_Phone, "2");
                    }                    }
                }
                DeleteMsgByIndex(int_NewMsgIndex);
            }
        }
        #endregion

        #region 发送短信
        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="phone">号码</param>
        /// <param name="msg">内容</param>
        public void SendMsg(string phone, string msg)
        {
            PDUEncoding pe = new PDUEncoding();
            pe.ServiceCenterAddress = "8613800311500";
            string tmp = string.Empty;
            foreach (CodedMessage cm in pe.PDUEncoder(phone, msg))//运行到该行时会报错
            {
                try
                {
                    //注销事件关联,为发送做准备
                    serialPort1.DataReceived -= serialPort1_DataReceived;
                    //信息字符数
                    serialPort1.Write("AT+CMGS=" + cm.Length.ToString() + "\r");

                    //定位到>
                    serialPort1.ReadTo(">");

                    //清除接收缓冲区数据
                    serialPort1.DiscardInBuffer();
                    //事件重新绑定 正常监视串口数据
                    serialPort1.DataReceived += serialPort1_DataReceived;
                    //发送数据
                    tmp = SendAT_No_Rec(cm.PduCode + (char)(26));//26 Ctrl+Z ascii码
                    //string a = cm.PduCode;
                    //lb_Message.Text = pe.PDUDecoder(a).SmsContent;
                }
                catch (Exception)
                {
                    throw new Exception("短信发送失败");
                }
                if (tmp.Contains("OK"))
                {
                    continue;
                }

                throw new Exception("短信发送失败");
            }
        }
        #endregion

Original issue reported on code.google.com by duhongyu...@gmail.com on 27 Sep 2011 at 12:16

GoogleCodeExporter commented 9 years ago
我试了一个方法
用您GsmModem.cs中的
private Queue<int> newMsgIndexQueue = new Queue<int>();//新消息序号
然后再serialPort1_DataReceived中添加
 if (temp.Substring(0, 6) == "+CMTI:")//收到短信标记
                {
                    newMsgIndexQueue.Enqueue(Convert.ToInt32(temp.Split(',')[1]));  //存储新信息序号

                }

自己定义一个timer,时间设为10秒,在它的Tick事件中添加如下��
�码,同时发送短信的手机少的时候还可以,多的时候就不行�
��。
int Msg_Count = newMsgIndexQueue.Count;
            if (Msg_Count > 0)
            {
                Thread.Sleep(5000);
                //注销事件关联,为发送做准备
                serialPort1.DataReceived -= serialPort1_DataReceived;
                PDUEncoding pe = new PDUEncoding();
                for (int i = 0; i < Msg_Count; i++)
                {
                    int int_NewMsgIndex = newMsgIndexQueue.Dequeue();
                    string str_NewMsg_Ori = ReadMsgByIndex(int_NewMsgIndex);//获取短信原始内容
                    string str_NewMsg = pe.PDUDecoder(str_NewMsg_Ori).SmsContent;//短信解码内容
                    string str_Phone = pe.PDUDecoder(str_NewMsg_Ori).PhoneNumber;//电话号码

                    if (str_NewMsg.Trim() == "1")
                    {
                        SendMsg(str_Phone, "2");
                    }
                }
                serialPort1.DataReceived += serialPort1_DataReceived;
            }

我想如何判断串口是否正在接收短信,如果正在接收短信的��
�就不运行上面代码,是不是就行了。可是怎么判断呢?

Original comment by duhongyu...@gmail.com on 28 Sep 2011 at 2:56

GoogleCodeExporter commented 9 years ago
还不是太明白这个需求,手机或Modem一般收发短信速度比较慢
,如果需要收发量都比较大,建议收发短信的处理分开,如��
�手机一直处于繁忙状态,会丢失接收短信的。

Original comment by wyun...@gmail.com on 19 Jan 2012 at 3:51