问题与#34; BytesToRead"

时间:2014-01-27 14:55:35

标签: c# buffer byte

我正在尝试使用SerialPort.BytesToRead属性,但我遇到了问题。下面是我使用“BytesToRead”属性的代码的一部分。在我发送的例子中

// Send 8 byte test command
byte[] test = { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x01, 0x02 };
comport.Write(test, 0, test.Length);

// Use "BytesToRead" to see 8 bytes and store it in a variable "bytes"
var bytes = comport.BytesToRead;
byte[] rbuffer = new byte[bytes];
comport.Read(rbuffer, 0, rbuffer.Length);

然而,当我执行我的代码时,我得到一个rbuffer看到0字节的异常。当我将鼠标悬停在BytesToRead上时,它会正确显示它捕获8个字节。但是,当我点击“bytes”的值时,它显示为0(我还将这个代码用“bytes”作为int ...同样的结果运行。)

我很困惑为什么当BytesToRead显示时var / int“bytes”为0为什么。在这件事情上的任何帮助都将非常感谢!

0 个答案:

没有答案