/******************************************************************************** * File Name: ProtocolPack.c * Function Describe: 协议封包 * Explain: * Writer: ShiLiangWen * Date: 2019-2-9 *******************************************************************************/ #include "includes.h" #include "ProtocolPack.h" /* 部标协议 |标识位|消息头|消息体|检验码|标识| 发送消息时:消息封装——>计算并填充校验码——>转义 接收消息时:转义还原——>验证校验码——>解析消息 */ unsigned char g_ucPropacketBuf[PROPACKET_BUF_LEN]; /********************************************************************************* 将封包Buf数据进行转义,头尾加标识0x7e,中间有0x7e 的按如下规则转换 0x7e <————> 0x7d 后紧跟一个 0x02; 0x7d <————> 0x7d 后紧跟一个 0x01 返回: 0--转义失败,Buf空间不够 >0 转义后Buf有效数据长度 *********************************************************************************/ int PacketConvert(unsigned char *Buf,int BufLen,int DataLen) { int i,j,num,len; //先统计Buf中的Data有多少个0x7e、0x7d num=0; for(i=0;iBufLen){//扩展后的长度大于缓存长度,转义失败 return 0; } //尾部添加0x7e Buf[len-1]=0x7e; //中间转义 j=len-2; for(i=DataLen-1;i>0;i--){ if(Buf[i]==0x7e){ Buf[j--]=0x02; Buf[j--]=0x7d; }else if(Buf[i]==0x7d){ Buf[j--]=0x01; Buf[j--]=0x7d; }else{ Buf[j--]=Buf[i]; } } //i=0 的值也要处理一下 if(Buf[0]==0x7e){ Buf[j--]=0x02; Buf[j--]=0x7d; }else if(Buf[0]==0x7d){ Buf[j--]=0x01; Buf[j--]=0x7d; }else{ Buf[j--]=Buf[0]; } //头部添加0x7e Buf[0]=0x7e; return len; } void PacketConvertTest(void) { int i,len; unsigned char buf[10]={0x09,0x29,0x7e,0x03,0x7d,0x04}; printf("PacketConvertTest begin!\r\n"); printf("buf1="); for(i=0;i<6;i++){ printf("%02X ",buf[i]); } len=PacketConvert(buf,sizeof(buf),6); printf("\r\nbuf2="); for(i=0;i 0x7d 后紧跟一个 0x02; 0x7d <————> 0x7d 后紧跟一个 0x01 0x7d 0x02<————> 0x7e 0x7d 0x01<————> 0x7d 返回: <0 协议错误 >0 解转义后的数据长度 *********************************************************************************/ int PacketDeConvert(unsigned char *pMsg,int DataLen) { unsigned char *w,*p,*n; int i,j; if(pMsg[0]!=0x7e || pMsg[DataLen-1]!=0x7e)return -1; i=0; j=0; w=pMsg; p=pMsg; n=p+1; while(i<(DataLen-1)){ if(*p==0x7e){ if(i==0){ p++; n++; i++; }else{ return j; } }else if(*p==0x7d){ if(*n==0x01){ *w=0x7d; w++; j++; p+=2; n+=2; i+=2; }else if(*n==0x02){ *w=0x7e; w++; j++; p+=2; n+=2; i+=2; }else return -3; }else{ *w=*p; w++; j++; p++; n++; i++; } } return j; } void PacketDeConvertTest(void) { int i,len; unsigned char buf[]={ 0x7E,0x89,0x29,0x00,0x0E,0x00,0x01,0x90,0x21,0x00,0x03,0x00,0x01,0x19,0x02,0x19,0x17,0x00,0x22,0x01,0x00,0x00,0x68,0x00,0x05,0x00,0x05,0x42,0x7E, 0x7E,0x89,0x29,0x00,0x0E,0x00,0x01,0x90,0x21,0x00,0x03,0x00,0x01,0x19,0x02,0x19,0x17,0x00,0x46,0x01,0x00,0x00,0x68,0x00,0x05,0x00,0x05,0x26,0x7E }; SlwTrace(INF,"PacketDeConvertTest begin!\r\n"); TraceData(buf,sizeof(buf)); len=PacketDeConvert(buf,sizeof(buf)); if(len>0){ TraceData(buf,len); }else{ sprintf((char *)buf,"len=%d\r\n",len); SlwTrace(DEBUG,(char *)buf); } SlwTrace(INF,"PacketDeConvertTest end!\r\n"); } /******************************************************************************** 消息封装——>计算并填充校验码-->转义 *********************************************************************************/ int Packet(unsigned short MsgID,unsigned short MsgNum ,unsigned char *pData,int DataLen) { SUTDS ds; SUTDL dl; unsigned char check=0; unsigned char *pBuf=g_ucPropacketBuf; int i,j; if((DataLen+14)>PROPACKET_BUF_LEN)return 0; i=0; ds.Data.usData=MsgID; pBuf[i++] = ds.Data.ucData.b2; pBuf[i++] = ds.Data.ucData.b1; //消息体属性 //分包=无,加密=无,长度=DataLen ds.Data.usData=DataLen; pBuf[i++] = ds.Data.ucData.b2; pBuf[i++] = ds.Data.ucData.b1; //设备编码 for(j=0;j<6;j++){ pBuf[i++]=sutProductPara.DeviceID[j]; } //消息流水号 ds.Data.usData=MsgNum; pBuf[i++] = ds.Data.ucData.b2; pBuf[i++] = ds.Data.ucData.b1; //消息体 if(DataLen>0){ for(j=0;j