您的位置:首页 > 其它

uart过来数据不够,组包

2015-07-08 14:50 197 查看
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

{

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: