uart过来数据不够,组包

unsigned char aTmpBuf[MAX_SIZE] = {0};
unsigned char aParsBuf[MAX_SIZE/2] = {0};

int iDex=0,iSaveDex=0,iGetDex=0,iGetCnt=0;

count = read(fd, buf, 512);
if(count)
{
if((iSaveDex+count)<MAX_SIZE)
{
memcpy(aTmpBuf+iSaveDex,buf,count);
iSaveDex+=count;
printf("iDex:%d, count:%d\n",iSaveDex, count);
/*printf("rev buf \n");
for(i=0;i<count;i++)
{
printf("%02x ",aTmpBuf[iSaveDex-count+i]);
}
printf("\n");*/

}else
{
memcpy(aTmpBuf+iSaveDex,buf,(MAX_SIZE-iSaveDex));
//iSaveDex=0;
memcpy(aTmpBuf,buf+(MAX_SIZE-iSaveDex),(count-(MAX_SIZE-iSaveDex)));
iSaveDex=count-(MAX_SIZE-iSaveDex);
}
//printBuf(aTmpBuf,iSaveDex);
/*printf("aTmpBuf buf \n");
for(i=0;i<iSaveDex;i++)
{
printf("%02x ",aTmpBuf[i]);
}
printf("\n");*/
printf("iSaveDex %d ,iGetDex %d \n",iSaveDex,iGetDex);
//if(((iSaveDex-iGetDex)>32)||(iSaveDex-iGetDex)<0)
if(((iSaveDex-iGetDex)>MAX_SIZE/32)||((iSaveDex-iGetDex)<0&&((MAX_SIZE+iSaveDex-iGetDex)>MAX_SIZE/32)))
{
if((0xff==aTmpBuf[iGetDex%MAX_SIZE])&&(0xff==aTmpBuf[(iGetDex+1)%MAX_SIZE]))//&&(0xff==aTmpBuf[aTmpBuf[iGetDex+2]+6]))
{
iGetCnt=aTmpBuf[(iGetDex+2)%MAX_SIZE]+6;
if((iGetDex+iGetCnt)<MAX_SIZE)
{
memcpy(aParsBuf,aTmpBuf+iGetDex,iGetCnt);
iGetDex+=iGetCnt;
}
else
{


memcpy(aParsBuf,aTmpBuf+iGetDex,(MAX_SIZE-iGetDex));
memcpy(aParsBuf+(MAX_SIZE-iGetDex),aTmpBuf,(iGetCnt-(MAX_SIZE-iGetDex)));
iGetDex=iGetCnt-(MAX_SIZE-iGetDex);
}
printf("iGetCnt is %d \n",iGetCnt);
printBuf(aParsBuf,iGetCnt);
int back =  g_CalcUartIf(aParsBuf, iGetCnt, uartBuf, &msgSize);
  printf("back:%d\n", back);
}
else
{

}

}



      }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值