ml307a.c 6.6 KB

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