缓存区数据循环
缓存区循环读取数据,处理拆包和解包的网络字节流处理示例
取模运算,循环内存
recvLength += m_serialPort.Read(tcmsBuf, (checkIndex+recvLength)%tcmsBuf.Length,
(checkIndex + recvLength<tcmsBuf.Length)?
(tcmsBuf.Length - checkIndex - recvLength):
(checkIndex- (checkIndex + recvLength) % tcmsBuf.Length)); //循环内存接收判断,缓存2倍接收数据,方便内存区域循环
if(Math.Abs(m_TimeWatch.ElapsedMilliseconds% PositionManager.IntervalTime) > PositionManager.IntervalTime)
{
string tcmsReceiveError = "TCMS接收数据超时"+ PositionManager.IntervalTime + "ms";
ControlLogOperator.Error(tcmsReceiveError);
GlobalManager.PushMessage(DataType.FormLog, tcmsReceiveError);
}
while (recvLength >= Marshal.SizeOf(typeof(TCMSRecvPackage)))
{
//170,85
if (tcmsBuf[checkIndex] == 0xaa && tcmsBuf[(checkIndex + 28)%tcmsBuf.Length] == 0x55)
{
//清零计数
byte[] buf = tcmsBuf.Skip(checkIndex).Take(29).ToArray();
buf = buf.Length < 29 ? buf.Concat(tcmsBuf.Take(29 - buf.Len
来源:CSDN
作者:怪力左手
链接:https://blog.csdn.net/daoer_sofu/article/details/103613557