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
{
}
}
}
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
{
}
}
}
相关文章推荐
- 单例模式
- 数字信号处理公式变程序(四)——巴特沃斯滤波器(下)
- 软件开发过程学习笔记(六)之测试报告模板
- windows 2012 r2下安装sharepoint 2013错误解决
- 软件开发过程学习笔记(六)之测试报告模板 分类: 开发过程 2015-07-08 14:50 8人阅读 评论(0) 收藏
- 判断一个数是否为2的若干次幂
- 实现编辑和删除
- Linux下重启mysql数据库的方法
- ipcs 使用
- C++顺序容器vector、deque、list
- 纯python实现的web: tornado性能测试以及实际使用解析
- linux中的sleep
- .NET 验证码/验证图片
- 计算一个整数中含1的个数
- 通过抽象工厂模式再理解java的上转型
- 大数据平台架构及数据分析平台架构
- eclipse自动补全的设置
- TCP Retransmission
- dp与px
- 高通8x12平台 mipi屏 调试