使用MSComm读串口,发送的时候是无握手的设置,刚开始的时候使用无握手的方式接收能正常接收.
最近使用无握手的方式接收到的是乱码,使用任意一种握手协议都能正常接收到数据,但是接收一段时间之后
就不触发接收事件了,代码如下,大家帮我分析下:
   private bool InitComPort()
        {
            
            //通讯端口
            com.CommPort = short.Parse(tbComPort.Text.Substring(3,tbComPort.Text.Length-3).Trim());            if (com.PortOpen)
                return false;
            string baudrate = txtBOT.Text;
            //
            
            com.RThreshold = 1;
            //设置通讯端口
            com.Settings = baudrate+",N,8,1";
            //
            com.DTREnable = true;
            ////
            //com.RTSEnable =true;
            ////这里是设定握手协议
           com.Handshaking=HandshakeConstants.comRTSXOnXOff;
            //
            com.InputMode = MSCommLib.InputModeConstants.comInputModeText;
            //
            com.InputLen = 0;
            //
            com.NullDiscard = false;
            //            com.OnComm += new MSCommLib.DMSCommEvents_OnCommEventHandler(this.OnComm);
            //
            com.PortOpen = true;
            return true;
        }
        private void OnComm()
        {
            System.Threading.Thread.Sleep(1000);
            a = com.Input.ToString();
           //rtbComMsg.AppendText((string)com.Input);
           string f = a.Trim().Substring(0, 1);
           if (f == "$")
           {
               if (b == null)
               {
                   b = a;
               }
               else
               {
                   string bb = b.Trim().Replace("\r\n", "|");
                   //string[] aaa = b.Trim().Split(new char[2] { '\r', '\n' });
                   string[] aaa = bb.Split('|');
                   if (aaa.Length % 2 == 1)
                   {
                       b = aaa[aaa.Length - 1] = "\r\n" + a;                       for (int i = 0; i < aaa.Length - 1; i = i + 2)
                       {
                           //对记录进行处理
                           rtbComMsg.AppendText(aaa[i] + "\r\n");
                           rtbComMsg.AppendText(aaa[i + 1] + "\r\n");
                           if (aaa[i].Substring(0, 1) == "$")
                           {
                               resolv(aaa[i], aaa[i + 1]);                           }                       }
                   }
                   else
                   {
                       for (int i = 0; i < aaa.Length; i = i + 2)
                       {
                           //对记录进行处理
                           rtbComMsg.AppendText(aaa[i] + "\r\n");
                           rtbComMsg.AppendText(aaa[i + 1] + "\r\n");
                           if (aaa[i].Substring(0, 1) == "$")
                           {
                               resolv(aaa[i], aaa[i + 1]);                           }
                       }
                       b = null;
                   }
               }
               //MessageBox.Show(b);
           }
           else
           {               //if (b != null)
               //{
               b = b + a;
               //MessageBox.Show(d);
               //}
           }
           
                    }