ml307a.c 7.9 KB

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