ProtocolPack.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /********************************************************************************
  2. * File Name: ProtocolPack.c
  3. * Function Describe: 协议封包
  4. * Explain:
  5. * Writer: ShiLiangWen
  6. * Date: 2019-2-9
  7. *******************************************************************************/
  8. #include "includes.h"
  9. #include "ProtocolPack.h"
  10. /*
  11. 部标协议
  12. |标识位|消息头|消息体|检验码|标识|
  13. 发送消息时:消息封装——>计算并填充校验码——>转义
  14. 接收消息时:转义还原——>验证校验码——>解析消息
  15. */
  16. unsigned char g_ucPropacketBuf[PROPACKET_BUF_LEN];
  17. /*********************************************************************************
  18. 将封包Buf数据进行转义,头尾加标识0x7e,中间有0x7e 的按如下规则转换
  19. 0x7e <————> 0x7d 后紧跟一个 0x02;
  20. 0x7d <————> 0x7d 后紧跟一个 0x01
  21. 返回:
  22. 0--转义失败,Buf空间不够
  23. >0 转义后Buf有效数据长度
  24. *********************************************************************************/
  25. int PacketConvert(unsigned char *Buf,int BufLen,int DataLen)
  26. {
  27. int i,j,num,len;
  28. //先统计Buf中的Data有多少个0x7e、0x7d
  29. num=0;
  30. for(i=0;i<DataLen;i++){
  31. if(Buf[i]==0x7e || Buf[i]==0x7d)num++;
  32. }
  33. //扩展后的长度
  34. len=DataLen+num+2;//加2是因为头尾都要加0x7e
  35. if(len>BufLen){//扩展后的长度大于缓存长度,转义失败
  36. return 0;
  37. }
  38. //尾部添加0x7e
  39. Buf[len-1]=0x7e;
  40. //中间转义
  41. j=len-2;
  42. for(i=DataLen-1;i>0;i--){
  43. if(Buf[i]==0x7e){
  44. Buf[j--]=0x02;
  45. Buf[j--]=0x7d;
  46. }else if(Buf[i]==0x7d){
  47. Buf[j--]=0x01;
  48. Buf[j--]=0x7d;
  49. }else{
  50. Buf[j--]=Buf[i];
  51. }
  52. }
  53. //i=0 的值也要处理一下
  54. if(Buf[0]==0x7e){
  55. Buf[j--]=0x02;
  56. Buf[j--]=0x7d;
  57. }else if(Buf[0]==0x7d){
  58. Buf[j--]=0x01;
  59. Buf[j--]=0x7d;
  60. }else{
  61. Buf[j--]=Buf[0];
  62. }
  63. //头部添加0x7e
  64. Buf[0]=0x7e;
  65. return len;
  66. }
  67. void PacketConvertTest(void)
  68. {
  69. int i,len;
  70. unsigned char buf[10]={0x09,0x29,0x7e,0x03,0x7d,0x04};
  71. printf("PacketConvertTest begin!\r\n");
  72. printf("buf1=");
  73. for(i=0;i<6;i++){
  74. printf("%02X ",buf[i]);
  75. }
  76. len=PacketConvert(buf,sizeof(buf),6);
  77. printf("\r\nbuf2=");
  78. for(i=0;i<len;i++){
  79. printf("%02X ",buf[i]);
  80. }
  81. printf("\r\nPacketConvertTest end!\r\n");
  82. }
  83. /*********************************************************************************
  84. 将封包Buf数据进行解转义,去掉头尾标识0x7e,中间有0x7e 的按如下规则转换
  85. 0x7e <————> 0x7d 后紧跟一个 0x02;
  86. 0x7d <————> 0x7d 后紧跟一个 0x01
  87. 0x7d 0x02<————> 0x7e
  88. 0x7d 0x01<————> 0x7d
  89. 返回:
  90. <0 协议错误
  91. >0 解转义后的数据长度
  92. *********************************************************************************/
  93. int PacketDeConvert(unsigned char *pMsg,int DataLen)
  94. {
  95. unsigned char *w,*p,*n;
  96. int i,j;
  97. if(pMsg[0]!=0x7e || pMsg[DataLen-1]!=0x7e)return -1;
  98. i=0;
  99. j=0;
  100. w=pMsg;
  101. p=pMsg;
  102. n=p+1;
  103. while(i<(DataLen-1)){
  104. if(*p==0x7e){
  105. if(i==0){
  106. p++;
  107. n++;
  108. i++;
  109. }else{
  110. return j;
  111. }
  112. }else if(*p==0x7d){
  113. if(*n==0x01){
  114. *w=0x7d;
  115. w++;
  116. j++;
  117. p+=2;
  118. n+=2;
  119. i+=2;
  120. }else if(*n==0x02){
  121. *w=0x7e;
  122. w++;
  123. j++;
  124. p+=2;
  125. n+=2;
  126. i+=2;
  127. }else return -3;
  128. }else{
  129. *w=*p;
  130. w++;
  131. j++;
  132. p++;
  133. n++;
  134. i++;
  135. }
  136. }
  137. return j;
  138. }
  139. void PacketDeConvertTest(void)
  140. {
  141. int i,len;
  142. unsigned char buf[]={
  143. 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,
  144. 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
  145. };
  146. SlwTrace(INF,"PacketDeConvertTest begin!\r\n");
  147. TraceData(buf,sizeof(buf));
  148. len=PacketDeConvert(buf,sizeof(buf));
  149. if(len>0){
  150. TraceData(buf,len);
  151. }else{
  152. sprintf((char *)buf,"len=%d\r\n",len);
  153. SlwTrace(DEBUG,(char *)buf);
  154. }
  155. SlwTrace(INF,"PacketDeConvertTest end!\r\n");
  156. }
  157. /********************************************************************************
  158. 消息封装——>计算并填充校验码-->转义
  159. *********************************************************************************/
  160. int Packet(unsigned short MsgID,unsigned short MsgNum ,unsigned char *pData,int DataLen)
  161. {
  162. SUTDS ds;
  163. SUTDL dl;
  164. unsigned char check=0;
  165. unsigned char *pBuf=g_ucPropacketBuf;
  166. int i,j;
  167. if((DataLen+14)>PROPACKET_BUF_LEN)return 0;
  168. i=0;
  169. ds.Data.usData=MsgID;
  170. pBuf[i++] = ds.Data.ucData.b2;
  171. pBuf[i++] = ds.Data.ucData.b1;
  172. //消息体属性
  173. //分包=无,加密=无,长度=DataLen
  174. ds.Data.usData=DataLen;
  175. pBuf[i++] = ds.Data.ucData.b2;
  176. pBuf[i++] = ds.Data.ucData.b1;
  177. //设备编码
  178. for(j=0;j<6;j++){
  179. pBuf[i++]=sutProductPara.DeviceID[j];
  180. }
  181. //消息流水号
  182. ds.Data.usData=MsgNum;
  183. pBuf[i++] = ds.Data.ucData.b2;
  184. pBuf[i++] = ds.Data.ucData.b1;
  185. //消息体
  186. if(DataLen>0){
  187. for(j=0;j<DataLen;j++){
  188. pBuf[i++]=pData[j];
  189. }
  190. }
  191. //校验码
  192. for(j=0;j<i;j++){
  193. check^=pBuf[j];
  194. }
  195. pBuf[i++]=check;
  196. return PacketConvert(pBuf,PROPACKET_BUF_LEN,i);
  197. }
  198. /********************************************************************************
  199. 打包成一个位置点数据包
  200. *********************************************************************************/
  201. int PacketPosition(unsigned short MsgNum,SUT_POSITION sutPosition)
  202. {
  203. SUTDS ds;
  204. SUTDL dl;
  205. unsigned char *pBuf=g_ucPropacketBuf;
  206. unsigned char check;
  207. int i,j;
  208. i=0;
  209. tsk_lock();
  210. //消息ID
  211. ds.Data.usData=MSGID_POSITION;
  212. pBuf[i++] = ds.Data.ucData.b2;
  213. pBuf[i++] = ds.Data.ucData.b1;
  214. //消息体属性: 分包=无,加密=无,长度暂时设置为0,后续再填充
  215. pBuf[i++]=0;
  216. pBuf[i++]=0;
  217. //设备编码
  218. for(j=0;j<6;j++){
  219. pBuf[i++]=sutProductPara.DeviceID[j];
  220. }
  221. //消息流水号
  222. ds.Data.usData=MsgNum;
  223. pBuf[i++] = ds.Data.ucData.b2;
  224. pBuf[i++] = ds.Data.ucData.b1;
  225. //以下是消息体
  226. dl.Data.ulData=*(unsigned int *)&sutPosition.Alarm;//0 报警标志 DWORD 报警标志位定义见 表 24
  227. pBuf[i++] = dl.Data.ucData.b4;
  228. pBuf[i++] = dl.Data.ucData.b3;
  229. pBuf[i++] = dl.Data.ucData.b2;
  230. pBuf[i++] = dl.Data.ucData.b1;
  231. dl.Data.ulData=*(unsigned int *)&sutPosition.Status;//4 状态 DWORD 状态位定义见 表 25
  232. pBuf[i++] = dl.Data.ucData.b4;
  233. pBuf[i++] = dl.Data.ucData.b3;
  234. pBuf[i++] = dl.Data.ucData.b2;
  235. pBuf[i++] = dl.Data.ucData.b1;
  236. dl.Data.ulData=sutPosition.Latitude;//8 纬度 DWORD 以度为单位的纬度值乘以10的6次方, 精确到百万分之一度
  237. pBuf[i++] = dl.Data.ucData.b4;
  238. pBuf[i++] = dl.Data.ucData.b3;
  239. pBuf[i++] = dl.Data.ucData.b2;
  240. pBuf[i++] = dl.Data.ucData.b1;
  241. dl.Data.ulData=sutPosition.Longitude;//12 经度 DWORD 以度为单位的经度值乘以10的6次方, 精确到百万分之一度
  242. pBuf[i++] = dl.Data.ucData.b4;
  243. pBuf[i++] = dl.Data.ucData.b3;
  244. pBuf[i++] = dl.Data.ucData.b2;
  245. pBuf[i++] = dl.Data.ucData.b1;
  246. ds.Data.usData=sutPosition.Altitude; //16 高程 WORD 海拔高度,单位为米(m)
  247. pBuf[i++] = ds.Data.ucData.b2;
  248. pBuf[i++] = ds.Data.ucData.b1;
  249. ds.Data.usData=sutPosition.Speed;//18 速度 WORD 1/10km/h
  250. pBuf[i++] = ds.Data.ucData.b2;
  251. pBuf[i++] = ds.Data.ucData.b1;
  252. ds.Data.usData=sutPosition.Aspect;//20 方向 WORD 0-359,正北为 0,顺时针
  253. pBuf[i++] = ds.Data.ucData.b2;
  254. pBuf[i++] = ds.Data.ucData.b1;
  255. pBuf[i++]=sutPosition.Time[0];//22 时间 BCD[6] YY-MM-DD-hh-mm-ss(GMT+8 时及的时间均采用此时区)
  256. pBuf[i++]=sutPosition.Time[1];
  257. pBuf[i++]=sutPosition.Time[2];
  258. pBuf[i++]=sutPosition.Time[3];
  259. pBuf[i++]=sutPosition.Time[4];
  260. pBuf[i++]=sutPosition.Time[5];
  261. //附加信息
  262. pBuf[i++]=sutPosition.ExtraMsgId;//附加信息ID --水表项目固定为:0x3A
  263. pBuf[i++]=sutPosition.ExtraMsgLen;//附加信息长度 --长度固定为从下一项开始到结构体最后的长度
  264. pBuf[i++]=sutPosition.NetMode;
  265. pBuf[i++]=sutPosition.CSQ;
  266. pBuf[i++]=sutPosition.Infrared;
  267. pBuf[i++]=sutPosition.Camera;
  268. for(j=0;j<sutPosition.TcLen;j++){
  269. pBuf[i++]=sutPosition.TcData[j];
  270. }
  271. //修正长度
  272. ds.Data.usData=i-12;
  273. pBuf[2] = ds.Data.ucData.b2;
  274. pBuf[3] = ds.Data.ucData.b1;
  275. //校验码
  276. check=0;
  277. for(j=0;j<i;j++){
  278. check^=pBuf[j];
  279. }
  280. pBuf[i++]=check;
  281. j=PacketConvert(pBuf,PROPACKET_BUF_LEN,i);
  282. tsk_unlock();
  283. return j;
  284. }
  285. /********************************************************************************
  286. 打包成多个位置点数据包
  287. *********************************************************************************/
  288. //int PacketMorePosition(unsigned short MsgNum)
  289. //{
  290. // SUTDS ds;
  291. // SUTDL dl;
  292. // unsigned char check;
  293. // unsigned char *pBuf=sutPropacket.pSendBuf;
  294. // int i,j;
  295. // SUT_POSITION sutPosition;
  296. // i=0;
  297. // //消息ID
  298. // ds.Data.usData=MSGID_M_POSITION;
  299. // pBuf[i++] = ds.Data.ucData.b2;
  300. // pBuf[i++] = ds.Data.ucData.b1;
  301. // //消息体属性: 分包=无,加密=无,长度暂时设置为0,后续再填充
  302. // pBuf[i++]=0;
  303. // pBuf[i++]=0;
  304. // //设备编码
  305. // for(j=0;j<6;j++){
  306. // pBuf[i++]=sutPropacket.DeviceID[j];
  307. // }
  308. // //消息流水号
  309. // ds.Data.usData=MsgNum;
  310. // pBuf[i++] = ds.Data.ucData.b2;
  311. // pBuf[i++] = ds.Data.ucData.b1;
  312. // //以下是消息体
  313. // pBuf[i++]=0;//数据项个数高位 先填充0 ,后面修正
  314. // pBuf[i++]=0;//数据项个数低位 先填充0 ,后面修正
  315. // pBuf[i++]=1;//位置数据类型 0:正常位置批量汇报,1:盲区补报
  316. // for(j=0;j<16;j++){
  317. // if(!StorageFiFoPop(&sutPosition))break;
  318. // pBuf[i++]=0;//每项长度 WORD 固定为50bytes
  319. // pBuf[i++]=50;//每项长度 WORD 固定为50bytes
  320. // dl.Data.ulData=*(unsigned int *)&sutPosition.Alarm;//0 报警标志 DWORD 报警标志位定义见 表 24
  321. // pBuf[i++] = dl.Data.ucData.b4;
  322. // pBuf[i++] = dl.Data.ucData.b3;
  323. // pBuf[i++] = dl.Data.ucData.b2;
  324. // pBuf[i++] = dl.Data.ucData.b1;
  325. //
  326. // dl.Data.ulData=*(unsigned int *)&sutPosition.Status;//4 状态 DWORD 状态位定义见 表 25
  327. // pBuf[i++] = dl.Data.ucData.b4;
  328. // pBuf[i++] = dl.Data.ucData.b3;
  329. // pBuf[i++] = dl.Data.ucData.b2;
  330. // pBuf[i++] = dl.Data.ucData.b1;
  331. //
  332. // dl.Data.ulData=sutPosition.Latitude;//8 纬度 DWORD 以度为单位的纬度值乘以10的6次方, 精确到百万分之一度
  333. // pBuf[i++] = dl.Data.ucData.b4;
  334. // pBuf[i++] = dl.Data.ucData.b3;
  335. // pBuf[i++] = dl.Data.ucData.b2;
  336. // pBuf[i++] = dl.Data.ucData.b1;
  337. //
  338. // dl.Data.ulData=sutPosition.Longitude;//12 经度 DWORD 以度为单位的经度值乘以10的6次方, 精确到百万分之一度
  339. // pBuf[i++] = dl.Data.ucData.b4;
  340. // pBuf[i++] = dl.Data.ucData.b3;
  341. // pBuf[i++] = dl.Data.ucData.b2;
  342. // pBuf[i++] = dl.Data.ucData.b1;
  343. //
  344. // ds.Data.usData=sutPosition.Altitude; //16 高程 WORD 海拔高度,单位为米(m)
  345. // pBuf[i++] = ds.Data.ucData.b2;
  346. // pBuf[i++] = ds.Data.ucData.b1;
  347. //
  348. // ds.Data.usData=sutPosition.Speed;//18 速度 WORD 1/10km/h
  349. // pBuf[i++] = ds.Data.ucData.b2;
  350. // pBuf[i++] = ds.Data.ucData.b1;
  351. //
  352. // ds.Data.usData=sutPosition.Aspect;//20 方向 WORD 0-359,正北为 0,顺时针
  353. // pBuf[i++] = ds.Data.ucData.b2;
  354. // pBuf[i++] = ds.Data.ucData.b1;
  355. //
  356. // pBuf[i++]=sutPosition.Time[0];//22 时间 BCD[6] YY-MM-DD-hh-mm-ss(GMT+8 时及的时间均采用此时区)
  357. // pBuf[i++]=sutPosition.Time[1];
  358. // pBuf[i++]=sutPosition.Time[2];
  359. // pBuf[i++]=sutPosition.Time[3];
  360. // pBuf[i++]=sutPosition.Time[4];
  361. // pBuf[i++]=sutPosition.Time[5];
  362. //
  363. // //附加信息
  364. // pBuf[i++]=sutPosition.ExtraMsgId;//附加信息ID --水表项目固定为:0x3A
  365. // pBuf[i++]=sutPosition.ExtraMsgLen;//附加信息长度 --长度固定为从下一项开始到结构体最后的长度
  366. //
  367. // dl.Data.ulData=sutPosition.InstantFlow;//瞬间流速 m3/h 放大1000倍
  368. // pBuf[i++] = dl.Data.ucData.b4;
  369. // pBuf[i++] = dl.Data.ucData.b3;
  370. // pBuf[i++] = dl.Data.ucData.b2;
  371. // pBuf[i++] = dl.Data.ucData.b1;
  372. //
  373. // dl.Data.ulData=sutPosition.TotalFlow; //累计流量 m3
  374. // pBuf[i++] = dl.Data.ucData.b4;
  375. // pBuf[i++] = dl.Data.ucData.b3;
  376. // pBuf[i++] = dl.Data.ucData.b2;
  377. // pBuf[i++] = dl.Data.ucData.b1;
  378. //
  379. // ds.Data.usData=sutPosition.Ax;
  380. // pBuf[i++] = ds.Data.ucData.b2;
  381. // pBuf[i++] = ds.Data.ucData.b1;
  382. // ds.Data.usData=sutPosition.Ay;
  383. // pBuf[i++] = ds.Data.ucData.b2;
  384. // pBuf[i++] = ds.Data.ucData.b1;
  385. // ds.Data.usData=sutPosition.Az;
  386. // pBuf[i++] = ds.Data.ucData.b2;
  387. // pBuf[i++] = ds.Data.ucData.b1;
  388. // ds.Data.usData=sutPosition.Mx;
  389. // pBuf[i++] = ds.Data.ucData.b2;
  390. // pBuf[i++] = ds.Data.ucData.b1;
  391. // ds.Data.usData=sutPosition.My;
  392. // pBuf[i++] = ds.Data.ucData.b2;
  393. // pBuf[i++] = ds.Data.ucData.b1;
  394. // ds.Data.usData=sutPosition.Mz;
  395. // pBuf[i++] = ds.Data.ucData.b2;
  396. // pBuf[i++] = ds.Data.ucData.b1;
  397. // }
  398. // //
  399. // pBuf[13]=j;//数据项修正
  400. //
  401. // //修正长度
  402. // ds.Data.usData=i-12;
  403. // pBuf[2] = ds.Data.ucData.b2;
  404. // pBuf[3] = ds.Data.ucData.b1;
  405. // //校验码
  406. // check=0;
  407. // for(j=0;j<i;j++){
  408. // check^=pBuf[j];
  409. // }
  410. // pBuf[i++]=check;
  411. // return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
  412. //}
  413. /********************************************************************************
  414. 打包成注册包
  415. 消息头+消息体
  416. *********************************************************************************/
  417. int PacketLogin(unsigned short MsgNum)
  418. {
  419. SUTDS ds;
  420. SUTDL dl;
  421. unsigned char *pBuf=g_ucPropacketBuf;
  422. unsigned char check;
  423. int i,j;
  424. i=0;
  425. tsk_lock();
  426. //消息ID
  427. ds.Data.usData=MSGID_LOGIN;
  428. pBuf[i++] = ds.Data.ucData.b2;
  429. pBuf[i++] = ds.Data.ucData.b1;
  430. //消息体属性: 分包=无,加密=无,长度暂时设置为0,后续再填充
  431. pBuf[i++]=0;
  432. pBuf[i++]=0;
  433. //设备编码
  434. for(j=0;j<6;j++){
  435. pBuf[i++]=sutProductPara.DeviceID[j];
  436. }
  437. //消息流水号
  438. ds.Data.usData=MsgNum;
  439. pBuf[i++] = ds.Data.ucData.b2;
  440. pBuf[i++] = ds.Data.ucData.b1;
  441. //产品型号 BYTE[8]
  442. for(j=0;j<8;j++){
  443. pBuf[i++]=sutProductPara.ProductName[j];
  444. }
  445. //硬件版本号 WORD
  446. ds.Data.usData=sutProductPara.HardwareVer;
  447. pBuf[i++] = ds.Data.ucData.b2;
  448. pBuf[i++] = ds.Data.ucData.b1;
  449. //软件版本号 WORD
  450. ds.Data.usData=sutProductPara.SoftwareVer;
  451. pBuf[i++] = ds.Data.ucData.b2;
  452. pBuf[i++] = ds.Data.ucData.b1;
  453. //终端校验码 DWORD
  454. dl.Data.ulData=sutProductPara.AKEY;
  455. pBuf[i++]=dl.Data.ucData.b4;
  456. pBuf[i++]=dl.Data.ucData.b3;
  457. pBuf[i++]=dl.Data.ucData.b2;
  458. pBuf[i++]=dl.Data.ucData.b1;
  459. //物联网卡号 BYTE[20]
  460. for(j=0;j<20;j++)pBuf[i++]=sutDeviceConfig.CCID[j];
  461. //采样间隔 WORD
  462. ds.Data.usData=sutDeviceConfig.SamplingInterval;
  463. pBuf[i++] = ds.Data.ucData.b2;
  464. pBuf[i++] = ds.Data.ucData.b1;
  465. //上传间隔 WORD
  466. ds.Data.usData=sutDeviceConfig.UploadInterval;
  467. pBuf[i++] = ds.Data.ucData.b2;
  468. pBuf[i++] = ds.Data.ucData.b1;
  469. //修正长度
  470. ds.Data.usData=i-12;
  471. pBuf[2] = ds.Data.ucData.b2;
  472. pBuf[3] = ds.Data.ucData.b1;
  473. //校验码
  474. check=0;
  475. for(j=0;j<i;j++){
  476. check^=pBuf[j];
  477. }
  478. pBuf[i++]=check;
  479. j=PacketConvert(pBuf,PROPACKET_BUF_LEN,i);
  480. tsk_unlock();
  481. return j;
  482. }
  483. /*
  484. 检查接收到的数据包是否符合要求
  485. 输入:pData/len 如:pData=7E8001000500000000000000000000020000867E len=20
  486. 输出:
  487. 校验失败,返回小于0的值,如-1
  488. 校验成功,返回应答ID,如0x8001
  489. 如果成功,应带消息ID放在AckID,应答消息流水号存放到AckNum
  490. */
  491. int CheckPacket(unsigned char *pData,unsigned short DataLen)
  492. {
  493. unsigned short MsgID;
  494. int i;
  495. unsigned char checksum=0;
  496. int len;
  497. //pData=7E8001000500000000000000000000020000867E
  498. //TraceData(pData,DataLen);
  499. len=PacketDeConvert(pData,DataLen);
  500. if(len<0){
  501. return -1;
  502. }
  503. //TraceData(pData,len);
  504. //pData=8001 0005 000000000000 0000 0000 0200 00 86
  505. // ID 属性 设备编号 流水号 应答流水号 应答ID 结果 校验
  506. //校验码验证
  507. for(i=0;i<(len-1);i++){
  508. checksum^=pData[i];
  509. }
  510. if(checksum!=pData[len-1]){
  511. // char buf[30];
  512. //printf("sum:%d!=%d\r\n",checksum,pData[len-1]);
  513. // SlwTrace(DEBUG,buf);
  514. return -2;
  515. }
  516. //设备编号验证
  517. for(i=0;i<6;i++){
  518. if(sutProductPara.DeviceID[i]!=pData[4+i])break;
  519. }
  520. if(i<6)return -3;
  521. MsgID=((unsigned short)pData[0]<<8)|pData[1];
  522. return MsgID;
  523. }