我试图处理我的传入缓冲区,并确保我在每次传输时获得了所有125字节的数据。我创建了一个字节数组。我如何知道正在接收125个字节的数据。我试图显示字节数,但它显示不同的数字,我不确定是否正确的编码来获得接收的字节数。如何读取C#中的字节
这里是我的代码:
void datareceived(object sender, SerialDataReceivedEventArgs e)
{
myDelegate d = new myDelegate(update);
listBox1.Invoke(d, new object[] { });
}
public void update()
{
Console.WriteLine("Number of bytes:" + serialPort.BytesToRead); // it shows 155
while (serialPort.BytesToRead > 0)
bBuffer.Add((byte)serialPort.ReadByte());
ProcessBuffer(bBuffer);
}
private void ProcessBuffer(List<byte> bBuffer)
{
// Create a byte array buffer to hold the incoming data
byte[] buffer = bBuffer.ToArray();
// Show the user the incoming data // Display mode
for (int i = 0; i < buffer.Length; i++)
{
listBox1.Items.Add("SP: " + (bBuffer[43].ToString()) + " " + " HR: " + (bBuffer[103].ToString()) + " Time: ");
}
}
我想澄清我的答案(我最初误解了问题),但:'只BytesToRead'指本地接收BU FFER;它不会告诉你什么是传入的。通常情况下,像BytesToRead这样的主要用途是:如果你想在处理已经存在的数据或阻塞(同步)之间做出决定,或者做一个'BeginRead'(async) – 2011-12-19 07:08:59