c# 错误索引超出了数组的边界



我在下面写了代码,但我有错误:索引超出了数组的范围。

    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];        

如何更正代码?

交叉检查字节是否包含任何数据。 或尝试使用 byte[] byte_buffer = 新字节[bytes-1];

最新更新