滑动窗口循环移动接收数据

时间秒杀一切 提交于 2020-03-10 06:31:51

缓存区数据循环

缓存区循环读取数据,处理拆包和解包的网络字节流处理示例

取模运算,循环内存

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!