CPU使用率高,串口时间

问题描述:

Iam目前正在开发一个项目,我必须连续读取串口。数据持续最多45分钟。我必须通过校验和验证数据并创建一个79字节的数据包。在此之后,我必须实时绘制数据(轨迹)。代码的问题是,在开始时它使用20%的CPU使用率(奔腾4,3.0 GHz,超线程)(我认为它仍然很高),但随着时间的推移CPU使用率增加,最终达到60% 。CPU使用率高,串口时间

数据以115200的波特率进入,并以100毫秒的速率连续发送。

我读码,验证和绘制如下: 下面的函数接收数据并验证它...

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) 
    { 
     try 
     { 
      header1 = serialPort1.ReadByte(); 
      if (header1 == 0) 
       header2 = serialPort1.ReadByte(); 
      if ((header1 == 0) && (header2 == 1))//Store the data in an array. 
      { 
       for (int i = 0; i < 77; i++) 
        abudata[i] = serialPort1.ReadByte(); 
       tail = abudata[76]; 
      } 
      else 
      { 
       serialPort1.DiscardInBuffer(); 
      } 
      checksum = 1;// Calculate the checksum. 
      for (i = 0; i < 74; i++) 
       checksum = checksum + (abudata[i]); 
      checksum1 = (abudata[75] << 8); 
      checksum1 = checksum1 + (abudata[74]); 

      if ((checksum == checksum1) && (tail == 4)) 
       this.Invoke(new EventHandler(Display_Results));// Function to display 
     } 
     catch (Exception ode) 
     { 
      l4[4].BackColor = Color.Red; 
     } 
    } 

以下功能显示标签上的数据并绘制在轨迹图片框

private void Display_Results(object s, EventArgs e) 
    { 
     head1[0] = header1; 
     head1[1] = header2; 
     for (k = 0; k < 77; ++k) 
      head1[k + 2] = (((int)abudata[k]) & 0x000000ff); 
     jk = 0; 
     for (k = 0; k < 36; ++k) //Data packing into 36 bytes 
     { 
      num_1[k] = (ulong)((head1[jk + 1]) + (head1[jk] << 8)) & 0x0000ffff; 
      num_1[k] = (double)num_1[k]; 
      num_2[k] = (double)num_1[k]; 
      jk = jk + 2; 
      signbit = (int)num_1[k] >> 15; 

      if (signbit == 1) 
      { 
       sgnval = -1; 
       num_1[k] = num_1[k] - 65535; 
       num_1[k] = num_1[k] * (-1.0); 
      } 
      else 
       sgnval = 1; 

      //Converting the data into engineering values 

      engval[k] = Math.Round(num_1[k] * parammaxval[k] * sgnval/32767.0, 3); 

      if (k == 14) 
      { 
       try 
       { 

        curr_x = (pictureBox2.Width/2) + (int)((engval[13] * (pictureBox2.Width))/map_width); 
        curr_y = (pictureBox2.Height/2) - (int)((engval[14] * (pictureBox2.Height))/map_height); 
        PointF p1 = new Point(curr_x, curr_y); 
        if (_gPath != null && _gPath.PointCount > 0) 
         p1 = _gPath.PathPoints[_gPath.PathPoints.Length - 1]; 
        PointF p2 = new Point(curr_x, curr_y); 
        _gPath.AddLine(p1, p2); 
        pictureBox2.Invalidate(); 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message); 
       } 
      } 

     }   
    }  
+0

你是什么意思“C#串口”? C#没有串口。 – 2012-03-02 04:57:35

我得到了什么是与上面的代码的问题..

我使用图形路径绘制轨迹

if (k == 14) 
{ 
    try 
    { 

     curr_x = (pictureBox2.Width/2) + (int)((engval[13] * (pictureBox2.Width))/map_width); 
     curr_y = (pictureBox2.Height/2) - (int)((engval[14] * (pictureBox2.Height))/map_height); 
     PointF p1 = new Point(curr_x, curr_y); 
     if (_gPath != null && _gPath.PointCount > 0) 
      p1 = _gPath.PathPoints[_gPath.PathPoints.Length - 1]; 
     PointF p2 = new Point(curr_x, curr_y); 
     _gPath.AddLine(p1, p2); 
     pictureBox2.Invalidate(); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 

现在随着应用程序继续运行,它会收集大量的图表点因此绘制这个巨大数量的点正耗费资源。

有人建议我解决这个问题,即如何画出的轨迹不会降低系统...

我广泛地与串行端口连接的设备协同工作,可以向你保证,有规律使用SerialPort类的本身并不能产生高CPU负载。

FIRST我建议你到简介您的应用程序。有用于.NET的a bunch of配置文件

只有配置文件我建议将串口读取和数据处理分离。使用生产者消费者模式。将来自SerialPort的数据放入队列并从其他线程使用它。

这是我在SerialPortDataReceived功能在我的项目之一

private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e) 
    { 
     lock (SyncObject) 
     { 
      if (!_serialPort.IsOpen) return; 

      try 
      {      
       int toread = _serialPort.BytesToRead; 
       byte[] bytes = new byte[toread]; 
       _serialPort.Read(bytes, 0, toread); 

       ProducerAddBytes(bytes); 
      } 
      catch (TimeOutException) 
      { 
       //logic 
      } 
     } 
    } 

附:但简介第一!

+0

+1用于提示生产者消费者方法。 DataReceived事件来自IO完成线程,因此在那里进行数据处理并形成方法调用是一个大错误。 – ogggre 2012-03-01 13:11:27

+0

非常感谢您的建议,串口没有问题.. – tspshikari 2012-03-02 04:47:05