tt808.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. #include "tt808.h"
  2. #include "usart.h"
  3. #include "key.h"
  4. #include <stdio.h>
  5. #include <time.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. //*整型
  9. int rlt;
  10. uint8_t xxtnewlength=0;
  11. uint16_t uploadtime=0; //上传时间
  12. uint16_t hccd = 0;//ascii个数(*2)
  13. uint8_t xxtfzData[100]; //放入封装后数据
  14. uint8_t tcppacket[100]; //消息数据
  15. uint8_t header[12]; //处理头部
  16. uint8_t index_2c = 0;
  17. uint8_t MIPurc[100]; //接收平台数据
  18. uint8_t MIPurc1[100]; //接收平台数据
  19. uint8_t MIPurcXXTCSJ[100];//平台下行透传数据
  20. uint8_t speedBegin=0;
  21. uint8_t speedEnd=0;
  22. uint8_t speed[10]; //处理头部
  23. double dSpeed=0;
  24. double dWd=0;
  25. double dJd=0;
  26. char cSpeed[10];
  27. char wd[15];
  28. char jd[15];
  29. char MGNSSLOC[100]; //接收定位数据
  30. char MGNSSLOC_time[6]; //接收定位数据
  31. uint32_t Stamp =0;
  32. uint8_t GNSSdata[40]={0};
  33. //*结构体
  34. TT808 tt808;
  35. extern TipsFlag tipsflag;
  36. extern Menu_table menu;
  37. extern bool refresh;
  38. extern Database db;
  39. //*布尔
  40. bool ZDJQflag = false;
  41. bool GnssFlag = false;
  42. void chuli(uint16_t XXID)
  43. {
  44. uint16_t XXTSXdata = xxtnewlength;
  45. XXTSXdata |= ( xxtnewlength | (TCPJMFSNULL<<10)); // 将长度左移3位后和加密方式进行按位或操作(001 RSA ,000 无加密)
  46. header[0] = ((XXID>>8)&0xff);
  47. header[1] = (XXID&0xff); //消息ID
  48. header[2] = ((XXTSXdata>>8)&0xff);
  49. header[3] = (XXTSXdata&0xff); //消息属性
  50. header[4] = 0x00;
  51. header[5] = 0x00;
  52. header[6] = 0x16;
  53. header[7] = 0x00;
  54. header[8] = 0x00;
  55. header[9] = 0x08; //“手机号”
  56. header[10] = ((tt808.LSH>>8)&0xff);;
  57. header[11] = (tt808.LSH&0xff);;
  58. int index = 0;
  59. tcppacket[index++] = TT808FLAG; // Set the headflag 0x7E
  60. for (int i = 0; i < 12; i++) {
  61. tcppacket[index++] = header[i];
  62. }
  63. // Copy message body to packet
  64. for (size_t i = 0; i < xxtnewlength; i++) {
  65. tcppacket[index++] = xxtfzData[i];
  66. }
  67. tcppacket[index++] = xorBytes(&tcppacket[1],12+xxtnewlength);; // Set the checksum
  68. tcppacket[index++] = TT808FLAG; // Set the flag at the end
  69. // for (int i = 0; i < index; i++) {
  70. // printf("%02X ", tcppacket[i]); // 以十六进制格式打印每个元素
  71. // }
  72. char TCPvalue[100];
  73. char *ptr = TCPvalue;
  74. ptr += sprintf(ptr, "AT+MIPSEND=1,%d,\"",index);
  75. for (int i = 0; i < index; i++) {
  76. ptr += sprintf(ptr, "%02X", tcppacket[i]);
  77. }
  78. sprintf(ptr, "\"\r\n");//TCPvalue
  79. if(sendCmd_4G(TCPvalue, "+MIPSEND:", 1, 1))//发送
  80. {
  81. printf("#发送完成\r\n");
  82. }
  83. tt808.LSH+=1;//需做写入处理
  84. }
  85. /**
  86. * @breaf 消息体封装
  87. * xxt[] 消息数据; xxtlength 消息数据长度; outXxt[] 封装后消息数据; XXID 消息ID
  88. */
  89. uint8_t xxtFZ(uint8_t xxt[], uint8_t xxtlength, uint8_t outXxt[],uint16_t XXID)
  90. {
  91. int index = 0;
  92. uint8_t newlength = 0;//加首尾 25/23/crc
  93. if(XXID==XXID_zdjq || XXID==XXID_zdxt)//不加头尾(鉴权、心跳)
  94. {
  95. newlength = xxtlength;//不加首尾
  96. uint8_t newXxt[newlength];
  97. for (int i = 0; i < xxtlength; i++) {
  98. newXxt[index++] = xxt[i]; // 将消息头拷贝到报文中
  99. }
  100. newlength = index;
  101. for(int i =0;i<newlength;i++)
  102. {
  103. outXxt[i]=newXxt[i];
  104. }
  105. }
  106. else if(XXID==XXID_zddz) //终端地址
  107. {
  108. newlength = xxtlength;//不加首尾
  109. uint8_t newXxt[newlength];
  110. for (int i = 0; i < xxtlength; i++) {
  111. newXxt[index++] = xxt[i]; // 将消息头拷贝到报文中
  112. }
  113. //*begin
  114. newXxt[index++] = 0xEB;
  115. //待添加
  116. //*end
  117. newlength = index;
  118. for(int i =0;i<newlength;i++)
  119. {
  120. outXxt[i]=newXxt[i];
  121. }
  122. }
  123. else//加首尾
  124. {
  125. newlength = xxtlength+3;//加首尾 25/23/crc
  126. uint8_t newXxt[newlength];
  127. newXxt[index++]=newlength;
  128. newXxt[index++]=0x2A;
  129. for (int i = 0; i < xxtlength; i++) {
  130. newXxt[index++] = xxt[i]; // 将消息头拷贝到报文中
  131. }
  132. newXxt[index++]=0x23;
  133. newXxt[index++]=xorBytes(&newXxt[1],newlength-1);
  134. newlength = index;
  135. for(int i =0;i<newlength;i++)
  136. {
  137. outXxt[i]=newXxt[i];
  138. }
  139. }
  140. //test
  141. // for (int i = 0; i < newlength; i++) {
  142. // printf("%02X ", newXxt[i]); // 以十六进制格式打印每个元素
  143. // }
  144. return newlength;
  145. }
  146. /*需要传入消息体长度sizeof(xxt)/sizeof(xxt[0]);*/
  147. void tt808FsFunc(uint8_t xxt[], uint8_t xxtlength, uint16_t XXID)
  148. {
  149. HAL_Delay(200);
  150. xxtnewlength = xxtFZ(xxt, xxtlength, xxtfzData, XXID);
  151. chuli(XXID);
  152. }
  153. // 函数用于提取第7到第8个逗号之间的字符串
  154. void extractString(char* receiveStr, char* result, uint8_t start, uint16_t end)
  155. {
  156. int comma_count = 0;
  157. int start_index = -1;
  158. int end_index = -1;
  159. int length = strlen(receiveStr);
  160. // 遍历字符串,统计逗号数量和记录起始和结束索引
  161. for (int i = 0; i < length; i++)
  162. {
  163. if (receiveStr[i] == ',')
  164. {
  165. comma_count++;
  166. if (comma_count == start)
  167. {
  168. start_index = i + 1; // 第start个逗号之后的字符
  169. }
  170. else if (comma_count == end)
  171. {
  172. if(start==1 && end==2)//*纬度去尾符号
  173. {
  174. end_index = i-1; // 第end个逗号之前的字符
  175. }
  176. else if(start==2 && end==3)//*经度去尾符号
  177. {
  178. end_index = i-1; // 第end个逗号之前的字符
  179. }
  180. else
  181. {
  182. end_index = i; // 第end个逗号之前的字符
  183. }
  184. break;
  185. }
  186. }
  187. }
  188. // 提取字符串并存储在result数组中
  189. if (start_index != -1 && end_index != -1 && start_index < end_index)
  190. {
  191. strncpy(result, receiveStr + start_index, end_index - start_index);
  192. result[end_index - start_index] = '\0'; // 添加字符串结束符
  193. }
  194. }
  195. double convertToDouble(char* str) {
  196. double result;
  197. sscanf(str, "%lf", &result);
  198. return result;
  199. }
  200. char hex2asc(const char *hex)
  201. {
  202. char asc = 0;
  203. char a = (hex[0]>='A' && hex[0]<='F')?(hex[0]^32):hex[0];
  204. char b = (hex[1]>='A' && hex[1]<='F')?(hex[1]^32):hex[1];
  205. if ((a>='0' && a<='9' || a>='a' && a<='f') && (b>='0' && b<='9' || b>='a' && b<='f')){
  206. asc = ((('a'<=a && a<='f')?(a-'a')+10:a-'0')<<4)|(('a'<=b && b<='f')?(b-'a')+10:b-'0');
  207. }
  208. return asc;
  209. }
  210. char *bin2hex(char *hex, const unsigned char *bin, int size)
  211. {
  212. size_t i;
  213. for(i=0; i<size; i++)
  214. {
  215. sprintf(hex+i*2, "%02x", bin[i]);
  216. }
  217. hex[i+i] = 0;
  218. return hex;
  219. }
  220. int hex2bin(unsigned char *bin, const char *hex)
  221. {
  222. const char *h = hex;
  223. unsigned char *b = bin;
  224. int rlt = 0;
  225. while(*h){
  226. *b++ = hex2asc(h);
  227. h = h + 2;
  228. rlt++;
  229. }
  230. return rlt;//长度
  231. }
  232. void ReMIPURC(void)
  233. {
  234. //4D 49 50 55 52 43
  235. if(U4_4GrecvBuff[1]==0x4D && U4_4GrecvBuff[2]==0x49 && U4_4GrecvBuff[3]==0x50 \
  236. && U4_4GrecvBuff[4]==0x55 && U4_4GrecvBuff[5]==0x52 && U4_4GrecvBuff[6]==0x43 ) //监测平台回复+MIPURC:
  237. {
  238. uint8_t high_byte = (U4_4GrecvBuff[18]&0x0f) *10;
  239. uint8_t low_byte = U4_4GrecvBuff[19]&0x0f;
  240. hccd = high_byte + low_byte;
  241. memcpy(MIPurc1,&U4_4GrecvBuff[21],hccd*2);
  242. const char *hex = (const char *)MIPurc1; // 将 uint8_t * 转换为 const char *
  243. int size = strlen(hex) / 2; // 计算二进制数据的长度
  244. unsigned char *bin = (unsigned char *)malloc(size); // 分配足够的内存用于存储二进制数据
  245. rlt = hex2bin(bin, hex); // 调用hex2bin函数进行转换
  246. memcpy(MIPurc, bin, rlt); // 将 bin 中的 rlt 个字节复制到 MIPurc1 中 接收到的数据放入MIPurc1中
  247. free(bin); // 释放内存
  248. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  249. memset(MIPurc1,0,100);
  250. }
  251. //时间 纬度(北纬) 经度(东经) 水平精度因子 海拔高度 定位类型(1无,2 2d,3 3d) 运动角度 水平运动速度(KM/h) 水平运动速度(Knots) 日月年 卫星数量 差分定位标识(1单点,2差分)
  252. //eg.+MGNSSLOC: 015032.301,3014.8605N,12001.9250E,1.8, 17.6, 3, 0.00, 3.6, 1.9, 080424, 12, 1
  253. else if((U4_4GrecvBuff[2]==0x47 || (U4_4GrecvBuff[0]==0x4F && U4_4GrecvBuff[1]==0x4B)) && MgnssFlag) //监测平台回复+MGNSSLOC OK4f4b
  254. {
  255. memcpy(MGNSSLOC,&U4_4GrecvBuff[11],75);
  256. GnssFlag=true;
  257. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  258. }
  259. else if(U5_BTrecvBuff[0]==0x66 && U5_BTrecvBuff[1]==0x00 && U5_BTrecvBuff[2]==0x02 && U5_BTrecvBuff[3]==0x01)//解封接收
  260. {
  261. if(U5_BTrecvBuff[4]==0x01)
  262. {//接收完成
  263. tipsflag = FinshOK;
  264. HAL_Delay(1000);
  265. if(preworkUp==3)
  266. {
  267. SET_MENU_STATUS(4,1,0,3);
  268. }
  269. else if(preworkUp==2)
  270. {
  271. SET_MENU_STATUS(5,1,0,3);
  272. }
  273. else if(preworkUp==1)
  274. {
  275. SET_MENU_STATUS(6,1,0,3);
  276. }
  277. else if(preworkUp==0)
  278. {
  279. SET_MENU_STATUS(7,1,0,3);
  280. }
  281. //USART_SendString(&huart5, "AT+DISCONN=0\r\n");//断开连接
  282. }
  283. else if(U5_BTrecvBuff[4]==0x02)
  284. {//流水号异常
  285. }
  286. }
  287. else if(U5_BTrecvBuff[0]==0x66 && U5_BTrecvBuff[1]==0x00 && U5_BTrecvBuff[2]==0x03 && U5_BTrecvBuff[3]==0x01)//解封接收
  288. {
  289. if(U5_BTrecvBuff[4]==0x01)
  290. {//接收完成
  291. tipsflag = FinshOK;
  292. HAL_Delay(1000);
  293. if(preworkUp==3)
  294. {
  295. SET_MENU_STATUS(4,1,0,3);
  296. }
  297. else if(preworkUp==2)
  298. {
  299. SET_MENU_STATUS(5,1,0,3);
  300. }
  301. else if(preworkUp==1)
  302. {
  303. SET_MENU_STATUS(6,1,0,3);
  304. }
  305. else if(preworkUp==0)
  306. {
  307. SET_MENU_STATUS(7,1,0,3);
  308. }
  309. //USART_SendString(&huart5, "AT+DISCONN=0\r\n");//断开连接
  310. }
  311. else if(U5_BTrecvBuff[4]==0x02)
  312. {//流水号异常
  313. }
  314. }
  315. else if(U5_BTrecvBuff[0]==0x2B && U5_BTrecvBuff[1]==0x43 && U5_BTrecvBuff[2]==0x50 && U5_BTrecvBuff[3]==0x49 && U5_BTrecvBuff[4]==0x4E && \
  316. U5_BTrecvBuff[5]==0x3A && U5_BTrecvBuff[6]==0x20 && U5_BTrecvBuff[7]==0x53 && U5_BTrecvBuff[8]==0x49 && U5_BTrecvBuff[9]==0x4D && U5_BTrecvBuff[10]==0x20 && U5_BTrecvBuff[11]==0x52)//+CPIN: SIM R
  317. {
  318. reset4Gmodule = true;
  319. }
  320. }
  321. void MIPURCHandle(void)
  322. {
  323. if(MIPurc[0]==0x7E)
  324. {
  325. uint16_t PTXXID = MIPurc[1]<<8 | MIPurc[2]; //消息ID
  326. uint16_t xxtcLen = MIPurc[3]<<8 | MIPurc[4]; //数据长度
  327. uint16_t xxRE = MIPurc[15]<<8 | MIPurc[16]; //回复ID
  328. uint8_t YESorNO = MIPurc[17]; //00 success ; 01 fail
  329. if(PTXXID==XXID_pttyyd) //收到平台通用应答0x8001
  330. {
  331. if(xxRE == XXID_zdjq)//回复鉴权ID
  332. {
  333. if(YESorNO==YES)
  334. {
  335. printf("@鉴权应答yes\r\n");
  336. ZDJQflag = true;
  337. }
  338. else if(YESorNO==NO)
  339. {
  340. printf("@鉴权应答no\r\n");
  341. }
  342. memset(MIPurc,0,rlt);//rlt下发长度
  343. }
  344. if(xxRE == XXID_zdxt)//回复鉴权ID
  345. {
  346. if(YESorNO==YES)
  347. {
  348. printf("@心跳应答yes\r\n");
  349. }
  350. else if(YESorNO==NO)
  351. {
  352. printf("@心跳应答no\r\n");
  353. }
  354. memset(MIPurc,0,rlt);//rlt下发长度
  355. }
  356. }
  357. else if(PTXXID == XXID_ptxxtc) //收到平台消息透传 8900
  358. {
  359. uint8_t tcsjLen = MIPurc[13];
  360. memcpy(MIPurcXXTCSJ,&MIPurc[13],xxtcLen);
  361. printf("@得到透传数据,%d\r\n",tcsjLen);
  362. memset(MIPurc,0,rlt);//rlt下发长度
  363. }
  364. }
  365. else if(ZDJQflag)
  366. {
  367. ZDJQflag = false;
  368. HAL_Delay(500);
  369. uint8_t test[] = {0xAA}; //test心跳
  370. tt808FsFunc(test,1,XXID_zdxt); //test心跳
  371. uploadtime = HAL_GetTick();//获取首次发送状态信息基准时间
  372. }
  373. }
  374. time_t timestamp ;
  375. void time_bj(void) {
  376. struct tm timeinfo;
  377. timeinfo.tm_year = tt808.Rtime[0]+2000-1900;
  378. timeinfo.tm_mon = tt808.Rtime[1]-1;
  379. timeinfo.tm_mday = tt808.Rtime[2];
  380. timeinfo.tm_hour = tt808.Rtime[3];
  381. timeinfo.tm_min = tt808.Rtime[4];
  382. timeinfo.tm_sec = tt808.Rtime[5];
  383. timestamp = mktime(&timeinfo);
  384. db.timeStamp[0] = (timestamp >> 16)&0xFFFF;
  385. db.timeStamp[1] = (timestamp)&0xFFFF;
  386. }
  387. void ReU4Proces(void)
  388. {
  389. if(GnssFlag)
  390. {
  391. GnssFlag=false;
  392. RTCtime();//同步时间
  393. // 调用函数提取字符串
  394. extractString(MGNSSLOC, cSpeed,7 ,8);//采集
  395. dSpeed = convertToDouble(cSpeed); //转换浮点数
  396. dSpeed*=100;
  397. // 调用函数提取字符串
  398. extractString(MGNSSLOC, wd,1 ,2);//采集
  399. dWd = convertToDouble(wd); //转换浮点数
  400. dWd*=10000;
  401. // 调用函数提取字符串
  402. extractString(MGNSSLOC, jd,2 ,3);//采集
  403. dJd = convertToDouble(jd); //转换浮点数
  404. dJd*=10000;
  405. if(dSpeed !=0 && dWd!=0 &&dJd!=0) //有数据进行更新!
  406. {
  407. db.speed[0] = (uint16_t)dSpeed;
  408. db.WD_u16[0] = ((uint32_t)dWd >> 16) &0xffff;
  409. db.WD_u16[1] = ((uint32_t)dWd) &0xffff; // 打印提取到的字符串
  410. db.JD_u16[0] = ((uint32_t)dJd >> 16) &0xffff;
  411. db.JD_u16[1] = ((uint32_t)dJd) &0xffff;
  412. // printf("提取到的字符串为:%s\n", speed);
  413. // printf("Converted double: %.f\n", dSpeed);
  414. // printf("Converted double: %04X\n", db.speed[0]);
  415. // printf("提取到的字符串为:%s\n", wd);
  416. // printf("Converted double: %.f\n", dWd);
  417. // printf("Converted double: %04X%04X\n", db.WD_u16[0],db.WD_u16[1]);
  418. // printf("提取到的字符串为:%s\n", jd);
  419. // printf("Converted double: %.f\n", dJd);
  420. // printf("Converted double: %04X%04X\n", db.JD_u16[0],db.JD_u16[1]);
  421. if(menu.current == 0xBB)
  422. {
  423. menu.current = 0;
  424. refresh=true;
  425. }
  426. }
  427. else
  428. {
  429. if(signal)
  430. {
  431. //signal = false;
  432. menu.current = 0xBB;
  433. tipsflag = Nosignal;
  434. refresh=true;
  435. }
  436. }
  437. //printf("Time: %04X%04X\n", db.timeStamp[0],db.timeStamp[1]);
  438. memset(MGNSSLOC,0,100);
  439. }
  440. }