xBeeAppEscapingMode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #include "xBeeAppEscapingMode.h"
  2. //需要转义的数据
  3. #define ESCAPING_DATA1 0x7E
  4. #define ESCAPING_DATA2 0x7D
  5. #define ESCAPING_DATA3 0x11
  6. #define ESCAPING_DATA4 0x13
  7. #define XBEE_APP2_ESCAP_LENGTH 150//数组长度
  8. unsigned char xA2EsArray[XBEE_APP2_ESCAP_LENGTH];//保存转义完成后的数据
  9. #define XBEE_APP2_MAKEFRAME_LENGTH 150//makeFrame数组长度
  10. unsigned char xA2MakeFrame[XBEE_APP2_MAKEFRAME_LENGTH];//makeFrame保存转义完成后的数据
  11. #define XBEE_APP2_UNMAKEFRAME_LENGTH 150//解析makeFrame数组长度
  12. unsigned char xA2UnMakeFrame[XBEE_APP2_UNMAKEFRAME_LENGTH];//保存解析makeFrame完成后的数据
  13. #define XBEE_APP2_REMOTE_LENGTH 150//远程命令
  14. unsigned char xA2RemoteCommandFrame[XBEE_APP2_REMOTE_LENGTH];//远程命令数组
  15. fRameFORMAT_t userSendFrame_t;//发送时候帧格式初始化
  16. fRAME_0x90_RECEIVE_FORMAT_t comeFromWhom0x90Frame_t;//接收0x90时候帧格式
  17. fRAME_0x91_RECEIVE_FORMAT_t comeFromWhom0x91Frame_t;//接收0x91时候帧格式
  18. fRAME_0x17_REMOTE_FORMAT_t atRemoteCommand0x17Frame_t;//远程命令帧
  19. void xBeeFrameFormatInit()//发送时候的帧结构初始化
  20. {
  21. userSendFrame_t.frameHead = 0x7E;//帧头
  22. userSendFrame_t.frameLen = 0;//数据长度Number of bytes between length and checksum fields.
  23. userSendFrame_t.frameType = 0x10;//帧类型,Transmit Request
  24. userSendFrame_t.frameID = 0x01;
  25. userSendFrame_t.des64DeviceAdd[0] = 0x00;//器件ID 高
  26. userSendFrame_t.des64DeviceAdd[1] = 0x00;//器件ID |
  27. userSendFrame_t.des64DeviceAdd[2] = 0x00;//器件ID |
  28. userSendFrame_t.des64DeviceAdd[3] = 0x00;//器件ID |
  29. userSendFrame_t.des64DeviceAdd[4] = 0x00;//器件ID |
  30. userSendFrame_t.des64DeviceAdd[5] = 0x00;//器件ID |
  31. userSendFrame_t.des64DeviceAdd[6] = 0xFF;//器件ID |
  32. userSendFrame_t.des64DeviceAdd[7] = 0xFF;//器件ID 低
  33. userSendFrame_t.des16NetAdd = 0xFFFE ;//网络ID
  34. userSendFrame_t.broadcastRadius = 0x00;//广播半径
  35. }
  36. /*******************************************************************************
  37. 函数名:xBeeApp2EscapingMode
  38. 功 能:处理数据中需要转义后发送的数据
  39. data bytes which need escaping: 0x7E, 0x7D, 0x11, 0x13
  40. 参 数:buf,原始数据数组的首地址,len数组长度
  41. 返回值:处理后的数组地址
  42. 编 写:
  43. 时 间:2016-01-20
  44. *******************************************************************************/
  45. void xBeeApp2EscapingMode(unsigned char* buf)//返回一个处理完成后的数组的首地址
  46. {
  47. unsigned char l = 0,xLen = 1;
  48. xA2EsArray[xLen++] = buf[1];
  49. for(l = 2; l <= buf[0]; l++)
  50. {
  51. if(buf[l] == ESCAPING_DATA1 || buf[l] == ESCAPING_DATA2 || buf[l] == ESCAPING_DATA3 || buf[l] == ESCAPING_DATA4)
  52. {
  53. xA2EsArray[xLen++] = 0x7D;
  54. xA2EsArray[xLen++] = buf[l]^0x20;//XOR
  55. }
  56. else
  57. xA2EsArray[xLen++] = buf[l];
  58. }
  59. xA2EsArray[0] = xLen - 1;//数据长度
  60. for(l = 0; l <= xLen; l++)
  61. buf[l] = xA2EsArray[l];//数据长度
  62. }
  63. /*******************************************************************************
  64. 函数名:xBeeApp2MakeFrame
  65. 功 能:
  66. 参 数:
  67. 返回值:
  68. 编 写:
  69. 时 间:2016-01-20
  70. *******************************************************************************/
  71. unsigned char* xBeeApp2MakeFrame(fRameFORMAT_t *sendFrameFormat,unsigned char* buf)//返回一个处理完成后的数组的首地址
  72. {
  73. unsigned char len = buf[0];
  74. unsigned char l;
  75. unsigned char xLen = 1;//处理后的数组长度,放在数组的第一位
  76. unsigned char sumCheck = 0;//校验和
  77. sendFrameFormat->frameLen = len + 14;//总的数据长度
  78. xA2MakeFrame[xLen++] = sendFrameFormat->frameHead;//帧头0x7E
  79. xA2MakeFrame[xLen++] = sendFrameFormat->frameLen >> 8;//数据长度高八位
  80. xA2MakeFrame[xLen++] = sendFrameFormat->frameLen;//数据长度低八位
  81. xA2MakeFrame[xLen++] = sendFrameFormat->frameType;//帧类型,Transmit Request
  82. sumCheck += xA2MakeFrame[xLen - 1];
  83. xA2MakeFrame[xLen++] = sendFrameFormat->frameID;//
  84. sumCheck += xA2MakeFrame[xLen - 1];
  85. for(l = 0; l < 8; l++) //目标器件64位地址
  86. {
  87. xA2MakeFrame[xLen++] = sendFrameFormat->des64DeviceAdd[l];
  88. sumCheck += xA2MakeFrame[xLen - 1];
  89. }
  90. xA2MakeFrame[xLen++] = sendFrameFormat->des16NetAdd >> 8;//16位网络ID 高八位
  91. sumCheck += xA2MakeFrame[xLen - 1];
  92. xA2MakeFrame[xLen++] = sendFrameFormat->des16NetAdd;//16位网络ID 低八位
  93. sumCheck += xA2MakeFrame[xLen - 1];
  94. xA2MakeFrame[xLen++] = sendFrameFormat->broadcastRadius;//广播半径
  95. sumCheck += xA2MakeFrame[xLen - 1];
  96. xA2MakeFrame[xLen++] = sendFrameFormat->transmitOptions;//选项
  97. sumCheck += xA2MakeFrame[xLen - 1];
  98. //RFData
  99. for(l = 1; l < len + 1; l++)
  100. {
  101. xA2MakeFrame[xLen++] = buf[l];
  102. sumCheck += xA2MakeFrame[xLen - 1];
  103. }
  104. xA2MakeFrame[xLen++] = 0xFF - sumCheck;
  105. xA2MakeFrame[0] = xLen - 1;
  106. xBeeApp2EscapingMode(xA2MakeFrame);
  107. return xA2MakeFrame;//返回最终数组
  108. }
  109. /*******************************************************************************
  110. *******************************************************************************/
  111. /*******************************************************************************
  112. *******************************************************************************/
  113. /*******************************************************************************
  114. *******************************************************************************/
  115. /*******************************************************************************
  116. 函数名:xBeeApp2UnEscaping0x90Mode
  117. 功 能:接收指示,接收到0x91类型帧时的解析
  118. 参 数:buf,原始数据数组的首地址
  119. 返回值:处理后的数组地址
  120. 编 写:
  121. 时 间:2016-01-20
  122. *******************************************************************************/
  123. //if(buf[0] == 7E && buf[3] == 0x90);//
  124. unsigned char* xBeeApp2UnEscaping0x90Mode(unsigned char* buf)//返回一个处理完成后的数组的首地址
  125. {
  126. unsigned char l = 0;
  127. unsigned int frameDatLen;//帧中的数据长度字段
  128. unsigned int datLen,datLength;//帧中包含的数据长度
  129. //重新定义了一个与发送端相同的结构体,用来保存发送者的帧信息 comeFromWhoFrame_t
  130. comeFromWhom0x90Frame_t.frameHead = buf[0];//帧头
  131. comeFromWhom0x90Frame_t.frameLen = buf[1];//高八位
  132. comeFromWhom0x90Frame_t.frameLen <<= 8;
  133. comeFromWhom0x90Frame_t.frameLen |= buf[2];//低八位
  134. comeFromWhom0x90Frame_t.frameType = buf[3];//帧类型,Transmit Request,应该是0x90
  135. comeFromWhom0x90Frame_t.frameID = buf[4];//协议中,frameID是64位地址的最高位,所以最高位也是buf[4]
  136. comeFromWhom0x90Frame_t.source64DeviceAdd[0] = buf[5];//器件ID 高
  137. comeFromWhom0x90Frame_t.source64DeviceAdd[1] = buf[6];//器件ID |
  138. comeFromWhom0x90Frame_t.source64DeviceAdd[2] = buf[7];//器件ID |
  139. comeFromWhom0x90Frame_t.source64DeviceAdd[3] = buf[8];//器件ID |
  140. comeFromWhom0x90Frame_t.source64DeviceAdd[4] = buf[9];//器件ID |
  141. comeFromWhom0x90Frame_t.source64DeviceAdd[5] = buf[10];//器件ID |
  142. comeFromWhom0x90Frame_t.source64DeviceAdd[6] = buf[11];//器件ID |
  143. comeFromWhom0x90Frame_t.source64DeviceAdd[7] = buf[12];//器件ID 低
  144. comeFromWhom0x90Frame_t.source16NetAdd = buf[13] ;//网络ID
  145. comeFromWhom0x90Frame_t.source16NetAdd <<= 8 ;//网络ID
  146. comeFromWhom0x90Frame_t.source16NetAdd |= buf[14] ;//网络ID
  147. comeFromWhom0x90Frame_t.receiveOptions = buf[15];//接收选项
  148. frameDatLen = comeFromWhom0x90Frame_t.frameLen;//帧中标识的数据长度
  149. datLen = frameDatLen - 12;//负载数据长度
  150. datLength = 0;//真实数据长度
  151. for(l = 1; l < datLen + 1; l++) //取出帧中的负载数据
  152. {
  153. if(buf[l + 15] == 0x7D)
  154. {
  155. l++;//指向下一个
  156. xA2UnMakeFrame[datLength+1] = buf[l + 15] ^ 0x20;
  157. datLen++;
  158. }
  159. else
  160. xA2UnMakeFrame[datLength+1] = buf[l + 15];
  161. datLength++;//真实数据长度
  162. }
  163. xA2UnMakeFrame[0] = datLength;
  164. return xA2UnMakeFrame;//返回处理完成后的数组
  165. }
  166. /*******************************************************************************
  167. 函数名:xBeeApp2UnEscaping0x91Mode
  168. 功 能:接收指示,接收到0x91类型帧时的解析
  169. 参 数:buf,原始数据数组的首地址
  170. 返回值:处理后的数组地址
  171. 编 写:
  172. 时 间:2016-01-20
  173. *******************************************************************************/
  174. //if(buf[0] == 7E && buf[3] == 0x91);//
  175. unsigned char* xBeeApp2UnEscaping0x91Mode(unsigned char* buf)//返回一个处理完成后的数组的首地址
  176. {
  177. unsigned char l = 0;
  178. unsigned int frameDatLen;//帧中的数据长度字段
  179. unsigned int datLen,datLength;//帧中包含的数据长度
  180. //重新定义了一个与发送端相同的结构体,用来保存发送者的帧信息 comeFromWhoFrame_t
  181. comeFromWhom0x91Frame_t.frameHead = buf[0];//帧头
  182. comeFromWhom0x91Frame_t.frameLen = buf[1];//高八位
  183. comeFromWhom0x91Frame_t.frameLen <<= 8;
  184. comeFromWhom0x91Frame_t.frameLen |= buf[2];//低八位
  185. comeFromWhom0x91Frame_t.frameType = buf[3];//帧类型,Transmit Request,应该是0x90
  186. comeFromWhom0x91Frame_t.source64DeviceAdd[0] = buf[4];//器件ID 高
  187. comeFromWhom0x91Frame_t.source64DeviceAdd[1] = buf[5];//器件ID |
  188. comeFromWhom0x91Frame_t.source64DeviceAdd[2] = buf[6];//器件ID |
  189. comeFromWhom0x91Frame_t.source64DeviceAdd[3] = buf[7];//器件ID |
  190. comeFromWhom0x91Frame_t.source64DeviceAdd[4] = buf[8];//器件ID |
  191. comeFromWhom0x91Frame_t.source64DeviceAdd[5] = buf[9];//器件ID |
  192. comeFromWhom0x91Frame_t.source64DeviceAdd[6] = buf[10];//器件ID |
  193. comeFromWhom0x91Frame_t.source64DeviceAdd[7] = buf[11];//器件ID 低
  194. comeFromWhom0x91Frame_t.source16NetAdd = buf[12] ;//网络ID
  195. comeFromWhom0x91Frame_t.source16NetAdd <<= 8 ;//网络ID
  196. comeFromWhom0x91Frame_t.source16NetAdd |= buf[13] ;//网络ID
  197. comeFromWhom0x91Frame_t.sourceEndpoint = buf[14] ;//Source endpoint.
  198. comeFromWhom0x91Frame_t.destinationEndpoint = buf[15] ;//Destination Endpoint
  199. comeFromWhom0x91Frame_t.clusterID = buf[16] ;//Cluster ID
  200. comeFromWhom0x91Frame_t.clusterID <<= 8 ;//
  201. comeFromWhom0x91Frame_t.clusterID |= buf[17] ;//
  202. comeFromWhom0x91Frame_t.proFileID = buf[18] ;//proFileID
  203. comeFromWhom0x91Frame_t.proFileID <<= 8;//
  204. comeFromWhom0x91Frame_t.proFileID = buf[19] ;//
  205. comeFromWhom0x91Frame_t.receiveOptions = buf[20];//接收选项
  206. frameDatLen = comeFromWhom0x91Frame_t.frameLen;//帧中标识的数据长度
  207. datLen = frameDatLen - 18;//负载数据长度
  208. datLength = 0;//真实数据长度
  209. for(l = 1; l < datLen + 1; l++) //取出帧中的负载数据
  210. {
  211. if(buf[l + 22] == 0x7D)
  212. {
  213. l++;//指向下一个
  214. xA2UnMakeFrame[datLength+1] = buf[l + 22] ^ 0x20;
  215. datLen++;
  216. }
  217. else
  218. xA2UnMakeFrame[datLength+1] = buf[l + 22];
  219. datLength++;//真实数据长度
  220. }
  221. xA2UnMakeFrame[0] = datLength;
  222. return xA2UnMakeFrame;//返回处理完成后的数组
  223. }
  224. /*******************************************************************************
  225. 函数名:xBeeApp2RemoteFrame
  226. 功 能:
  227. 参 数:
  228. 返回值:
  229. 编 写:
  230. 时 间:2016-01-20
  231. *******************************************************************************/
  232. unsigned char* xBeeApp2RemoteFrame(void)////远程命令付值,返回赋值后的数组
  233. {
  234. unsigned char sumCheck = 0;
  235. unsigned char xLen = 1;
  236. atRemoteCommand0x17Frame_t.frameHead = 0x7e;//帧头
  237. xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.frameHead;
  238. atRemoteCommand0x17Frame_t.frameLen = 0x0010;//
  239. xA2RemoteCommandFrame[xLen++] = (atRemoteCommand0x17Frame_t.frameLen >> 8);
  240. xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.frameLen;
  241. atRemoteCommand0x17Frame_t.frameType = 0x17;//帧类型
  242. xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.frameType;
  243. sumCheck += xA2RemoteCommandFrame[xLen - 1];
  244. atRemoteCommand0x17Frame_t.frameID = 0x01;//帧ID
  245. xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.frameID;
  246. sumCheck += xA2RemoteCommandFrame[xLen - 1];
  247. atRemoteCommand0x17Frame_t.des64DeviceAdd[0] = 0;//器件ID 高
  248. xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.des64DeviceAdd[0];
  249. sumCheck += xA2RemoteCommandFrame[xLen - 1];
  250. atRemoteCommand0x17Frame_t.des64DeviceAdd[1] = 0;//器件ID |
  251. xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.des64DeviceAdd[1];
  252. sumCheck += xA2RemoteCommandFrame[xLen - 1];
  253. atRemoteCommand0x17Frame_t.des64DeviceAdd[2] = 0;//器件ID |
  254. xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.des64DeviceAdd[2];
  255. sumCheck += xA2RemoteCommandFrame[xLen - 1];
  256. atRemoteCommand0x17Frame_t.des64DeviceAdd[3] = 0;//器件ID |
  257. xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.des64DeviceAdd[3];
  258. sumCheck += xA2RemoteCommandFrame[xLen - 1];
  259. atRemoteCommand0x17Frame_t.des64DeviceAdd[4] = 0;//器件ID |
  260. xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.des64DeviceAdd[4];
  261. sumCheck += xA2RemoteCommandFrame[xLen - 1];
  262. atRemoteCommand0x17Frame_t.des64DeviceAdd[5] = 0;//器件ID |
  263. xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.des64DeviceAdd[5];
  264. sumCheck += xA2RemoteCommandFrame[xLen - 1];
  265. atRemoteCommand0x17Frame_t.des64DeviceAdd[6] = 0xFF;//器件ID |
  266. xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.des64DeviceAdd[6];
  267. sumCheck += xA2RemoteCommandFrame[xLen - 1];
  268. atRemoteCommand0x17Frame_t.des64DeviceAdd[7] = 0xFF;//器件ID 低
  269. xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.des64DeviceAdd[7];
  270. sumCheck += xA2RemoteCommandFrame[xLen - 1];
  271. atRemoteCommand0x17Frame_t.des16NetAdd = 0xFFFE ;//网络ID
  272. xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.des16NetAdd >> 8;
  273. sumCheck += xA2RemoteCommandFrame[xLen - 1];
  274. xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.des16NetAdd;
  275. sumCheck += xA2RemoteCommandFrame[xLen - 1];
  276. atRemoteCommand0x17Frame_t.remoteCommandOptions = 0x02 ;//命令选项
  277. xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.remoteCommandOptions;
  278. sumCheck += xA2RemoteCommandFrame[xLen - 1];
  279. atRemoteCommand0x17Frame_t.ATCommand = 0x4248 ;//命令
  280. xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.ATCommand >> 8;
  281. sumCheck += xA2RemoteCommandFrame[xLen - 1];
  282. xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.ATCommand;
  283. sumCheck += xA2RemoteCommandFrame[xLen - 1];
  284. atRemoteCommand0x17Frame_t.commandParameter = 0x01 ;//命令参数
  285. xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.commandParameter;
  286. sumCheck += xA2RemoteCommandFrame[xLen - 1];
  287. sumCheck = 0xFF - sumCheck;
  288. xA2RemoteCommandFrame[xLen++] = sumCheck;
  289. xA2RemoteCommandFrame[0] = xLen - 1;
  290. return xA2RemoteCommandFrame;
  291. }