BytesToRead is greater than ReadBufferSize

本文关键字:ReadBufferSize than greater is BytesToRead | 更新日期: 2023-09-27 17:50:54

我正在调试一些串行的东西,重要的部分是SerialDataReceivedEventHandler。

我观察到(通过断点和悬停)读取的字节数几乎是缓冲区大小本身的两倍。嗯?

代码

aUartSemaphoreThatTells.WhatTheUartBackgroundRxIsDoing = (int)aValueWhichIndicatesThat.theUARTisReceivingData;
SerialPort CurrentPort = (SerialPort)sender; //// Int routine gave is this in the arguments
int LastByteInUartBuffer = CurrentPort.ReadBufferSize;
int TheNumberOfBytes = CurrentPort.BytesToRead;
byte[] inputData = new byte[TheNumberOfBytes];
int RenameThisInt = CurrentPort.Read(inputData, 0, TheNumberOfBytes);                 //// This is a C# property method of ports
int Dest;
Dest = UartPlaceHolders.RxBufferLeader; //// Will index into buffer for Chief dispatch

鼠标悬停告诉我…

  • LastByteInUartBuffer有4096
  • TheNumberOfBytes有8092
  • RenameThisInt有8092
  • inputData有正确的东西

(我知道串口另一边的数据是什么样子的,因为我自己生成它们)

我阅读了MSDN网站上的这两页,ReadBufferSize和BytesToRead

谁来指正我?如何从4K缓冲区中获得近8K字节?

BytesToRead is greater than ReadBufferSize

在SerialPort上注意这个注意事项。ReadBufferSize属性:

因为ReadBufferSize属性只表示的缓冲区,它可以返回比BytesToRead属性,它表示SerialPort缓冲区和windows创建的缓冲区

答案实际上在ReadBufferSize的文档中:

BytesToRead属性可以返回一个比ReadBufferSize属性大的值,因为ReadBufferSize属性只代表windows创建的缓冲区,而BytesToRead属性除了代表windows创建的缓冲区之外还代表SerialPort缓冲区。

在这种情况下,"Windows创建的缓冲区"是底层驱动程序内存,与c# SerialPort对象分配的内存相反。