tt808.c 14 KB

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