#include "ml307a.h" #include "usart.h" #include "tt808.h" #include "rtc.h" //*结构体 extern Menu_table menu; extern TT808 tt808; RTC_DateTypeDef GetData; //获取日期结构体 RTC_TimeTypeDef GetTime; //获取时间结构体 //*整型 uint8_t step4g = 0; uint8_t resetcount_4g=0; uint8_t ICCID[30]; //*布尔 bool module4G_F = false; bool reset4Gmodule = false; bool MgnssFlag = false; extern bool refresh; extern TipsFlag tipsflag; bool signal = false; //*字符 char year_str[3]; char month_str[3]; char day_str[3]; char hour_str[3]; char minute_str[3]; char second_str[3]; //*本模块方法 void ML307AReset(void); void RTCtime(void); void TcpConn(char *value, uint8_t cid, char *IPaddr, uint16_t port, uint8_t tcpConnmode) { char *ptr = value; ptr += sprintf(ptr, "AT+MIPOPEN=%d,\"TCP\",\"%s\",%d,60,%d\r\n",cid,IPaddr,port,tcpConnmode); } void ML307A_Init(void) { printf("\t###02\t4G_init...%d\r\n",resetcount_4g); sendCmd_4G("AT+MGNSS=0\r\n", "OK", 1, 1);//关闭MGNSS switch(step4g) { case 0: { if (sendCmd_4G("AT\r\n", "OK", 1, 5)) { sendCmd_4G("AT+MCFG=\"simhot\",1\r\n", "OK", 1, 1); printf("#4G模组正常\r\n"); if (sendCmd_4G("AT+ICCID\r\n", "OK", 1, 5)) { memcpy(ICCID,&U4_4GrecvBuff[8],20); ICCID[20]=0x00; ICCID[21]=0x01;//版本号(待修改) } step4g++; } else { ML307AReset(); break; } } case 1: { if (sendCmd_4G("AT+CPIN?\r\n", "+CPIN: READY", 1, 5)) { printf("#4G模组状态正常\r\n"); step4g++; } //break; } case 2: { if (sendCmd_4G("AT+CEREG?\r\n", "+CEREG: 0,1", 1, 5)) { printf("#4G模组附着正常\r\n"); printf("#4G模组初始化完成\r\n"); step4g++; tipsflag = Back; } //break; } case 3: { if(sendCmd_4G("AT+MIPCLOSE=1\r\n", "+MIPSEND:", 1, 1))//断开所有平台 { printf("#断开连接\r\n"); } char TCPconnvalue[100]; TcpConn(TCPconnvalue, 1, JLPTIP, JLPTPORT,0); //cid=1 tcpConnmode=0 普通模式 if(sendCmd_4G(TCPconnvalue, "+MIPOPEN: 1,0", 1, 1)) //TCP连接 0普通模式 2缓存模式 1透传模式 { step4g++; printf("#TCP连接完成\r\n"); // if(sendCmd_4G("AT+MDIALUPCFG=\"auto\",1\r\n", "OK", 1, 5)) // { // printf("#开启自动拨号\r\n"); // } } //break; } case 4: { sendCmd_4G("AT+MIPCFG=\"encoding\",1,1,1\r\n", "OK", 1, 1);//默认输入输出为ASCII,0 发16,1 接16,1 if(!reset4Gmodule) { if(sendCmd_4G("AT+MGNSSCFG=\"nmea/mask\",0\r\n", "OK", 1, 1)) { printf("#使能NMEA信息\r\n"); if(sendCmd_4G("AT+MGNSSLOC=0\r\n", "OK", 1, 1)) { printf("#关闭自动定位上报\r\n"); if(sendCmd_4G("AT+MGNSS=1\r\n", "OK", 1, 1)) { printf("#开启MGNSS\r\n"); // if(sendCmd_4G("AT+MUECONFIG=\"autoconn\",1\r\n", "OK", 1, 5)) // { // printf("#开启自动联网\r\n"); // } MgnssFlag=true; } } } } else { reset4Gmodule=false; } printf("#TCP设置完成\r\n"); Time_inquiry(0); if(tipsflag == NoSIM) { module4G_F=false; } else { module4G_F=true; } RTCtime();//写入做参考 break; } } Flash_ReadBytes(sys_mode, SysModeAddr, 1);//读取一个2B的,系统模式 if(sys_mode[0]==0xFFFF) { sys_mode[1]=DTU_MODE;//默认进入 menu.current=0xFF; if(!module4G_F) { menu.current=0xBB; if(tipsflag == NoSIM) { tipsflag = NoSIM; } else { tipsflag = Fail4G; } } } else if(sys_mode[0]==DTU_MODE) { sys_mode[1]=DTU_MODE; if(sendCmd_BT("AT+ADV=1\r\n","OK",1,1))//开启广播 { printf("开启广播\r\n"); USART_SendString(&huart5, "AT+DISCONN=1\r\n"); menu.current=0; } else if(!module4G_F) { menu.current=0xBB; if(tipsflag == NoSIM) { tipsflag = NoSIM; } else { tipsflag = Fail4G; } } else { //Todo } } else if(sys_mode[0]==PDA_MODE) { sys_mode[1]=PDA_MODE; if(sendCmd_BT("AT+ADV=0\r\n","OK",1,1))//关闭广播 { USART_SendString(&huart5, "AT+DISCONN=0\r\n"); printf("关闭广播\r\n"); menu.current=0; } else if(!module4G_F) { menu.current=0xBB; if(tipsflag == NoSIM) { tipsflag = NoSIM; } else { tipsflag = Fail4G; } } else { //Todo } } refresh=true; } /** * @breaf 4G发送串口命令 */ uint8_t sendCmd_4G(char *pCmd, char *pRes, uint32_t timeOut, uint8_t sendNum) { uint8_t i = 0; uint32_t time; for(i = 0; i < sendNum; i++) { time = timeOut * 10; memset(U4_4GrecvBuff,0,sizeof(U4_4GrecvBuff)); USART_SendString(&huart4, pCmd); HAL_Delay(100); while(time--) { if(strstr((const char *)U4_4GrecvBuff, pRes) != NULL) // 如果检索到关键词 { return 1; } HAL_Delay(100); } } return 0; } /** @brief 重启模块 @param 无 @return 无 */ void ML307AReset(void) { step4g=0; reset4Gmodule=true; resetcount_4g+=1; printf("4G Module_Reset\n"); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET); HAL_Delay(5000); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET); } void Pre_processing_4G(void)//传递消息前处理 { if(module4G_F) //4G启动位 { module4G_F=false; //鉴权操作 tt808FsFunc(ICCID,22,MessageID_authentication); } } void Transfer_time(uint8_t outTt[],char in[])//转换时间数据 { const char *hex = (const char *)in; // 将 uint8_t * 转换为 const char * int size = strlen(hex) / 2; // 计算二进制数据的长度 unsigned char *bin = (unsigned char *)malloc(size); // 分配足够的内存用于存储二进制数据 hex2bin(bin, hex); // 调用hex2bin函数进行转换 memcpy(outTt, bin, 1); // 将 bin 中的 rlt 个字节复制到 MIPurc1 中 接收到的数据放入MIPurc1中 free(bin); // 释放内存 } void RTCtime(void) { HAL_RTC_GetTime(&hrtc, &GetTime, RTC_FORMAT_BIN); /* Get the RTC current Date */ HAL_RTC_GetDate(&hrtc, &GetData, RTC_FORMAT_BIN); tt808.Rtime[0] = GetData.Year; tt808.Rtime[1] = GetData.Month; tt808.Rtime[2] = GetData.Date; tt808.Rtime[3] = GetTime.Hours; tt808.Rtime[4] = GetTime.Minutes; tt808.Rtime[5] = GetTime.Seconds; hexArrayToBcd(tt808.Rtime,6,dblocptr->location.timeBCD); //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]); //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]); time_bj();//转换时间戳 } void Time_inquiry(uint8_t flag) { if(flag==0)//首次问 { if (sendCmd_4G("AT+CCLK?\r\n", "OK", 1, 1)) { memcpy(year_str, &U4_4GrecvBuff[8], 2); // 复制两个字符 memcpy(month_str, &U4_4GrecvBuff[11], 2); memcpy(day_str, &U4_4GrecvBuff[14], 2); memcpy(hour_str, &U4_4GrecvBuff[17], 2); memcpy(minute_str, &U4_4GrecvBuff[20], 2); memcpy(second_str, &U4_4GrecvBuff[23], 2); year_str[2] = '\0'; // 添加空字符结束符 month_str[2] = '\0'; day_str[2] = '\0'; hour_str[2] = '\0'; minute_str[2] = '\0'; second_str[2] = '\0'; Transfer_time(&tt808.Rtime[0],year_str); Transfer_time(&tt808.Rtime[1],month_str); Transfer_time(&tt808.Rtime[2],day_str); Transfer_time(&tt808.Rtime[3],hour_str); Transfer_time(&tt808.Rtime[4],minute_str); Transfer_time(&tt808.Rtime[5],second_str); SyncTime(tt808.Rtime[0], tt808.Rtime[1], tt808.Rtime[2], tt808.Rtime[3], tt808.Rtime[4], tt808.Rtime[5]); } } else { /* 上面通过AT指令问 */ RTCtime();//上电同步后,作业时需再次同步 } }