一个单击时间.
       private void button2_Click(object sender, EventArgs e)
        {  
            Thread.Sleep(100); 
            Transmit();         
        }
上面调用此方法
       public void Transmit()
        {
            Byte[] Send = new Byte[1];
            Send[0] = 0xAA;
            serialPort1.Write(Send, 0, 1);
            Byte[] SendBy = new Byte[24];
            byte Sum = 0;
            SendBy[0] = 0x01;
            SendBy[3] = 0x07;
            for (int i = 0; i < 24; i++)
            {
                Sum = (byte)(SendBy[i] + Sum);
            }
            SendBy[23] = Sum;
            this.serialPort1.Write(SendBy, 0, 24);
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            label2.Text = System.DateTime.Now.ToString("HH:mm:ss");
            byte[] goor = new byte[serialPort1.BytesToRead];
            serialPort1.Read(goor, 0, serialPort1.BytesToRead);
            string boog = BitConverter.ToString(goor).Trim();
            boog = boog.Replace("-", "");
            Latestdata(boog);
        }
        public void Latestdata(string boog)
        {
            try
            { 
                if (boog.Length > 4)
                {
                    if (boog.Substring(0, 2) == "55")
                    {
                        string poor = boog.Substring(4, boog.Length - 4);
                        if (poor.Substring(18, 2) != "00")
                        {
                            poor = poor.Substring(14, 6).ToString();
                            textBox2.Text = poor;
                        }
                        else
                        {
                            poor = Convert.ToInt32(poor.Substring(14, 6), 16).ToString;                   
                            textBox2.Text = poor;
                        }
                    }
                    else if (boog.Substring(0, 2) == "FE")
                    {
                        string poor = boog.Substring(2, boog.Length - 2);
                        if (poor.Substring(18, 2) != "00")
                        {
                            poor = poor.Substring(14, 6).ToString();
                            textBox2.Text = poor;
                        }
                        else
                        {
                            poor = Convert.ToInt32(poor.Substring(14, 4), 16).ToString();
                            textBox2.Text = poor;
                        }
                    }
                }
            }          
            catch
            {
                MessageBox.Show("请重新读出数据!","系统提示");
            }
        }

解决方案 »

  1.   

    timer1_Tick是不是根本没有运行到单击事件里...
      

  2.   

    Thread.Sleep(100);这句话
    修改为:
    Thread.Sleep(1000);
    这样试试,或是
    int tick = Environment.TickCount;
    while(Environment.TickCount-tick<1000) Application.DoEvents();
      

  3.   

    //一个单击时间.
      private void button2_Click(object sender, EventArgs e)
      {   
      Thread.Sleep(100);  
      Transmit();   
      }
    //上面调用此方法
      public void Transmit()
      {
      Byte[] Send = new Byte[1];
      Send[0] = 0xAA;
      serialPort1.Write(Send, 0, 1);
      Byte[] SendBy = new Byte[24];
      byte Sum = 0;
      SendBy[0] = 0x01;
      SendBy[3] = 0x07;
      for (int i = 0; i < 24; i++)
      {
      Sum = (byte)(SendBy[i] + Sum);
      }
      SendBy[23] = Sum;
      this.serialPort1.Write(SendBy, 0, 24);
      }
      private void timer1_Tick(object sender, EventArgs e)
      {
      label2.Text = System.DateTime.Now.ToString("HH:mm:ss");
      byte[] goor = new byte[serialPort1.BytesToRead];
      serialPort1.Read(goor, 0, serialPort1.BytesToRead);
      string boog = BitConverter.ToString(goor).Trim();
      boog = boog.Replace("-", "");
      Latestdata(boog);
      }
      public void Latestdata(string boog)
      {
      try
      {  
      if (boog.Length > 4)
      {
      if (boog.Substring(0, 2) == "55")
      {
      string poor = boog.Substring(4, boog.Length - 4);
      if (poor.Substring(18, 2) != "00")
      {
      poor = poor.Substring(14, 6).ToString();
      textBox2.Text = poor;
      }
      else
      {
      poor = Convert.ToInt32(poor.Substring(14, 6), 16).ToString;   
      textBox2.Text = poor;
      }
      }
      else if (boog.Substring(0, 2) == "FE")
      {
      string poor = boog.Substring(2, boog.Length - 2);
      if (poor.Substring(18, 2) != "00")
      {
      poor = poor.Substring(14, 6).ToString();
      textBox2.Text = poor;
      }
      else
      {
      poor = Convert.ToInt32(poor.Substring(14, 4), 16).ToString();
      textBox2.Text = poor;
      }
      }
      }
      }   
      catch
      {
      MessageBox.Show("请重新读出数据!","系统提示");
      }
      }好头大看得
      

  4.   

    对端口不熟,不过你可以看看是不是每次timer事件里面接收的数据都被刷新了,如果是这么的话,那么你就要考虑下读取机制了
      

  5.   

    //===================
    中间的代码是我加的,你试试
    //===================public void Transmit()
      {
      Byte[] Send = new Byte[1];
      Send[0] = 0xAA;
      serialPort1.Write(Send, 0, 1);
      Byte[] SendBy = new Byte[24];
      byte Sum = 0;
      SendBy[0] = 0x01;
      SendBy[3] = 0x07;
      for (int i = 0; i < 24; i++)
      {
      Sum = (byte)(SendBy[i] + Sum);
      }
      SendBy[23] = Sum;
    //====================
    Thread.Sleep(100);  
    //=====================
     this.serialPort1.Write(SendBy, 0, 24);
      }private void timer1_Tick(object sender, EventArgs e)
      {
      label2.Text = System.DateTime.Now.ToString("HH:mm:ss");
    //===================
    while(serialPort1.BytesToRead==0)
    {
    Thread.Sleep(100);  
    }
    //===================
      byte[] goor = new byte[serialPort1.BytesToRead];
      serialPort1.Read(goor, 0, serialPort1.BytesToRead);
      string boog = BitConverter.ToString(goor).Trim();
      boog = boog.Replace("-", "");
      Latestdata(boog);
      }
      public void Latestdata(string boog)
      {
      try
      {  
      if (boog.Length > 4)
      {
      if (boog.Substring(0, 2) == "55")
      {
      string poor = boog.Substring(4, boog.Length - 4);
      if (poor.Substring(18, 2) != "00")
      {
      poor = poor.Substring(14, 6).ToString();
      textBox2.Text = poor;
      }
      else
      {
      poor = Convert.ToInt32(poor.Substring(14, 6), 16).ToString;   
      textBox2.Text = poor;
      }
      }
      else if (boog.Substring(0, 2) == "FE")
      {
      string poor = boog.Substring(2, boog.Length - 2);
      if (poor.Substring(18, 2) != "00")
      {
      poor = poor.Substring(14, 6).ToString();
      textBox2.Text = poor;
      }
      else
      {
      poor = Convert.ToInt32(poor.Substring(14, 4), 16).ToString();
      textBox2.Text = poor;
      }
      }
      }
      }   
      catch
      {
      MessageBox.Show("请重新读出数据!","系统提示");
      }
      }
      

  6.   

      private void button2_Click(object sender, EventArgs e)
      {  
    //======================= 
    timer1.Interval = 1000;
    timer1.enable=false;
    //=======================  
      Thread.Sleep(100);  
      Transmit();  
    //======================= 
    timer1.enable=true;
    //======================= 
      }
      

  7.   

    想了想timer中的
    //===================
    while(serialPort1.BytesToRead==0)
    {
    Thread.Sleep(100);   
    }
    //===================
    应该去掉
      

  8.   

    private void timer1_Tick(object sender, EventArgs e)
      {
    //F5运行前这一行放个断点
      label2.Text = System.DateTime.Now.ToString("HH:mm:ss");你F5运行,button2 按后,会断到断点处,然后再F10运行看看
      

  9.   

    可以了啊,,自己搞好了啊。.要多睡下才可以啊..Thread.Sleep(5000);