2012-03-01 112 views
2

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

回答

1

我得到了什麼是與上面的代碼的問題..

我使用圖形路徑繪製軌跡

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); 
    } 
} 

現在隨着應用程序繼續運行,它會收集大量的圖表點因此繪製這個巨大數量的點正耗費資源。

有人建議我解決這個問題,即如何畫出的軌跡不會降低系統...

4

我廣泛地與串行端口連接的設備協同工作,可以向你保證,有規律使用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