ml307a.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. #include "ml307a.h"
  2. #include "usart.h"
  3. #include "tt808.h"
  4. #include "rtc.h"
  5. //*结构体
  6. extern Menu_table menu;
  7. extern TT808 tt808;
  8. RTC_DateTypeDef GetData; //获取日期结构体
  9. RTC_TimeTypeDef GetTime; //获取时间结构体
  10. //*整型
  11. uint8_t step4g = 0;
  12. uint8_t resetcount_4g=0;
  13. uint8_t ICCID[30];
  14. //*布尔
  15. bool module4G_F = false;
  16. bool reset4Gmodule = false;
  17. bool MgnssFlag = false;
  18. extern bool refresh;
  19. extern TipsFlag tipsflag;
  20. bool signal = false;
  21. //*字符
  22. char year_str[3];
  23. char month_str[3];
  24. char day_str[3];
  25. char hour_str[3];
  26. char minute_str[3];
  27. char second_str[3];
  28. //*本模块方法
  29. void ML307AReset(void);
  30. void RTCtime(void);
  31. void TcpConn(char *value, uint8_t cid, char *IPaddr, uint16_t port, uint8_t tcpConnmode)
  32. {
  33. char *ptr = value;
  34. ptr += sprintf(ptr, "AT+MIPOPEN=%d,\"TCP\",\"%s\",%d,60,%d\r\n",cid,IPaddr,port,tcpConnmode);
  35. }
  36. uint16_t phonenum[3];
  37. bool phonenull=false;
  38. void ML307A_Init(void)
  39. {
  40. //*phoneNumADDR
  41. Flash_ReadBytes(phonenum, phoneNumADDR, 3);//读取phonenum
  42. if(phonenum[0]==0xFFFF)
  43. {
  44. phonenull=true;
  45. }
  46. else
  47. {
  48. //
  49. }
  50. printf("\t###02\t4G_init...%d\r\n",resetcount_4g);
  51. sendCmd_4G("AT+MGNSS=0\r\n", "OK", 1, 1);//关闭MGNSS
  52. switch(step4g)
  53. {
  54. case 0:
  55. {
  56. if (sendCmd_4G("AT\r\n", "OK", 1, 5))
  57. {
  58. sendCmd_4G("AT+MCFG=\"simhot\",1\r\n", "OK", 1, 1);
  59. printf("#4G模组正常\r\n");
  60. if (sendCmd_4G("AT+ICCID\r\n", "OK", 1, 5))
  61. {
  62. memcpy(ICCID,&U4_4GrecvBuff[8],20);
  63. ICCID[20]=0x00;
  64. ICCID[21]=0x01;//版本号(待修改)
  65. }
  66. step4g++;
  67. }
  68. else
  69. {
  70. ML307AReset();
  71. break;
  72. }
  73. }
  74. case 1:
  75. {
  76. if (sendCmd_4G("AT+CPIN?\r\n", "+CPIN: READY", 1, 5))
  77. {
  78. printf("#4G模组状态正常\r\n");
  79. step4g++;
  80. }
  81. //break;
  82. }
  83. case 2:
  84. {
  85. if (sendCmd_4G("AT+CEREG?\r\n", "+CEREG: 0,1", 1, 5))
  86. {
  87. printf("#4G模组附着正常\r\n");
  88. printf("#4G模组初始化完成\r\n");
  89. step4g++;
  90. tipsflag = Back;
  91. }
  92. //break;
  93. }
  94. case 3:
  95. {
  96. #if open4G == 1
  97. if(sendCmd_4G("AT+MIPCLOSE=1\r\n", "+MIPSEND:", 1, 1))//断开所有平台
  98. {
  99. printf("#断开连接\r\n");
  100. }
  101. char TCPconnvalue[100];
  102. TcpConn(TCPconnvalue, 1, JLPTIP, JLPTPORT,0); //cid=1 tcpConnmode=0 普通模式
  103. if(sendCmd_4G(TCPconnvalue, "+MIPOPEN: 1,0", 1, 1)) //TCP连接 0普通模式 2缓存模式 1透传模式
  104. #endif
  105. {
  106. step4g++;
  107. //printf("#TCP连接完成\r\n");
  108. }
  109. //break;
  110. }
  111. case 4:
  112. {
  113. sendCmd_4G("AT+MIPCFG=\"encoding\",1,1,1\r\n", "OK", 1, 1);//默认输入输出为ASCII,0 发16,1 接16,1
  114. if(!reset4Gmodule)
  115. {
  116. if(sendCmd_4G("AT+MGNSSCFG=\"nmea/mask\",0\r\n", "OK", 1, 1))
  117. {
  118. printf("#使能NMEA信息\r\n");
  119. if(sendCmd_4G("AT+MGNSSLOC=0\r\n", "OK", 1, 1))
  120. {
  121. printf("#关闭自动定位上报\r\n");
  122. if(sendCmd_4G("AT+MGNSS=1\r\n", "OK", 1, 1))
  123. {
  124. printf("#开启MGNSS\r\n");
  125. MgnssFlag=true;
  126. }
  127. }
  128. }
  129. }
  130. else
  131. {
  132. reset4Gmodule=false;
  133. }
  134. printf("#TCP设置完成\r\n");
  135. Time_inquiry(0);
  136. if(tipsflag == NoSIM)
  137. {
  138. module4G_F=false;
  139. }
  140. else
  141. {
  142. module4G_F=true;
  143. }
  144. RTCtime();//写入做参考
  145. break;
  146. }
  147. }
  148. Flash_ReadBytes(sys_mode, SysModeAddr, 1);//读取一个2B的,系统模式
  149. if(sys_mode[0]==0xFFFF)
  150. {
  151. sys_mode[1]=DTU_MODE;//默认进入
  152. menu.current=0xFF;
  153. if(!module4G_F)
  154. {
  155. menu.current=0xBB;
  156. if(tipsflag == NoSIM)
  157. {
  158. tipsflag = NoSIM;
  159. }
  160. else
  161. {
  162. tipsflag = Fail4G;
  163. }
  164. }
  165. }
  166. else if(sys_mode[0]==DTU_MODE)
  167. {
  168. sys_mode[1]=DTU_MODE;
  169. if(sendCmd_BT("AT+ADV=1\r\n","OK",1,1))//开启广播
  170. {
  171. printf("开启广播\r\n");
  172. USART_SendString(&huart5, "AT+DISCONN=1\r\n");
  173. menu.current=0;
  174. }
  175. else if(!module4G_F)
  176. {
  177. menu.current=0xBB;
  178. if(tipsflag == NoSIM)
  179. {
  180. tipsflag = NoSIM;
  181. }
  182. else
  183. {
  184. tipsflag = Fail4G;
  185. }
  186. }
  187. else
  188. {
  189. //Todo
  190. }
  191. }
  192. else if(sys_mode[0]==PDA_MODE)
  193. {
  194. sys_mode[1]=PDA_MODE;
  195. if(sendCmd_BT("AT+ADV=0\r\n","OK",1,1))//关闭广播
  196. {
  197. USART_SendString(&huart5, "AT+DISCONN=0\r\n");
  198. printf("关闭广播\r\n");
  199. menu.current=0;
  200. }
  201. else if(!module4G_F)
  202. {
  203. menu.current=0xBB;
  204. if(tipsflag == NoSIM)
  205. {
  206. tipsflag = NoSIM;
  207. }
  208. else
  209. {
  210. tipsflag = Fail4G;
  211. }
  212. }
  213. else
  214. {
  215. //Todo
  216. }
  217. }
  218. refresh=true;
  219. }
  220. /**
  221. * @breaf 4G发送串口命令
  222. */
  223. uint8_t sendCmd_4G(char *pCmd, char *pRes, uint32_t timeOut, uint8_t sendNum)
  224. {
  225. uint8_t i = 0;
  226. uint32_t time;
  227. for(i = 0; i < sendNum; i++)
  228. {
  229. time = timeOut * 10;
  230. memset(U4_4GrecvBuff,0,sizeof(U4_4GrecvBuff));
  231. USART_SendString(&huart4, pCmd);
  232. HAL_Delay(100);
  233. while(time--)
  234. {
  235. if(strstr((const char *)U4_4GrecvBuff, pRes) != NULL) // 如果检索到关键词
  236. {
  237. return 1;
  238. }
  239. HAL_Delay(100);
  240. }
  241. }
  242. return 0;
  243. }
  244. /**
  245. @brief 重启模块
  246. @param 无
  247. @return 无
  248. */
  249. void ML307AReset(void)
  250. {
  251. step4g=0;
  252. reset4Gmodule=true;
  253. resetcount_4g+=1;
  254. printf("4G Module_Reset\n");
  255. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
  256. HAL_Delay(5000);
  257. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
  258. }
  259. void Pre_processing_4G(void)//传递消息前处理
  260. {
  261. if(module4G_F) //4G启动位
  262. {
  263. module4G_F=false; //鉴权操作
  264. #if open4G ==1
  265. tt808FsFunc(ICCID,22,MessageID_authentication);
  266. #endif
  267. }
  268. }
  269. void Transfer_time(uint8_t outTt[],char in[])//转换时间数据
  270. {
  271. const char *hex = (const char *)in; // 将 uint8_t * 转换为 const char *
  272. int size = strlen(hex) / 2; // 计算二进制数据的长度
  273. unsigned char *bin = (unsigned char *)malloc(size); // 分配足够的内存用于存储二进制数据
  274. hex2bin(bin, hex); // 调用hex2bin函数进行转换
  275. memcpy(outTt, bin, 1); // 将 bin 中的 rlt 个字节复制到 MIPurc1 中 接收到的数据放入MIPurc1中
  276. free(bin); // 释放内存
  277. }
  278. void RTCtime(void)
  279. {
  280. HAL_RTC_GetTime(&hrtc, &GetTime, RTC_FORMAT_BIN);
  281. /* Get the RTC current Date */
  282. HAL_RTC_GetDate(&hrtc, &GetData, RTC_FORMAT_BIN);
  283. tt808.Rtime[0] = GetData.Year;
  284. tt808.Rtime[1] = GetData.Month;
  285. tt808.Rtime[2] = GetData.Date;
  286. tt808.Rtime[3] = GetTime.Hours;
  287. tt808.Rtime[4] = GetTime.Minutes;
  288. tt808.Rtime[5] = GetTime.Seconds;
  289. hexArrayToBcd(tt808.Rtime,6,dblocptr->location.timeBCD);
  290. //printf("%02X%02X%02X%02X%02X%02X\r\n",tt808.Rtime[0],tt808.Rtime[1],tt808.Rtime[2],tt808.Rtime[3],tt808.Rtime[4],tt808.Rtime[5]);
  291. //printf("%02X%02X%02X%02X%02X%02X\r\n",dblocptr->location.timeBCD[0],dblocptr->location.timeBCD[1],dblocptr->location.timeBCD[2],dblocptr->location.timeBCD[3],dblocptr->location.timeBCD[4],dblocptr->location.timeBCD[5]);
  292. time_bj();//转换时间戳
  293. }
  294. void Time_inquiry(uint8_t flag)
  295. {
  296. if(flag==0)//首次问
  297. {
  298. if (sendCmd_4G("AT+CCLK?\r\n", "OK", 1, 1))
  299. {
  300. memcpy(year_str, &U4_4GrecvBuff[8], 2); // 复制两个字符
  301. memcpy(month_str, &U4_4GrecvBuff[11], 2);
  302. memcpy(day_str, &U4_4GrecvBuff[14], 2);
  303. memcpy(hour_str, &U4_4GrecvBuff[17], 2);
  304. memcpy(minute_str, &U4_4GrecvBuff[20], 2);
  305. memcpy(second_str, &U4_4GrecvBuff[23], 2);
  306. year_str[2] = '\0'; // 添加空字符结束符
  307. month_str[2] = '\0';
  308. day_str[2] = '\0';
  309. hour_str[2] = '\0';
  310. minute_str[2] = '\0';
  311. second_str[2] = '\0';
  312. Transfer_time(&tt808.Rtime[0],year_str);
  313. Transfer_time(&tt808.Rtime[1],month_str);
  314. Transfer_time(&tt808.Rtime[2],day_str);
  315. Transfer_time(&tt808.Rtime[3],hour_str);
  316. Transfer_time(&tt808.Rtime[4],minute_str);
  317. Transfer_time(&tt808.Rtime[5],second_str);
  318. SyncTime(tt808.Rtime[0], tt808.Rtime[1], tt808.Rtime[2], tt808.Rtime[3], tt808.Rtime[4], tt808.Rtime[5]);
  319. }
  320. }
  321. else
  322. {
  323. /* 上面通过AT指令问 */
  324. RTCtime();//上电同步后,作业时需再次同步
  325. }
  326. }