ReDatCount = Receive();
                          if (ReDatCount == 7)
                            
                         comm.Read(readbuffer, 0, ReDatCount);
                     if (readbuffer[0] != 0xbd)
                         break;//子函数
        public int Receive()
        {
            int ReDatCount = 0;
            timeReceive.Interval = 500;
            timeReceive.Enabled = true;
            while (timeReceive.Enabled == true)
            {
                if (mycomm.BytesToRead > ReDatCount)
                {
                    ReDatCount = mycomm.BytesToRead;
                    timeReceive.Enabled = false;
                    timeReceive.Interval = 100;
                    timeReceive.Enabled = true;
                }
                else
                {
                    Application.DoEvents();
                }
            }
            return ReDatCount;
        }        private void timeReceive_Tick(object sender, EventArgs e)
        {
            timeReceive.Enabled = false;
        }

解决方案 »

  1.   

            private void timer_Tick(object sender, EventArgs e)
            {
                char[] sendbyte = { '#', 'R', 'P', '\r', '\n' };            cptComPort.Write(sendbyte, 0, sendbyte.Length);            if (cptComPort.BytesToRead > 0)
                {
                    char[] receivebyte = new char[cptComPort.BytesToRead];
                    cptComPort.Read(receivebyte, 0, receivebyte.Length);
                    if ((receivebyte[0] == '#') && (receivebyte[1] == 'R') && (receivebyte[2] == 'P') && (receivebyte[13] == 'M') && (receivebyte[14] == 'P') && (receivebyte[15] == 'a'))
                    {
                        char[] b = new char[10];
                        for (int i = 0; i < 10; i++)
                        {
                            b[i] = receivebyte[i + 3];
                        }
                        decComPress = decimal.Parse(new string(b));
                        /*decComPress = (decimal)((b[1] - 0x30) * 100 + (b[2] - 0x30) * 10 + (b[4] - 0x30) * 0.1 + (b[5] - 0x30) * 0.01 + (b[6] - 0x30) * 0.001 + (b[7] - 0x30) * 0.0001 + (b[8] - 0x30) * 0.00001 + (b[9] - 0x30) * 0.000001);
                        if (b[0] == '-')
                            decComPress = -decComPress;*/
                    }
    }我也是刚学,在定时器中断里面,发送一串字符,然后查询接收,看看对你有用不。
      

  2.   


            private void timer_Tick(object sender, EventArgs e)
            {
                char[] sendbyte = { '#', 'R', 'P', '\r', '\n' };            cptComPort.Write(sendbyte, 0, sendbyte.Length);            //System.Threading.Thread.Sleep(100);            if (cptComPort.BytesToRead > 0)
                {
                    char[] receivebyte = new char[cptComPort.BytesToRead];
                    cptComPort.Read(receivebyte, 0, receivebyte.Length);
                    if ((receivebyte[0] == '#') && (receivebyte[1] == 'R') && (receivebyte[2] == 'P') && (receivebyte[13] == 'M') && (receivebyte[14] == 'P') && (receivebyte[15] == 'a'))
                    {
                        char[] b = new char[10];
                        for (int i = 0; i < 10; i++)
                        {
                            b[i] = receivebyte[i + 3];
                        }
                        decComPress = decimal.Parse(new string(b));
                        /*decComPress = (decimal)((b[1] - 0x30) * 100 + (b[2] - 0x30) * 10 + (b[4] - 0x30) * 0.1 + (b[5] - 0x30) * 0.01 + (b[6] - 0x30) * 0.001 + (b[7] - 0x30) * 0.0001 + (b[8] - 0x30) * 0.00001 + (b[9] - 0x30) * 0.000001);
                        if (b[0] == '-')
                            decComPress = -decComPress;*/
                    }
    }
    看起来好乱,怎么发成代码!
      

  3.   

      private void timeReceive_Tick(object sender, EventArgs e)
      {
         timeReceive.Enabled = false;
      }一触发你就设为FALSE
      public int Receive()能获取一次算好了,有可能一次都获取不到