当前位置:   article > 正文

北斗短报文通信实现源码_北斗短报文模块开发代码

北斗短报文模块开发代码
  1. /******************************************************************************************************
  2. 1、北斗协议和GPS协议不一样,不是以"\r\n"为一条协议的结束.
  3. 2、printf函数中的打印信息是const字符串常量,放在cpu内部flash,北斗模块printf打印过多,导致scanf("%s",payload);输入的内容被改写.
  4. 3、协议发送时不要使用结构体表示发送内容,接收时可以使用结构体表示接收内容.
  5. 4、调试协议时使用串口监视工具AccessPort进行串口协议抓包.
  6. 5、Msp430F5438:RAM(16KB),内部flash(256KB + 256B),4个串口.
  7. 6、注意北斗协议净荷封装的是RTU的协议,由于RTU协议也是以'$'符号位为协议的开始,因此需要进行转义,本程序以'M'替代净荷中的'$'.
  8. 7、Created by xumin,2015-05-20,copyright is reserved.
  9. ******************************************************************************************************/
  10. #include "..\..\include\includes.h"
  11. #ifdef DEF_COMPILE_PROJECT_APP
  12. #ifdef BD_COMM_SUPPORT
  13. unsigned char bd_buf_bitmap = 0;
  14. //unsigned char dwxx_buf[todo];
  15. unsigned char txxx_buf[TXXX_MAX_SIZE];
  16. unsigned char icxx_buf[ICXX_FIRM_SIZE];
  17. unsigned char zjxx_buf[ZJXX_FIRM_SIZE];
  18. //unsigned char sjxx_buf[todo];
  19. //unsigned char bbxx_buf[todo];
  20. unsigned char fkxx_buf[FKXX_FIRM_SIZE];
  21. unsigned int rx_packet_len = 0;
  22. unsigned char bd_rx_char;
  23. unsigned int bd_buf_pointer = 0;
  24. unsigned char bd_shared_rx_buf[RX_BD_MAX_DATA_SIZE];
  25. unsigned char g_src_user_addr[3];
  26. unsigned char g_dst_user_addr[3];
  27. unsigned char g_transfer_format;
  28. void init_uart3_for_bd()
  29. {
  30. #if 1 //波特率115200配置
  31. P10SEL |= BIT4 + BIT5;
  32. UCA3CTL1 |= UCSWRST; //USCI状态机复位
  33. UCA3CTL1 |= UCSSEL_2; //SMCLK
  34. UCA3BR0 = 69; //115200
  35. UCA3BR1 = 0x00;
  36. UCA3MCTL = 0x06; //波特率微调
  37. UCA3CTL1 &= ~UCSWRST; //初始化USCI状态机
  38. UCA3IE |= UCRXIE; //使能BD串口RX中断
  39. #else //波特率9600配置
  40. P10SEL |= BIT4+BIT5;
  41. UCA3CTL1 |= UCSWRST;//USCI状态机复位
  42. UCA3CTL1 |= UCSSEL_1;//ACLK
  43. UCA3BR0 = 0x03;//9600
  44. UCA3BR1 = 0x00;
  45. UCA3MCTL = 0x06;//波特率微调
  46. UCA3CTL1 &= ~UCSWRST;//初始化USCI状态机
  47. UCA3IE |= UCRXIE; //使能BD串口RX中断
  48. #endif
  49. }
  50. /*
  51. 异或校验和算法
  52. */
  53. static unsigned char xor_checksum (unsigned char *buf, unsigned int len)
  54. {
  55. unsigned int i;
  56. unsigned char checksum = 0;
  57. for (i = 0; i < len; ++i)
  58. {
  59. checksum ^= *(buf++);
  60. }
  61. return checksum;
  62. }
  63. void tx_uart3_char(unsigned char c)
  64. {
  65. while (!(UCA3IFG & UCTXIFG));
  66. UCA3TXBUF = c;
  67. }
  68. void tx_uart3_string(unsigned char *str, unsigned int len)
  69. {
  70. unsigned int i;
  71. for (i = 0 ; i < len; ++i)
  72. {
  73. tx_uart3_char(*(str + i));
  74. }
  75. }
  76. void create_txsq(unsigned char *src_user_addr, unsigned char *dst_user_addr,
  77. unsigned char transfer_format, unsigned char *payload,
  78. unsigned int payload_len, unsigned char *send_txsq_data)
  79. {
  80. /* 1、通信申请指令初始化,不采用memcpy等库函数,提高指令执行效率,只有涉及到大量数据赋值拷贝时才考虑用库函数 */
  81. send_txsq_data[0] = '$';
  82. send_txsq_data[1] = 'T';
  83. send_txsq_data[2] = 'X';
  84. send_txsq_data[3] = 'S';
  85. send_txsq_data[4] = 'Q';
  86. /* 2、包长度,先传高位,再传低位 */
  87. send_txsq_data[5] = (TXSQ_FIRM_SIZE + payload_len) / 256;
  88. send_txsq_data[6] = (TXSQ_FIRM_SIZE + payload_len) % 256;
  89. /* 3、源用户地址 */
  90. send_txsq_data[7] = *src_user_addr;
  91. send_txsq_data[8] = *(src_user_addr + 1);
  92. send_txsq_data[9] = *(src_user_addr + 2);
  93. /* 4.1、信息-信息类别 */
  94. if (transfer_format == 0) //汉字
  95. {
  96. send_txsq_data[10] = TXSQ_PAYLOAD_CHINESE;//0b01000100;
  97. }
  98. else //代码/混发
  99. {
  100. send_txsq_data[10] = TXSQ_PAYLOAD_BCD;//0b01000110;
  101. }
  102. /* 4.2、信息-目的用户地址 */
  103. send_txsq_data[11] = *dst_user_addr;
  104. send_txsq_data[12] = *(dst_user_addr + 1);
  105. send_txsq_data[13] = *(dst_user_addr + 2);
  106. /* 4.3、信息-电文净荷长度-单位是bit */
  107. send_txsq_data[14] = (payload_len * 8) / 256;
  108. send_txsq_data[15] = (payload_len * 8) % 256;
  109. /* 4.4、信息-是否应答 */
  110. send_txsq_data[16] = 0;
  111. /* 4.5、信息-电文内容 */
  112. memcpy(&send_txsq_data[17] , payload, payload_len);
  113. /* 5、校验和 */
  114. send_txsq_data[TXSQ_FIRM_SIZE + payload_len -1] = xor_checksum(send_txsq_data, (TXSQ_FIRM_SIZE + payload_len -1));
  115. printf("\r\n xor_checksum = 0x%x\r\n", xor_checksum(send_txsq_data, (TXSQ_FIRM_SIZE + payload_len -1)));
  116. }
  117. void send_dwsq()
  118. {
  119. //todo
  120. }
  121. /*
  122. 1、结构体不宜管理可变长度的数据协议,如通讯申请协议
  123. 2、发送长度为6个字节("我爱你"),发送方式为中文,协议内容:
  124. txsq:24 54 58 53 51 00 18 02 ad f7 44 02 ad f7 00 30 00 ce d2 b0 ae c4 e3 63
  125. txxx:24 54 58 58 58 00 1a 02 ad f7 40 02 ad f7 00 00 00 30 ce d2 b0 ae c4 e3 00 67
  126. */
  127. void send_txsq(unsigned char cmd, unsigned char *src_user_addr, unsigned char *dst_user_addr,
  128. unsigned char transfer_format, unsigned char *send_txsq_payload, unsigned int send_txsq_payload_len)
  129. {
  130. unsigned int i;
  131. unsigned char l_transfer_format;
  132. unsigned int payload_len;
  133. unsigned long src_user_addr_long;
  134. unsigned long dst_user_addr_long;
  135. unsigned char l_src_user_addr[3];
  136. unsigned char l_dst_user_addr[3];
  137. unsigned char payload[MAX_PAYLOAD_LEN];
  138. unsigned char send_txsq_data[TXSQ_FIRM_SIZE + MAX_PAYLOAD_LEN];
  139. if (cmd == 1)
  140. {
  141. UCA2IE &= ~UCRXIE;
  142. DebugPrintBd("\r\n 输入十六进制用户地址(如02adf7,02adfb):");
  143. scanf("%lx", &src_user_addr_long);
  144. DebugPrintBd("\r\n 输入十六进制目的用户地址(如02adf7,02adfb):");
  145. scanf("%lx", &dst_user_addr_long);
  146. DebugPrintBd("\r\n 内容编码(0:汉字,1代码):");
  147. scanf("%d",&l_transfer_format);
  148. #if 1
  149. DebugPrintBd("\r\n 发送内容:");
  150. scanf("%s", payload);
  151. payload_len = strlen((char const *)payload);
  152. #else
  153. DebugPrintBd("\r\n 输入发送内容长度:");
  154. scanf("%d", &payload_len);
  155. //payload_len = 78;
  156. for (i = 0; i < payload_len; ++i)
  157. {
  158. payload[i] = 0x5a;
  159. }
  160. #endif
  161. UCA2IE |= UCRXIE;
  162. l_src_user_addr[0] = src_user_addr_long / 65536;
  163. l_src_user_addr[1] = (src_user_addr_long % 65536) / 256;
  164. l_src_user_addr[2] = (src_user_addr_long % 65536) % 256;
  165. l_dst_user_addr[0] = dst_user_addr_long / 65536;
  166. l_dst_user_addr[1] = (dst_user_addr_long % 65536) / 256;
  167. l_dst_user_addr[2] = (dst_user_addr_long % 65536) % 256;
  168. for (i = 0; i < 3; ++i)
  169. {
  170. g_src_user_addr[i] = l_src_user_addr[i];
  171. g_dst_user_addr[i] = l_src_user_addr[i];
  172. }
  173. g_transfer_format = l_transfer_format;
  174. create_txsq(l_src_user_addr, l_dst_user_addr, l_transfer_format, payload, payload_len, send_txsq_data);
  175. tx_uart3_string(send_txsq_data, (TXSQ_FIRM_SIZE + payload_len));
  176. }
  177. else
  178. {
  179. create_txsq(src_user_addr, dst_user_addr, transfer_format, send_txsq_payload, send_txsq_payload_len, send_txsq_data);
  180. tx_uart3_string(send_txsq_data, (TXSQ_FIRM_SIZE + send_txsq_payload_len));
  181. }
  182. }
  183. void send_cksc()
  184. {
  185. //todo
  186. }
  187. /*
  188. 1、IC检测协议内容:
  189. icjc:24 49 43 4A 43 00 0C 00 00 00 00 2B
  190. icxx:24 49 43 58 58 00 16 02 AD F7 00 00 00 0B 06 00 3C 03 00 00 00 52
  191. */
  192. void send_icjc()
  193. {
  194. unsigned char send_icjc_data[XTZJ_FIRM_SIZE];
  195. send_icjc_data[0] = '$';
  196. send_icjc_data[1] = 'I';
  197. send_icjc_data[2] = 'C';
  198. send_icjc_data[3] = 'J';
  199. send_icjc_data[4] = 'C';
  200. send_icjc_data[5] = ICJC_FIRM_SIZE / 256; //先传高位
  201. send_icjc_data[6] = ICJC_FIRM_SIZE % 256; //再传低位
  202. send_icjc_data[7] = 0x00;
  203. send_icjc_data[8] = 0x00;
  204. send_icjc_data[9] = 0x00;
  205. send_icjc_data[10] = 0x00;
  206. send_icjc_data[11] = xor_checksum(send_icjc_data, (XTZJ_FIRM_SIZE - 1));
  207. tx_uart3_string(send_icjc_data, XTZJ_FIRM_SIZE);
  208. }
  209. /*
  210. 1、XTZJ协议内容:
  211. xtzj:24 58 54 5A 4A 00 0D 02 AD FB 00 00 61
  212. zjxx:24 5a 4a 58 58 00 15 02 AD FB 01 00 64 02 00 00 03 00 02 00 13
  213. */
  214. void send_xtzj()
  215. {
  216. unsigned long user_addr;
  217. unsigned int frequency;
  218. unsigned char send_xtzj_data[XTZJ_FIRM_SIZE];
  219. UCA2IE &= ~UCRXIE;
  220. DebugPrintBd("\r\n 输入十六进制用户地址(如02adf7):");
  221. scanf("%lx", &user_addr);
  222. DebugPrintBd("\r\n 输入系统自检的频度:");
  223. scanf("%d", &frequency);
  224. UCA2IE |= UCRXIE;
  225. send_xtzj_data[0] = '$';
  226. send_xtzj_data[1] = 'X';
  227. send_xtzj_data[2] = 'T';
  228. send_xtzj_data[3] = 'Z';
  229. send_xtzj_data[4] = 'J';
  230. send_xtzj_data[5] = XTZJ_FIRM_SIZE / 256; //先传高位
  231. send_xtzj_data[6] = XTZJ_FIRM_SIZE % 256; //再传低位
  232. send_xtzj_data[7] = user_addr / 65536;
  233. send_xtzj_data[8] = (user_addr % 65536) / 256;
  234. send_xtzj_data[9] = (user_addr % 65536) % 256;
  235. send_xtzj_data[10] = frequency / 256;
  236. send_xtzj_data[11] = frequency % 256;
  237. send_xtzj_data[12] = xor_checksum(send_xtzj_data, XTZJ_FIRM_SIZE-1);
  238. tx_uart3_string(send_xtzj_data, XTZJ_FIRM_SIZE);
  239. }
  240. void send_sjsc()
  241. {
  242. //todo
  243. }
  244. void send_bbdq()
  245. {
  246. //todo
  247. }
  248. void parse_txxx(struct txxx_struct *txxx)
  249. {
  250. unsigned int i;
  251. unsigned int payload_len;
  252. unsigned char send_data[104];//用途有2
  253. /* 1、指令内容 */
  254. for (i = 0; i < 5; ++i)
  255. {
  256. (*txxx).instruction[i] = txxx_buf[i];
  257. }
  258. /* 2、接收包长 */
  259. (*txxx).packet_len = txxx_buf[5] * 256 + txxx_buf[6];
  260. /* 3、目的用户地址 */
  261. for (i = 0; i < 3; ++i)
  262. {
  263. (*txxx).user_addr[i] = txxx_buf[i + 7];
  264. }
  265. /* 4.1、信息-信息类别 */
  266. memcpy(&((*txxx).txxx_info.txxx_info_type), (txxx_buf + 10), 1);
  267. /* 4.2、信息-发送方地址 */
  268. for (i = 0; i < 3; ++i)
  269. {
  270. (*txxx).txxx_info.src_user_addr[i] = txxx_buf[i + 11];
  271. }
  272. /* 4.3、信息-发送时间 */
  273. (*txxx).txxx_info.send_time.hour = txxx_buf[14];
  274. (*txxx).txxx_info.send_time.minute = txxx_buf[15];
  275. /* 4.4、信息-电文长度 */
  276. (*txxx).txxx_info.payload_len = txxx_buf[16] * 256 + txxx_buf[17];
  277. payload_len = (*txxx).txxx_info.payload_len / 8;
  278. /* 4.5、信息-电文内容 */
  279. memcpy((*txxx).txxx_info.payload, (txxx_buf + 18), payload_len);
  280. /* 4.6、信息-CRC */
  281. (*txxx).txxx_info.crc = txxx_buf[18 + payload_len];
  282. /* 5、校验和 */
  283. (*txxx).checksum = txxx_buf[18 + payload_len + 1];
  284. if ((txxx_buf[18] == 'z') && (txxx_buf[19] == 't')/* (txxx_buf[18] == 'z') && (txxx_buf[19] == 't') && (txxx_buf[20] == 'c') && (txxx_buf[20] == 'x') */)
  285. {
  286. #if 1
  287. create_send_data_1(send_data);
  288. send_txsq(0, &(*txxx).user_addr[0], &(*txxx).txxx_info.src_user_addr[0], g_transfer_format, send_data, (104-37));
  289. #else
  290. for (i = 0; i < (104-37); ++i)
  291. {
  292. send_data[i] = 0x5a;
  293. }
  294. send_data[0] = 'M';
  295. send_data[1] = (104-37);
  296. send_txsq(0, &(*txxx).user_addr[0], &(*txxx).txxx_info.src_user_addr[0], g_transfer_format, send_data, (104-37));
  297. #endif
  298. }else if ( (txxx_buf[18] == 'M') && (txxx_buf[19] == (104-37)) )
  299. {
  300. create_send_data_2(send_data, &txxx_buf[18], payload_len);
  301. SendData(104, 1, GSMNEW_ENUM_measureCurSend, send_data);
  302. }
  303. }
  304. void parse_icxx(struct icxx_struct *icxx)
  305. {
  306. unsigned int i;
  307. for (i = 0; i < 5; ++i)
  308. {
  309. (*icxx).instruction[i] = icxx_buf[i];
  310. }
  311. (*icxx).packet_len = icxx_buf[5] * 256 + icxx_buf[6];
  312. for (i = 0; i < 3; ++i)
  313. {
  314. (*icxx).user_addr[i] = icxx_buf[i + 7];
  315. }
  316. (*icxx).icxx_info.frame_id = icxx_buf[10];
  317. for (i = 0; i < 3; ++i)
  318. {
  319. (*icxx).icxx_info.broadcast_id[i] = icxx_buf[i + 11];
  320. }
  321. (*icxx).icxx_info.user_feature = icxx_buf[14];
  322. (*icxx).icxx_info.service_frequency = icxx_buf[15] * 256 + icxx_buf[16];
  323. (*icxx).icxx_info.comm_level = icxx_buf[17];
  324. (*icxx).icxx_info.encryption_flag = icxx_buf[18];
  325. (*icxx).icxx_info.user_num = icxx_buf[19] * 256 + icxx_buf[20];
  326. (*icxx).checksum = icxx_buf[21];
  327. }
  328. void parse_zjxx(struct zjxx_struct *zjxx)
  329. {
  330. unsigned int i;
  331. for (i = 0; i < 5; ++i)
  332. {
  333. (*zjxx).instruction[i] = zjxx_buf[i];
  334. }
  335. (*zjxx).packet_len = zjxx_buf[5] * 256 + zjxx_buf[6];
  336. for (i = 0; i < 3; ++i)
  337. {
  338. (*zjxx).user_addr[i] = zjxx_buf[i + 7];
  339. }
  340. (*zjxx).zjxx_info.ic_status = zjxx_buf[10];
  341. (*zjxx).zjxx_info.hw_status = zjxx_buf[11];
  342. (*zjxx).zjxx_info.battery_quantity = zjxx_buf[12];
  343. (*zjxx).zjxx_info.in_station_status = zjxx_buf[13];
  344. for (i = 0; i < 6; ++i)
  345. {
  346. (*zjxx).zjxx_info.power_status[i] = zjxx_buf[14 + i];
  347. }
  348. (*zjxx).checksum = zjxx_buf[20];
  349. }
  350. void parse_fkxx(struct fkxx_struct *fkxx)
  351. {
  352. unsigned int i;
  353. for (i = 0; i < 5; ++i)
  354. {
  355. (*fkxx).instruction[i] = fkxx_buf[i];
  356. }
  357. (*fkxx).packet_len = fkxx_buf[5] * 256 + fkxx_buf[6];
  358. for (i = 0; i < 3; ++i)
  359. {
  360. (*fkxx).user_addr[i] = fkxx_buf[i + 7];
  361. }
  362. (*fkxx).fkxx_info.fk_flag = fkxx_buf[10];
  363. for (i = 0; i < 4; ++i)
  364. {
  365. (*fkxx).fkxx_info.extra_info[i] = fkxx_buf[11 + i];
  366. }
  367. (*fkxx).checksum = fkxx_buf[15];
  368. }
  369. void print_txxx(struct txxx_struct *txxx)
  370. {
  371. unsigned int i;
  372. Tx_monitor_S("\r\n ========= TXXX包-打印开始=========\r\n");
  373. DebugPrintBd("\r\n TXXX包总长度:%d", (*txxx).packet_len);
  374. DebugPrintBd("\r\n TXXX包用户地址:0x%02x%02x%02x", (*txxx).user_addr[0], (*txxx).user_addr[1], (*txxx).user_addr[2]);
  375. DebugPrintBd("\r\n TXXX包信息内容-发送方地址:0x%02x%02x%02x", (*txxx).txxx_info.src_user_addr[0], (*txxx).txxx_info.src_user_addr[1], (*txxx).txxx_info.src_user_addr[2]);
  376. DebugPrintBd("\r\n TXXX包信息内容-发送时间:%02d时%02d分", (*txxx).txxx_info.send_time.hour, (*txxx).txxx_info.send_time.minute);
  377. DebugPrintBd("\r\n TXXX包信息内容-电文长度:%d字节", (*txxx).txxx_info.payload_len / 8);
  378. DebugPrintBd("\r\n TXXX包信息内容-电文内容:");
  379. for (i = 0; i < ((*txxx).txxx_info.payload_len / 8); ++i)
  380. {
  381. DebugPrintBd("%02x ", (*txxx).txxx_info.payload[i]);
  382. }
  383. DebugPrintBd("\r\n TXXX包信息内容-CRC:0x%02x", (*txxx).txxx_info.crc);
  384. DebugPrintBd("\r\n TXXX包校验和:0x%02x", (*txxx).checksum);
  385. DebugPrintBd("\r\n ========= TXXX包-打印结束=========\r\n");
  386. }
  387. void print_icxx(struct icxx_struct *icxx)
  388. {
  389. Tx_monitor_S("\r\n ========= ICXX包-打印开始=========\r\n");
  390. DebugPrintBd("\r\n ICXX包总长度:%d", (*icxx).packet_len);
  391. DebugPrintBd("\r\n ICXX包用户地址:0x%02x%02x%02x", (*icxx).user_addr[0], (*icxx).user_addr[1], (*icxx).user_addr[2]);
  392. DebugPrintBd("\r\n ICXX信息内容-帧号:%d", (*icxx).icxx_info.frame_id);
  393. DebugPrintBd("\r\n ICXX信息内容-通播ID:0x%02x%02x%02x", (*icxx).icxx_info.broadcast_id[0], (*icxx).icxx_info.broadcast_id[1], (*icxx).icxx_info.broadcast_id[2]);
  394. DebugPrintBd("\r\n ICXX信息内容-用户特征:%d", (*icxx).icxx_info.user_feature);
  395. DebugPrintBd("\r\n ICXX信息内容-服务频度:%d秒", (*icxx).icxx_info.service_frequency);
  396. DebugPrintBd("\r\n ICXX信息内容-通信级别:%d", (*icxx).icxx_info.comm_level);
  397. DebugPrintBd("\r\n ICXX信息内容-加密标志:%d", (*icxx).icxx_info.encryption_flag);
  398. DebugPrintBd("\r\n ICXX信息内容-用户数目:%d", (*icxx).icxx_info.user_num);
  399. DebugPrintBd("\r\n ICXX包校验和:0x%02x\r\n", (*icxx).checksum);
  400. DebugPrintBd("\r\n ========= ICXX包-打印结束=========\r\n");
  401. }
  402. void print_zjxx(struct zjxx_struct *zjxx)
  403. {
  404. Tx_monitor_S("\r\n ========= ZJXX包-打印开始=========\r\n");
  405. DebugPrintBd("\r\n ZJXX包总长度:%d", (*zjxx).packet_len);
  406. DebugPrintBd("\r\n ZJXX包用户地址:0x%02x%02x%02x", (*zjxx).user_addr[0], (*zjxx).user_addr[1], (*zjxx).user_addr[2]);
  407. DebugPrintBd("\r\n ZJXX信息内容-IC卡状态:0x%02x", (*zjxx).zjxx_info.ic_status);
  408. DebugPrintBd("\r\n ZJXX信息内容-硬件状态:0x%02x", (*zjxx).zjxx_info.hw_status);
  409. DebugPrintBd("\r\n ZJXX信息内容-电池电量:0x%02x", (*zjxx).zjxx_info.battery_quantity);
  410. DebugPrintBd("\r\n ZJXX信息内容-入站状态:0x%02x", (*zjxx).zjxx_info.in_station_status);
  411. DebugPrintBd("\r\n ZJXX信息内容-功率状态:%d-%d-%d-%d-%d-%d", (*zjxx).zjxx_info.power_status[0], (*zjxx).zjxx_info.power_status[1],
  412. (*zjxx).zjxx_info.power_status[2], (*zjxx).zjxx_info.power_status[3],(*zjxx).zjxx_info.power_status[4], (*zjxx).zjxx_info.power_status[5]);
  413. DebugPrintBd("\r\n ZJXX包校验和:0x%02x\r\n", (*zjxx).checksum);
  414. DebugPrintBd("\r\n ========= ZJXX包-打印结束=========\r\n");
  415. }
  416. void print_fkxx(struct fkxx_struct *fkxx)
  417. {
  418. Tx_monitor_S("\r\n ========= FKXX包-打印开始=========\r\n");
  419. DebugPrintBd("\r\n FKXX包总长度:%d", (*fkxx).packet_len);
  420. DebugPrintBd("\r\n FKXX包用户地址:0x%02x%02x%02x", (*fkxx).user_addr[0], (*fkxx).user_addr[1], (*fkxx).user_addr[2]);
  421. DebugPrintBd("\r\n FKXX信息内容-反馈标志:0x%02x", (*fkxx).fkxx_info.fk_flag);
  422. DebugPrintBd("\r\n FKXX信息内容-附加信息:0x%02x-0x%02x-0x%02x-0x%02x", (*fkxx).fkxx_info.extra_info[0], (*fkxx).fkxx_info.extra_info[1], (*fkxx).fkxx_info.extra_info[2], (*fkxx).fkxx_info.extra_info[3]);
  423. DebugPrintBd("\r\n FKXX包校验和:0x%02x\r\n", (*fkxx).checksum);
  424. DebugPrintBd("\r\n ========= FKXX包-打印结束=========\r\n");
  425. }
  426. void read_bd_rx_info(void)
  427. {
  428. struct txxx_struct txxx;
  429. struct icxx_struct icxx;
  430. struct zjxx_struct zjxx;
  431. struct fkxx_struct fkxx;
  432. if (bd_buf_bitmap & FKXX_BUF)
  433. {
  434. parse_fkxx(&fkxx);
  435. print_fkxx(&fkxx);
  436. bd_buf_bitmap &= ~FKXX_BUF;
  437. }
  438. if (bd_buf_bitmap & ICXX_BUF)
  439. {
  440. parse_icxx(&icxx);
  441. print_icxx(&icxx);
  442. bd_buf_bitmap &= ~ICXX_BUF;
  443. }
  444. if (bd_buf_bitmap & ZJXX_BUF)
  445. {
  446. parse_zjxx(&zjxx);
  447. print_zjxx(&zjxx);
  448. bd_buf_bitmap &= ~ZJXX_BUF;
  449. }
  450. if (bd_buf_bitmap & TXXX_BUF)
  451. {
  452. parse_txxx(&txxx);
  453. print_txxx(&txxx);
  454. bd_buf_bitmap &= ~TXXX_BUF;
  455. }
  456. }
  457. void copy_packet_from_shared_buf()
  458. {
  459. if ((bd_shared_rx_buf[1] == 'D') && (bd_shared_rx_buf[2] == 'W')) //收到定位信息$DWXX
  460. {
  461. bd_buf_bitmap |= DWXX_BUF;
  462. //memcpy(dwxx_buf, bd_shared_rx_buf, sizeof(dwxx_buf));
  463. }
  464. else if ((bd_shared_rx_buf[1] == 'T') && (bd_shared_rx_buf[2] == 'X')) //收到通信信息$TXXX
  465. {
  466. bd_buf_bitmap |= TXXX_BUF;
  467. memcpy(txxx_buf, bd_shared_rx_buf, sizeof(txxx_buf));
  468. }
  469. else if ((bd_shared_rx_buf[1] == 'I') && (bd_shared_rx_buf[2] == 'C')) //收到IC信息$ICXX
  470. {
  471. bd_buf_bitmap |= ICXX_BUF;
  472. memcpy(icxx_buf, bd_shared_rx_buf, sizeof(icxx_buf));
  473. }
  474. else if ((bd_shared_rx_buf[1] == 'Z') && (bd_shared_rx_buf[2] == 'J')) //收到自检信息$ZJXX
  475. {
  476. bd_buf_bitmap |= ZJXX_BUF;
  477. memcpy(zjxx_buf, bd_shared_rx_buf, sizeof(zjxx_buf));
  478. }
  479. else if ((bd_shared_rx_buf[1] == 'S') && (bd_shared_rx_buf[2] == 'J')) //收到时间信息$SJXX
  480. {
  481. bd_buf_bitmap |= SJXX_BUF;
  482. //memcpy(sjxx_buf, bd_shared_rx_buf, sizeof(sjxx_buf));
  483. }
  484. else if ((bd_shared_rx_buf[1] == 'B') && (bd_shared_rx_buf[2] == 'B')) //收到版本信息$BBXX
  485. {
  486. bd_buf_bitmap |= BBXX_BUF;
  487. //memcpy(bbxx_buf, bd_shared_rx_buf, sizeof(bbxx_buf));
  488. }
  489. else if ((bd_shared_rx_buf[1] == 'F') && (bd_shared_rx_buf[2] == 'K')) //收到反馈信息$FKXX
  490. {
  491. bd_buf_bitmap |= FKXX_BUF;
  492. memcpy(fkxx_buf, bd_shared_rx_buf, sizeof(fkxx_buf));
  493. }
  494. }
  495. #pragma vector=USCI_A3_VECTOR
  496. __interrupt void USCI_A3_ISR(void)
  497. {
  498. unsigned int iv_temp;
  499. _DINT();
  500. PUSHALL
  501. iv_temp = UCA3IV;
  502. iv_temp = iv_temp % 16;
  503. if ((iv_temp & 0x02) != 0)
  504. {
  505. bd_rx_char = UCA3RXBUF;
  506. if (bd_rx_char == '$') //开始接收报文
  507. {
  508. bd_buf_pointer = 0;
  509. }
  510. bd_shared_rx_buf[bd_buf_pointer] = bd_rx_char;
  511. if (bd_buf_pointer == 6)//接收到报文中长度字段的第2个字节
  512. {
  513. rx_packet_len = bd_shared_rx_buf[5] * 256 + bd_shared_rx_buf[6];
  514. }
  515. else if (bd_buf_pointer == (rx_packet_len - 1)) //接收到报文最后一个字节
  516. {
  517. copy_packet_from_shared_buf();
  518. }
  519. if (bd_buf_pointer != (rx_packet_len - 1))
  520. {
  521. bd_buf_pointer = (bd_buf_pointer + 1) % RX_BD_MAX_DATA_SIZE; //指针自增
  522. }
  523. else
  524. {
  525. bd_buf_pointer = 0; //收到最后一个字节后
  526. }
  527. }
  528. POPALL
  529. _EINT();
  530. }
  531. #endif
  532. #endif
  533. #ifndef __APPBD_H__
  534. #define __APPBD_H__
  535. #define MAX_PAYLOAD_LEN 210 //即(1680/8)
  536. #define INSTRUCTION_SIZE 5
  537. #define PACKET_LEN_SIZE 2
  538. #define USER_ADDR_SIZE 3
  539. #define CHECKSUM_SIZE 1
  540. #define IPUC (INSTRUCTION_SIZE + PACKET_LEN_SIZE + USER_ADDR_SIZE + CHECKSUM_SIZE)
  541. #define TXSQ_INFO_FIRM_SIZE 7 //即(1 个信息类别 + 3 个用户地址 + 2个电文长度 + 1个应答字节)
  542. #define TXSQ_FIRM_SIZE (IPUC + TXSQ_INFO_FIRM_SIZE)
  543. #define ICJC_INFO_FIRM_SIZE 1 //即帧号,占一个字节
  544. #define ICJC_FIRM_SIZE (IPUC + ICJC_INFO_FIRM_SIZE)
  545. #define XTZJ_INFO_FIRM_SIZE 2 //即自检频度,占二个字节
  546. #define XTZJ_FIRM_SIZE (IPUC + XTZJ_INFO_FIRM_SIZE)
  547. #define ICXX_INFO_FIRM_SIZE 11 //即(1个帧号+3个通播ID+1个用户特征+2个服务频度+1个通信等级+1个加密标志+2个下属用户总数)
  548. #define ICXX_FIRM_SIZE (IPUC + ICXX_INFO_FIRM_SIZE)
  549. #define TXXX_INFO_FIRM_SIZE 9 //即(1个信息类别+3个发信方地址+2个发信时间+2个电文长度+1个CRC标志
  550. #define TXXX_FIRM_SIZE (IPUC + TXXX_INFO_FIRM_SIZE)
  551. #define TXXX_MAX_SIZE (TXXX_FIRM_SIZE + MAX_PAYLOAD_LEN)//TXXX由固定长度和净长度构成
  552. #define FKXX_INFO_FIRM_SIZE 5//即(1个反馈标志+4个附加信息)
  553. #define FKXX_FIRM_SIZE (IPUC + FKXX_INFO_FIRM_SIZE)
  554. #define ZJXX_INFO_FRIM_SIZE 10 //即(1个IC卡状态+1个硬件状态+1个电池电量+1个入站状态+6个功率状态)
  555. #define ZJXX_FIRM_SIZE (IPUC + ZJXX_INFO_FRIM_SIZE)
  556. #define RX_BD_MAX_DATA_SIZE TXXX_MAX_SIZE
  557. #define TXSQ_PAYLOAD_CHINESE 0x44
  558. #define TXSQ_PAYLOAD_BCD 0x46
  559. enum {
  560. DWXX_BUF = (1 << 0),
  561. TXXX_BUF = (1 << 1),
  562. ICXX_BUF = (1 << 2),
  563. ZJXX_BUF = (1 << 3),
  564. SJXX_BUF = (1 << 4),
  565. BBXX_BUF = (1 << 5),
  566. FKXX_BUF = (1 << 6),
  567. };
  568. /* =================== RTU到用户机 ============================ */
  569. /*
  570. 注意:因为发送协议中通信申请(txsq)协议有可变数据内容,使用结构体来表示时,因为要通过串口发送出去,
  571. 无法控制长度,所以发送协议不宜采用结构体表示!
  572. struct peri_to_user_struct
  573. {
  574. struct dwsq_struct dwsq;
  575. struct txsq_struct txsq;
  576. struct cksc_struct cksc;
  577. struct icjc_struct icjc;
  578. struct xtzj_struct xtzj;
  579. struct sjsc_struct sjsc;
  580. struct bbdq_struct bbdq;
  581. };
  582. */
  583. /* =================== 用户机到RTU ============================*/
  584. /* 定位信息 */
  585. struct dwxx_struct
  586. {
  587. unsigned int todo;
  588. };
  589. /* 通信信息 */
  590. struct txxx_info_type_struct
  591. {
  592. unsigned char packet_comm:2;
  593. unsigned char transfer_format:1;
  594. unsigned char ack:1;
  595. unsigned char comm_way:1;
  596. unsigned char has_key:1;
  597. unsigned char rest:2;
  598. };
  599. struct send_time_struct
  600. {
  601. unsigned char hour;
  602. unsigned char minute;
  603. };
  604. struct txxx_info_struct
  605. {
  606. struct txxx_info_type_struct txxx_info_type;
  607. unsigned char src_user_addr[3];
  608. struct send_time_struct send_time;
  609. unsigned int payload_len;
  610. unsigned char payload[MAX_PAYLOAD_LEN];
  611. unsigned char crc;
  612. };
  613. struct txxx_struct
  614. {
  615. unsigned char instruction[5];
  616. unsigned int packet_len; //解析结构体时以整形数据表示其长度
  617. unsigned char user_addr[3];
  618. struct txxx_info_struct txxx_info;
  619. unsigned char checksum;
  620. };
  621. struct icxx_info_struct
  622. {
  623. unsigned char frame_id;
  624. unsigned char broadcast_id[3];
  625. unsigned char user_feature;
  626. unsigned int service_frequency;
  627. unsigned char comm_level;
  628. unsigned char encryption_flag;
  629. unsigned int user_num;
  630. };
  631. /* IC信息 */
  632. struct icxx_struct
  633. {
  634. unsigned char instruction[5];
  635. unsigned int packet_len;
  636. unsigned char user_addr[3];
  637. struct icxx_info_struct icxx_info;
  638. unsigned char checksum;
  639. };
  640. struct zjxx_info_struct
  641. {
  642. unsigned char ic_status;
  643. unsigned char hw_status;
  644. unsigned char battery_quantity;
  645. unsigned char in_station_status;
  646. unsigned char power_status[6];
  647. };
  648. struct zjxx_struct
  649. {
  650. unsigned char instruction[5];
  651. unsigned int packet_len;
  652. unsigned char user_addr[3];
  653. struct zjxx_info_struct zjxx_info;
  654. unsigned char checksum;
  655. };
  656. struct sjxx_struct
  657. {
  658. unsigned int todo;
  659. };
  660. struct bbxx_struct
  661. {
  662. unsigned int todo;
  663. };
  664. struct fkxx_info_struct
  665. {
  666. unsigned char fk_flag;
  667. unsigned char extra_info[4];
  668. };
  669. struct fkxx_struct
  670. {
  671. unsigned char instruction[5];
  672. unsigned int packet_len;
  673. unsigned char user_addr[3];
  674. struct fkxx_info_struct fkxx_info;
  675. unsigned char checksum;
  676. };
  677. /*
  678. struct user_to_peri_struct
  679. {
  680. struct dwxx_struct dwxx;
  681. struct txxx_struct txxx;
  682. struct icxx_struct icxx;
  683. struct zjxx_struct zjxx;
  684. struct sjxx_struct sjxx;
  685. struct bbxx_struct bbxx;
  686. struct fkxx_struct fkxx;
  687. };
  688. */
  689. extern void send_dwsq();
  690. extern void send_txsq(unsigned char cmd, unsigned char *src_user_addr, unsigned char *dst_user_addr,
  691. unsigned char transfer_format, unsigned char *send_txsq_payload, unsigned int send_txsq_payload_len);
  692. extern void send_cksc();
  693. extern void send_icjc();
  694. extern void send_xtzj();
  695. extern void send_sjsc();
  696. extern void send_bbdq();
  697. extern void init_uart3_for_bd();
  698. extern void read_bd_rx_info(void);
  699. #endif

</pre><pre>

                
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/485381
推荐阅读
相关标签
  

闽ICP备14008679号