tt808.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. #include "tt808.h"
  2. #include "usart.h"
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. //BYTE uint8_t
  6. //WORD uint16_t
  7. //DWORD uint32_t
  8. TT808 tt808;
  9. TCPCenterD tcpd;
  10. TCPClientD tcpc;
  11. uint16_t TCPCenterdatelength=0;//数据中心下行长度
  12. void newpacket(uint8_t *head,uint8_t *body,uint8_t outarr[]);
  13. void packageData(uint8_t flag, uint8_t *head, uint8_t *body, size_t bodylength, uint8_t *escaped_data, size_t *new_length);
  14. void tt808test(uint8_t* Bodyxxt, size_t bodylength, uint8_t XXLSH);
  15. // 发送一包内容为0x30 0x7e 0x08 0x7d 0x55的数据包,则经过封装如下:0x7e 0x30 0x7d 0x02 0x08 0x7d 0x01 0x55 0x7e
  16. /**消息结构
  17. * 标识位 消息头 消息体 校验码 标识位
  18. * 0x7e 11BYTE MORE 1BYTE 0x7e
  19. *
  20. */
  21. /**消息头
  22. *
  23. * 消息ID(WORD) 消息体属性(WORD) 终端ID(BCD[6]) 消息流水号(WORD)
  24. * (0 1) (2 3) (4 5 6 7 8 9) (10 11)
  25. */
  26. /**消息体属性
  27. *
  28. * 消息体长度 数据加密方式 分包 保留
  29. * (0 1 2 3 4 5 6 7 8 9) (10 11 12) 13 (14 15)
  30. */
  31. // 校验码指从消息头开始,同后一字节异或,直到校验码前一个字节,占用一个字节。
  32. void printBinary(uint16_t num) {
  33. for (int i = 15; i >= 0; i--) {
  34. if (num & (1u << i)) {
  35. printf("1");
  36. } else {
  37. printf("0");
  38. }
  39. }
  40. printf("\n");
  41. }
  42. typedef struct Node {
  43. uint8_t data;
  44. struct Node* next;
  45. } Node;
  46. Node* createNode(uint8_t value) {
  47. Node* newNode = (Node*)malloc(sizeof(Node));
  48. newNode->data = value;
  49. newNode->next = NULL;
  50. return newNode;
  51. }
  52. void insertAfter(Node* prevNode, uint8_t value) {
  53. Node* newNode = createNode(value);
  54. newNode->next = prevNode->next;
  55. prevNode->next = newNode;
  56. }
  57. uint8_t escaped_data[100]; // 定义足够大的数组存储转义后的数据
  58. void escape_data(uint8_t* input, int length, uint8_t* escaped_data, size_t* new_length) {
  59. *new_length = 0;
  60. Node* head = createNode(input[0]); // 创建链表头节点
  61. Node* current = head;
  62. escaped_data[(*new_length)++] = input[0]; // 将第一个数据直接存入
  63. for (int i = 1; i < length; i++) {
  64. if (input[i] == 0x7E && (i != length - 1)) {
  65. escaped_data[(*new_length)++] = 0x7D;
  66. escaped_data[(*new_length)++] = 0x02;
  67. }
  68. else if (input[i] == 0x7D) {
  69. escaped_data[(*new_length)++] = 0x7D;
  70. escaped_data[(*new_length)++] = 0x01;
  71. }
  72. else {
  73. escaped_data[(*new_length)++] = input[i];
  74. }
  75. }
  76. // 释放链表内存
  77. current = head;
  78. Node* temp;
  79. while (current != NULL) {
  80. temp = current;
  81. current = current->next;
  82. free(temp);
  83. }
  84. }
  85. //uint8_t escaped_data[100]; // 假设足够大来存储转义后的数据
  86. size_t new_length = 0;
  87. // 数据封装函数
  88. void packageData(uint8_t flag, uint8_t *head, uint8_t *body, size_t bodylength, uint8_t *escaped_data, size_t *new_length)
  89. {
  90. // Calculate total length
  91. uint8_t len = 1 + 12 + bodylength + 1 + 1; // 1 byte for flag + 10 bytes for header + body length + 1 byte for checksum + 1 byte for flag
  92. uint8_t checksum = 0; // Initialize checksum
  93. // Calculate checksum from message header
  94. for (int i = 0; i < 12; i++) {
  95. checksum ^= head[i];
  96. }
  97. for (int i = 0; i < bodylength; i++) {
  98. checksum ^= body[i];
  99. }
  100. // Start building the packet
  101. uint8_t packet[len];
  102. int index = 0;
  103. packet[index++] = flag; // Set the flag
  104. // Copy message header to packet
  105. for (int i = 0; i < 12; i++) {
  106. packet[index++] = head[i];
  107. }
  108. // Copy message body to packet
  109. for (size_t i = 0; i < bodylength; i++) {
  110. packet[index++] = body[i];
  111. }
  112. packet[index++] = checksum; // Set the checksum
  113. packet[index++] = flag; // Set the flag at the end
  114. int packetlength = (sizeof(packet)/sizeof(packet[0]));
  115. // 执行转义处理
  116. escape_data(packet, packetlength, escaped_data, new_length);
  117. // 输出转义后的数据
  118. // for (int i = 0; i < *new_length; i++) {
  119. // printf("%02X ", escaped_data[i]);
  120. // }
  121. // printf("\n");
  122. // char TCPvalue[128];
  123. // char *ptr = TCPvalue;
  124. // ptr += sprintf(ptr, "AT+MIPSEND=1,%d,\"",sizeof(escaped_data));
  125. // for (int i = 0; i < sizeof(escaped_data); i++) {
  126. // ptr += sprintf(ptr, "%02X", escaped_data[i]);
  127. // }
  128. // sprintf(ptr, "\"\r\n");//TCPvalue
  129. // if(sendCmd_4G(TCPvalue, "+MIPSEND:", 1, 3))//发送11BYTE MAX1460
  130. // //if(sendCmd_4G("AT+MIPSEND=1,11,\"12345678900\"\r\n", "+MIPSEND:", 1, 3))//发送11BYTE MAX1460
  131. // {
  132. // printf("#发送完成\r\n");
  133. // }
  134. // memset(cmd_replyArray,0,sizeof(cmd_replyArray));
  135. // memcpy(cmd_replyArray,escaped_data,*new_length);
  136. }
  137. void reCenter(void);
  138. uint16_t xxid =0 ;
  139. uint8_t BusinessData[256] = {0}; // 定义业务数据数组
  140. uint8_t TCPCenterCrc=0;
  141. /**
  142. * @breaf 目前监测 U1485标志位 0x7E 0x2A 0x23 0x7E
  143. */
  144. bool U4re =false;
  145. bool newmac=false;
  146. void TCPCenterDownward(void)//信息处理
  147. {
  148. //MIPurc1
  149. /*测试 由485执行*/ //7E 1235 0000 647475303031 0001 252A 1532 64747531 02 00000000000000000402 101000000003 03 01108026 081010164318 23 1234 7E
  150. /*
  151. xxid = U1recvBuff[1]<<8 | U1recvBuff[2];
  152. if (U1reFlag && U1recvBuff[0] == TT808FLAG && U1recvBuff[14] == TCPHeadBYTE && U1recvBuff[49] == TCPTAILBYTE && U1recvBuff[52] == TT808FLAG && (xxid==XXIDzdy ||xxid==XXIDxfMac1|| xxid==XXIDxfMac2||xxid==XXIDxfMac3||xxid==XXIDxfMac4)) // 业务头尾 下发MAC1
  153. {
  154. U1reFlag = 0;
  155. TCPCenterdatelength = U1recvBuff[13]+1;//长度+1(7E)
  156. //HAL_UART_Transmit_DMA(&huart1, U1recvBuff, 53); //TEST
  157. // 将业务数据存入BusinessData数组中
  158. if(U1recvLength-15==TCPCenterdatelength)//消息包长度
  159. {
  160. //HAL_UART_Transmit_DMA(&huart1, U1recvBuff, 53); //TEST 问询53B
  161. //crc测试暂时不用
  162. //TCPCenterCrc = xorBytes(&U1recvBuff[1],TCPCenterdatelength-1);
  163. tcpd.crc = U1recvBuff[TCPCenterdatelength+12];//50
  164. tcpd.Tcrc = U1recvBuff[TCPCenterdatelength+13];//51
  165. // if(tcpd.crc == TCPCenterCrc)
  166. // {
  167. tcpd.Xxid = (U1recvBuff[1] << 8) | U1recvBuff[2];
  168. tcpd.Xxtsx = (U1recvBuff[3] << 8) | U1recvBuff[4];
  169. for(int i = 0; i < 6; i++){
  170. tcpd.CidNum[i] = U1recvBuff[5 + i];
  171. }
  172. tcpd.Xxlsh = (U1recvBuff[11] << 8) | U1recvBuff[12];
  173. tcpd.workcmd = U1recvBuff[15];
  174. tcpd.Cmd = U1recvBuff[16];
  175. tcpd.LockID = (U1recvBuff[17] << 24) | (U1recvBuff[18] << 16) | (U1recvBuff[19] << 8) | U1recvBuff[20];
  176. tcpd.Gate = U1recvBuff[21];
  177. for(int i = 0; i < 8; i++){
  178. tcpd.Bill[i] = U1recvBuff[22 + i];
  179. }
  180. tcpd.LineCode = (U1recvBuff[30] << 8) | U1recvBuff[31];
  181. for(int i = 0; i < 6; i++){
  182. tcpd.Key[i] = U1recvBuff[32 + i];//KEY暂存mac号
  183. }
  184. tcpd.VaildTime = U1recvBuff[38];
  185. tcpd.standby = (U1recvBuff[39] << 24) | (U1recvBuff[40] << 16) | (U1recvBuff[41] << 8) | U1recvBuff[42];
  186. for(int i = 0; i < 6; i++){
  187. tcpd.Time[i] = U1recvBuff[43 + i];
  188. }
  189. U4re=true;
  190. newmac=true;
  191. // }
  192. }
  193. }
  194. */
  195. /*参考
  196. xxid = MIPurc1[1]<<8 | MIPurc1[2];
  197. if (u4RecvFlag && MIPurc1[0] == TT808FLAG && MIPurc1[14] == TCPHeadBYTE && MIPurc1[49] == TCPTAILBYTE && MIPurc1[52] == TT808FLAG && (xxid==XXIDzdy ||xxid==XXIDxfMac1|| xxid==XXIDxfMac2||xxid==XXIDxfMac3||xxid==XXIDxfMac4)) // 业务头尾
  198. {
  199. u4RecvFlag = 0;
  200. TCPCenterdatelength = MIPurc1[13]+1;//长度+1(7E)
  201. //HAL_UART_Transmit_DMA(&huart1, U1recvBuff, 53); //TEST
  202. // 将业务数据存入BusinessData数组中
  203. if(u4length-15==TCPCenterdatelength)//消息包长度
  204. {
  205. //HAL_UART_Transmit_DMA(&huart1, U1recvBuff, 53); //TEST 问询53B
  206. //crc测试暂时不用
  207. //TCPCenterCrc = xorBytes(&U1recvBuff[1],TCPCenterdatelength-1);
  208. tcpd.crc = MIPurc1[TCPCenterdatelength+12];//50
  209. tcpd.Tcrc = MIPurc1[TCPCenterdatelength+13];//51
  210. // if(tcpd.crc == TCPCenterCrc)
  211. // {
  212. tcpd.Xxid = (MIPurc1[1] << 8) | MIPurc1[2];
  213. tcpd.Xxtsx = (MIPurc1[3] << 8) | MIPurc1[4];
  214. for(int i = 0; i < 6; i++){
  215. tcpd.CidNum[i] = MIPurc1[5 + i];
  216. }
  217. tcpd.Xxlsh = (MIPurc1[11] << 8) | MIPurc1[12];
  218. tcpd.workcmd = MIPurc1[15];
  219. tcpd.Cmd = MIPurc1[16];//30 问询 32远程施封 38远程解封
  220. tcpd.LockID = (MIPurc1[17] << 24) | (MIPurc1[18] << 16) | (MIPurc1[19] << 8) | MIPurc1[20];
  221. tcpd.Gate = MIPurc1[21];
  222. for(int i = 0; i < 8; i++){
  223. tcpd.Bill[i] = MIPurc1[22 + i];
  224. }
  225. tcpd.LineCode = (MIPurc1[30] << 8) | MIPurc1[31];
  226. for(int i = 0; i < 6; i++){
  227. tcpd.Key[i] = MIPurc1[32 + i];
  228. }
  229. tcpd.VaildTime = MIPurc1[38];
  230. tcpd.standby = (MIPurc1[39] << 24) | (MIPurc1[40] << 16) | (MIPurc1[41] << 8) | MIPurc1[42];
  231. for(int i = 0; i < 6; i++){
  232. tcpd.Time[i] = MIPurc1[43 + i];
  233. }
  234. tcpc.Gate = 0x00;
  235. U4re=true;
  236. // }
  237. }
  238. }
  239. */
  240. }
  241. bool genghaunmac =true;
  242. extern Menu_table menu;
  243. void newMacchange(void)
  244. {
  245. //conn AT+CONN=101000000010\r\n
  246. //connected +CONNECTED:0,101000000010
  247. //dis +DISCONN:0,101000000010
  248. //snnum 0010
  249. uint16_t mac = (tcpd.Key[4] << 8) | tcpd.Key[5];
  250. char SNconn[50];
  251. char *ptr = SNconn;
  252. ptr += sprintf(ptr, "AT+CONN=");
  253. for (int i = 0; i < 6; i++) {
  254. ptr += sprintf(ptr, "%02X", tcpd.Key[i]);
  255. }
  256. sprintf(ptr, "\r\n");//TCPvalue
  257. char SNF[50];
  258. char *ptr1 = SNF;
  259. ptr1 += sprintf(ptr1, "+CONNECTED:0,");
  260. for (int i = 0; i < 6; i++) {
  261. ptr1 += sprintf(ptr1, "%02X", tcpd.Key[i]);
  262. }
  263. //sprintf(ptr, "\r\n");//TCPvalue
  264. char SNname[2];
  265. char *ptr2 = SNname;
  266. for (int i = 4; i < 6; i++) {
  267. ptr2 += sprintf(ptr2, "%02X", tcpd.Key[i]);
  268. }
  269. //sprintf(ptr, "\r\n");//TCPvalue
  270. char SND[50];
  271. char *ptr3 = SND;
  272. ptr3 += sprintf(ptr3, "+DISCONN:0,");
  273. for (int i = 0; i < 6; i++) {
  274. ptr3 += sprintf(ptr3, "%02X", tcpd.Key[i]);
  275. }
  276. if(xxid==0x1234)
  277. {
  278. //scanMac();
  279. }
  280. else if(xxid==0x1235)//M1
  281. {
  282. //xxid=0;
  283. menu.SN1name=NULL;
  284. menu.SN1=NULL;
  285. menu.SN1F=NULL;
  286. menu.SN1D=NULL;
  287. erase_flash(Sn1StatusAddr);
  288. erase_flash(Sn1Addr);
  289. erase_flash(serialNum1Addr);
  290. menu.SN1 = SNconn;//AT+CONN=*********\r\n
  291. menu.SN1F = SNF;//+CONNECTED:0,101000000010
  292. menu.SN1name = SNname;
  293. menu.SN1D = SND;
  294. Write_Information(Sn1Addr, mac);
  295. //refresh=true;
  296. tcpc.Gate = 0x01;
  297. xxid=0;
  298. menu.home&=0x0<<3;
  299. //scanMac();
  300. genghaunmac=true;
  301. }
  302. else if(xxid==0x1236)//M2
  303. {
  304. menu.SN2name=NULL;
  305. menu.SN2=NULL;
  306. menu.SN2F=NULL;
  307. menu.SN2D=NULL;
  308. erase_flash(Sn2StatusAddr);
  309. erase_flash(Sn2Addr);
  310. erase_flash(serialNum2Addr);
  311. menu.SN2 = SNconn;//AT+CONN=*********\r\n
  312. menu.SN2F = SNF;//+CONNECTED:0,101000000010
  313. menu.SN2name = SNname;
  314. menu.SN2D = SND;
  315. Write_Information(Sn2Addr, mac);
  316. //refresh=true;
  317. tcpc.Gate = 0x01;
  318. xxid=0;
  319. menu.home&=0x0<<2;
  320. genghaunmac=true;
  321. //scanMac();
  322. }
  323. else if(xxid==0x1237)//M3
  324. {
  325. menu.SN3name=NULL;
  326. menu.SN3=NULL;
  327. menu.SN3F=NULL;
  328. menu.SN3D=NULL;
  329. erase_flash(Sn3StatusAddr);
  330. erase_flash(Sn3Addr);
  331. erase_flash(serialNum3Addr);
  332. menu.SN3 = SNconn;//AT+CONN=*********\r\n
  333. menu.SN3F = SNF;//+CONNECTED:0,101000000010
  334. menu.SN3name = SNname;
  335. menu.SN3D = SND;
  336. Write_Information(Sn3Addr, mac);
  337. //refresh=true;
  338. tcpc.Gate = 0x01;
  339. xxid=0;
  340. menu.home&=0x0<<1;
  341. genghaunmac=true;
  342. //scanMac();
  343. }
  344. else if(xxid==0x1238)//M4
  345. {
  346. menu.SN4name=NULL;
  347. menu.SN4=NULL;
  348. menu.SN4F=NULL;
  349. menu.SN4D=NULL;
  350. erase_flash(Sn4StatusAddr);
  351. erase_flash(Sn4Addr);
  352. erase_flash(serialNum4Addr);
  353. menu.SN4 = SNconn;//AT+CONN=*********\r\n
  354. menu.SN4F = SNF;//+CONNECTED:0,101000000010
  355. menu.SN4name = SNname;
  356. menu.SN4D = SND;
  357. Write_Information(Sn4Addr, mac);
  358. //refresh=true;
  359. tcpc.Gate = 0x01;
  360. xxid=0;
  361. menu.home &= 0x0;
  362. genghaunmac=true;
  363. //scanMac();
  364. }
  365. }
  366. uint8_t hext[256]={0};
  367. uint8_t cmd_replyArray[] = {0};
  368. uint32_t DTUID=0x64747531;
  369. uint16_t length;
  370. void wsj(void);
  371. extern Time time;
  372. void tcphuifu(void);
  373. int tcpdataLength = 0;
  374. void wjwd(void);
  375. void replyCenter(void)
  376. {
  377. U4re=false;
  378. if(tcpd.Cmd==0x30 || tcpd.Cmd==0x32 || tcpd.Cmd==0x38)
  379. {
  380. wsj();//闻讯时间
  381. HAL_Delay(100);
  382. //wjwd();
  383. //HAL_Delay(100);
  384. tcpc.workcmd = tcpd.workcmd;//终端回复
  385. tcpc.Cmd = tcpd.Cmd;
  386. //64 74 75 31 dtu1
  387. tcpc.LockID = ((DTUID << 24)&0xff) | ((DTUID << 16)&0xff) | ((DTUID << 8)&0xff) | DTUID;
  388. //tcpc.Gate = 0x00;//待传入
  389. /* 载货清单 */
  390. tcpc.Bill[0] = (JD_dw>>24)&0xff;//待传入
  391. tcpc.Bill[1] = (JD_dw>>16)&0xff;//待传入
  392. tcpc.Bill[2] = (JD_dw>>8)&0xff;//待传入
  393. tcpc.Bill[3] = (JD_dw)&0xff;//待传入
  394. tcpc.Bill[4] = (WD_dw>>24)&0xff;//待传入
  395. tcpc.Bill[5] = (WD_dw>>16)&0xff;//待传入
  396. tcpc.Bill[6] = (WD_dw>>8)&0xff;//待传入
  397. tcpc.Bill[7] = (WD_dw)&0xff;//待传入
  398. /* 电压 */
  399. tcpc.Voltage = 0x00;//待传入
  400. if(tcpc.Cmd==0x32)//施封
  401. {
  402. if(tcpd.VaildTime==0x01)//锁1施封
  403. {
  404. }
  405. if(tcpd.VaildTime==0x02)//锁2施封
  406. {
  407. }
  408. if(tcpd.VaildTime==0x03)//锁3施封
  409. {
  410. }
  411. if(tcpd.VaildTime==0x04)//锁4施封
  412. {
  413. }
  414. }
  415. if(tcpc.Cmd==0x38)//解封
  416. {
  417. if(tcpd.VaildTime==0x01)//锁1解封
  418. {
  419. }
  420. if(tcpd.VaildTime==0x02)//锁2解封
  421. {
  422. }
  423. if(tcpd.VaildTime==0x03)//锁3解封
  424. {
  425. }
  426. if(tcpd.VaildTime==0x04)//锁4解封
  427. {
  428. }
  429. }
  430. /* 锁状态 */
  431. //tcpc.LockStatus = 0xAA;//待传入
  432. tcpc.LockStatus = tt808.L1Lockstatus;
  433. /* 电机状态 */
  434. //tcpc.MotoStatus = 0x00;//待传入
  435. tcpc.MotoStatus = tt808.L2Lockstatus;
  436. /* 线路编号 */
  437. tcpc.LineCode = ((uint16_t)tt808.L3Lockstatus << 8) | tt808.L4Lockstatus;//待传入
  438. // /* 操作标识码 */
  439. tcpc.OperationIdNum[0] = 0x00;//待传入
  440. tcpc.OperationIdNum[1] = 0x00;//待传入
  441. tcpc.OperationIdNum[2] = 0x00;//待传入
  442. tcpc.OperationIdNum[3] = 0x00;//待传入
  443. tcpc.OperationIdNum[4] = 0x00;//待传入
  444. tcpc.OperationIdNum[5] = 0x00;//待传入
  445. tcpc.OperationIdNum[6] = 0x00;//待传入
  446. tcpc.OperationIdNum[7] = 0x00;//待传入
  447. /* 设备标识码 */
  448. tcpc.cmdSRC = 0x0F;//dtu设备
  449. /* 下发命令时间 */
  450. tcpc.Time[0] = time.year;
  451. tcpc.Time[1] = time.month;
  452. tcpc.Time[2] = time.day;
  453. tcpc.Time[3] = time.hour;
  454. tcpc.Time[4] = time.minute;
  455. tcpc.Time[5] = time.second;
  456. //***
  457. cmd_replyArray[1]=TCPHeadBYTE;
  458. cmd_replyArray[2]=tcpc.workcmd;
  459. cmd_replyArray[3]=tcpc.Cmd;
  460. cmd_replyArray[4]=(tcpc.LockID>>24)&0xff;
  461. cmd_replyArray[5]=(tcpc.LockID>>16)&0xff;
  462. cmd_replyArray[6]=(tcpc.LockID>>8)&0xff;
  463. cmd_replyArray[7]=(tcpc.LockID)&0xff;
  464. cmd_replyArray[8]=tcpc.Gate;
  465. memcpy(&cmd_replyArray[9],tcpc.Bill,8);
  466. cmd_replyArray[17]=tcpc.Voltage;
  467. cmd_replyArray[18]=tcpc.LockStatus;
  468. cmd_replyArray[19]=tcpc.MotoStatus;
  469. cmd_replyArray[20]=(tcpc.LineCode>>8)&0xff;
  470. cmd_replyArray[21]=(tcpc.LineCode)&0xff;
  471. memcpy(&cmd_replyArray[22],tcpc.OperationIdNum,8);
  472. cmd_replyArray[30]=tcpc.cmdSRC;
  473. memcpy(&cmd_replyArray[31],tcpc.Time,6);
  474. cmd_replyArray[0]=sizeof(cmd_replyArray)-1;
  475. cmd_replyArray[37]=TCPTAILBYTE;
  476. cmd_replyArray[38]=xorBytes(&cmd_replyArray[1],37);//第2字节开始校验
  477. tcpdataLength = sizeof(cmd_replyArray) / sizeof(cmd_replyArray[0]);
  478. tcphuifu();
  479. }
  480. }
  481. uint8_t MIPurc[256];
  482. uint8_t MIPurc1[256];
  483. uint8_t MIPurcXXTCSJ[256];//平台下行透传数据
  484. bool MIPurcReFlag = false;
  485. uint16_t hccd = 0;
  486. char high[]={0};
  487. bool chuliSJ = false;
  488. char hex2asc(const char *hex)
  489. {
  490. char asc = 0;
  491. char a = (hex[0]>='A' && hex[0]<='F')?(hex[0]^32):hex[0];
  492. char b = (hex[1]>='A' && hex[1]<='F')?(hex[1]^32):hex[1];
  493. if ((a>='0' && a<='9' || a>='a' && a<='f') && (b>='0' && b<='9' || b>='a' && b<='f')){
  494. asc = ((('a'<=a && a<='f')?(a-'a')+10:a-'0')<<4)|(('a'<=b && b<='f')?(b-'a')+10:b-'0');
  495. }
  496. return asc;
  497. }
  498. char *bin2hex(char *hex, const unsigned char *bin, int size)
  499. {
  500. size_t i;
  501. for(i=0; i<size; i++)
  502. {
  503. sprintf(hex+i*2, "%02x", bin[i]);
  504. }
  505. hex[i+i] = 0;
  506. return hex;
  507. }
  508. int hex2bin(unsigned char *bin, const char *hex)
  509. {
  510. const char *h = hex;
  511. unsigned char *b = bin;
  512. int rlt = 0;
  513. while(*h){
  514. *b++ = hex2asc(h);
  515. h = h + 2;
  516. rlt++;
  517. }
  518. return rlt;//长度
  519. }
  520. int rlt;
  521. char year_str[3];
  522. char month_str[3];
  523. char day_str[3];
  524. char hour_str[3];
  525. char minute_str[3];
  526. char second_str[3];
  527. void wsj(void)
  528. {
  529. if (sendCmd_4G("AT+CCLK?\r\n", "OK", 1, 1))
  530. {
  531. memcpy(year_str, &g_ML307A_8buf_Down[8], 2); // 复制两个字符
  532. memcpy(month_str, &g_ML307A_8buf_Down[11], 2);
  533. memcpy(day_str, &g_ML307A_8buf_Down[14], 2);
  534. memcpy(hour_str, &g_ML307A_8buf_Down[17], 2);
  535. memcpy(minute_str, &g_ML307A_8buf_Down[20], 2);
  536. memcpy(second_str, &g_ML307A_8buf_Down[23], 2);
  537. year_str[2] = '\0'; // 添加空字符结束符
  538. month_str[2] = '\0';
  539. day_str[2] = '\0';
  540. hour_str[2] = '\0';
  541. minute_str[2] = '\0';
  542. second_str[2] = '\0';
  543. time.year = (uint8_t)atoi(year_str); // 使用atoi函数将字符转换为数字并赋值给time.year
  544. time.month = (uint8_t)atoi(month_str);
  545. time.day = (uint8_t)atoi(day_str);
  546. time.hour = (uint8_t)atoi(hour_str)+8;
  547. time.minute = (uint8_t)atoi(minute_str);
  548. time.second = (uint8_t)atoi(second_str);
  549. }
  550. }
  551. uint8_t tcpoutarr[]={0};
  552. uint8_t header[12];
  553. void tcphuifu(void)
  554. {
  555. if(!tcpconn)
  556. {
  557. if(sendCmd_4G("AT+MIPCLOSE=1\r\n", "+MIPSEND:", 1, 1))//发送11BYTE MAX1460
  558. {
  559. printf("#断开连接\r\n");
  560. }
  561. if(sendCmd_4G("AT+MIPOPEN=1,\"TCP\",\"118.31.22.26\",8901,60,0\r\n", "+MIPOPEN: 1,0", 1, 3))//TCP连接
  562. {
  563. printf("#TCP连接成功\r\n");
  564. if(sendCmd_4G("AT+MIPCFG=\"encoding\",1,1,1\r\n", "OK", 1, 3))//默认输入输出为ASCII,0 发16,1 接16,1
  565. {
  566. printf("#TCP设置完成\r\n");
  567. tcpconn=true;
  568. }
  569. }
  570. }
  571. module4G_F=false;
  572. uint16_t XXTSXdata = tcpdataLength;
  573. uint8_t checksum = 0; // Initialize checksum
  574. //XXTSXdata |= ( tcpdataLength | (TCPJMFSRSA<<10)); // 将长度左移3位后和加密方式进行按位或操作(001 RSA ,000 无加密)
  575. XXTSXdata |= ( tcpdataLength | (TCPJMFSNULL<<10)); // 将长度左移3位后和加密方式进行按位或操作(001 RSA ,000 无加密)
  576. if(xxid == XXIDzdy)
  577. {
  578. //xxid=0;
  579. header[0] = ((XXIDzdy>>8)&0xff);
  580. header[1] = (XXIDzdy&0xff);
  581. }
  582. if(xxid == XXIDxfMac1)
  583. {
  584. //xxid=0;
  585. header[0] = ((XXIDxfMac1>>8)&0xff);
  586. header[1] = (XXIDxfMac1&0xff);
  587. }
  588. if(xxid == XXIDxfMac2)
  589. {
  590. //xxid=0;
  591. header[0] = ((XXIDxfMac2>>8)&0xff);
  592. header[1] = (XXIDxfMac2&0xff);
  593. }
  594. if(xxid == XXIDxfMac3)
  595. {
  596. //xxid=0;
  597. header[0] = ((XXIDxfMac3>>8)&0xff);
  598. header[1] = (XXIDxfMac3&0xff);
  599. }
  600. if(xxid == XXIDxfMac4)
  601. {
  602. //xxid=0;
  603. header[0] = ((XXIDxfMac4>>8)&0xff);
  604. header[1] = (XXIDxfMac4&0xff);
  605. }
  606. header[2] = ((XXTSXdata>>8)&0xff);
  607. header[3] = (XXTSXdata&0xff);
  608. header[4] = 0x64;
  609. header[5] = 0x74;
  610. header[6] = 0x75;
  611. header[7] = 0x30;
  612. header[8] = 0x30;
  613. header[9] = 0x31;
  614. header[10] = ((tt808.LSH>>8)&0xff);
  615. header[11] = (tt808.LSH&0xff);
  616. for (int i = 0; i < 12; i++) {
  617. checksum ^= header[i];
  618. }
  619. for (int i = 0; i < tcpdataLength; i++) {
  620. checksum ^= cmd_replyArray[i];
  621. }
  622. ;
  623. uint8_t len = 1 + 12 + tcpdataLength + 1 + 1;
  624. uint8_t tcppacket[len];
  625. int index = 0;
  626. tcppacket[index++] = TT808FLAG; // Set the flag
  627. for (int i = 0; i < 12; i++) {
  628. tcppacket[index++] = header[i];
  629. }
  630. // Copy message body to packet
  631. for (size_t i = 0; i < tcpdataLength; i++) {
  632. tcppacket[index++] = cmd_replyArray[i];
  633. }
  634. uint16_t tcpoutLength = len;
  635. tcppacket[index++] = checksum; // Set the checksum
  636. tcppacket[index++] = TT808FLAG; // Set the flag at the end
  637. char TCPvalue[256];
  638. char *ptr = TCPvalue;
  639. ptr += sprintf(ptr, "AT+MIPSEND=1,%d,\"",tcpoutLength);
  640. for (int i = 0; i < tcpoutLength; i++) {
  641. ptr += sprintf(ptr, "%02X", tcppacket[i]);
  642. }
  643. sprintf(ptr, "\"\r\n");//TCPvalue
  644. if(sendCmd_4G(TCPvalue, "+MIPSEND:", 1, 1))//发送11BYTE MAX1460
  645. {
  646. printf("#发送完成\r\n");
  647. }
  648. module4G_F=true;
  649. tt808.LSH+=1;
  650. memset(MIPurc1,0,rlt);
  651. }
  652. // 7E 0002 0005 0000000000000000 042A002309 03 7E
  653. uint8_t xxtnewlength=0;
  654. uint8_t xxtfz[100];
  655. uint8_t tcppacket[256];
  656. void chuli(uint16_t XXID)
  657. {
  658. uint16_t XXTSXdata = xxtnewlength;
  659. XXTSXdata |= ( tcpdataLength | (TCPJMFSNULL<<10)); // 将长度左移3位后和加密方式进行按位或操作(001 RSA ,000 无加密)
  660. header[0] = ((XXID>>8)&0xff);
  661. header[1] = (XXID&0xff);
  662. header[2] = ((XXTSXdata>>8)&0xff);
  663. header[3] = (XXTSXdata&0xff);
  664. header[4] = 0x00;
  665. header[5] = 0x00;
  666. header[6] = 0x16;
  667. header[7] = 0x00;
  668. header[8] = 0x00;
  669. header[9] = 0x08;
  670. header[10] = ((tt808.LSH>>8)&0xff);;
  671. header[11] = (tt808.LSH&0xff);;
  672. int index = 0;
  673. tcppacket[index++] = TT808FLAG; // Set the flag
  674. for (int i = 0; i < 12; i++) {
  675. tcppacket[index++] = header[i];
  676. }
  677. // Copy message body to packet
  678. for (size_t i = 0; i < xxtnewlength; i++) {
  679. tcppacket[index++] = xxtfz[i];
  680. }
  681. tcppacket[index++] = xorBytes(&tcppacket[1],12+xxtnewlength);; // Set the checksum
  682. tcppacket[index++] = TT808FLAG; // Set the flag at the end
  683. // for (int i = 0; i < index; i++) {
  684. // printf("%02X ", tcppacket[i]); // 以十六进制格式打印每个元素
  685. // }
  686. char TCPvalue[256];
  687. char *ptr = TCPvalue;
  688. ptr += sprintf(ptr, "AT+MIPSEND=1,%d,\"",index);
  689. for (int i = 0; i < index; i++) {
  690. ptr += sprintf(ptr, "%02X", tcppacket[i]);
  691. }
  692. sprintf(ptr, "\"\r\n");//TCPvalue
  693. if(sendCmd_4G(TCPvalue, "+MIPSEND:", 1, 1))//发送11BYTE MAX1460
  694. {
  695. printf("#发送完成\r\n");
  696. }
  697. tt808.LSH+=1;
  698. }
  699. /*需要传入消息体长度sizeof(xxt)/sizeof(xxt[0]);*/
  700. void tt808FsFunc(uint8_t xxt[], uint8_t xxtlength)
  701. {
  702. xxtnewlength = xxtFZ(xxt, xxtlength, xxtfz);
  703. //chuli(XXIDzdxt);
  704. chuli(XXIDzdjq);
  705. }
  706. uint8_t xxtFZ(uint8_t xxt[], uint8_t xxtlength, uint8_t outXxt[])
  707. {
  708. int index = 0;
  709. uint8_t newlength = xxtlength+3;
  710. uint8_t newXxt[newlength];
  711. newXxt[index++]=newlength;
  712. newXxt[index++]=0x2A;
  713. for (int i = 0; i < xxtlength; i++) {
  714. newXxt[index++] = xxt[i]; // 将消息头拷贝到报文中
  715. }
  716. newXxt[index++]=0x23;
  717. newXxt[index++]=xorBytes(&newXxt[1],newlength-1);
  718. newlength = index;
  719. for(int i =0;i<newlength;i++)
  720. {
  721. outXxt[i]=newXxt[i];
  722. }
  723. //test
  724. // for (int i = 0; i < newlength; i++) {
  725. // printf("%02X ", newXxt[i]); // 以十六进制格式打印每个元素
  726. // }
  727. return newlength;
  728. }