2013-10-31 72 views
0

我寫下面的代碼,但我有錯誤:索引超出了數組的界限。c#error-Index超出數組範圍

int bytes = serialPort1.BytesToRead; 
     int r = 0; 
     byte[] byte_buffer = new byte[bytes]; 
     serialPort1.Read(byte_buffer, 0, bytes); 
      int x = 0; 
      while ((byte_buffer[x] == 0)&(x<230400)) 
      { 
       r++; 
       x++; 
      } 

      byte[] byte_mdfy = new byte[bytes-r];   

我該如何更正代碼?

+0

檢查以確保'x'也小於'byte_buffer.Length'? (或者更好 - 從'Read'獲得返回值...) – Ryan

回答

0

交叉檢查字節是否包含任何數據。或嘗試使用byte [] byte_buffer = new byte [bytes-1];

+0

感謝priya gund。 –