ml307a.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "ml307a.h"
  2. #include "mba32a.h"
  3. #include "usart.h"
  4. uint8_t u4length=0;
  5. uint8_t resetcount_4g=0;
  6. bool u4RecvFlag = false;
  7. bool ml307aFail = false;
  8. uint8_t g_ML307A_8buf[2048]={0};
  9. uint8_t g_ML307A_8buf_Down[2048]={0};
  10. bool reset4Gmodule = false;
  11. extern UART_HandleTypeDef huart3;
  12. extern UART_HandleTypeDef huart4;
  13. uint8_t sendCmd_4G(char *pCmd, char *pRes, uint32_t timeOut, uint8_t sendNum);
  14. void ML307AReset(void);
  15. void ML307A_Init(void)
  16. {
  17. printf("\t###02\t4G_init%d...\r\n",resetcount_4g); \
  18. if(strstr((const char *)g_ML307A_8buf_Down, "+MATREADY") != NULL)
  19. {
  20. memset(g_ML307A_8buf_Down,0,15);
  21. printf("#4G模块上电");
  22. HAL_Delay(150);
  23. if(!sendCmd_4G("AT\r\n","OK",1,1))
  24. {
  25. ml307aFail=true;
  26. printf("4G模组异常!\r\n");
  27. }
  28. if (ml307aFail)
  29. {
  30. printf("#4G模块初始化Fail");
  31. }
  32. else
  33. {
  34. if (sendCmd_4G("AT+CPIN?\r\n", "+CME ERROR: 10", 1, 1)) // 00检测是否有SIM卡 <+CPIN: READY\r\nOK>
  35. {
  36. printf("\tERROR!-->未检测到SIM卡\r\n");
  37. OLED_ShowString(42,50,(unsigned char*)" NO_SIM ",12,0);
  38. }
  39. else
  40. {
  41. HAL_Delay(200);
  42. if (sendCmd_4G("AT+CPIN?\r\n", "+CME ERROR: 13", 1, 1))
  43. {
  44. printf("\tERROR!-->SIM失败\r\n");
  45. OLED_ShowString(35,45,(unsigned char*)" SIM_Fail ",12,0);
  46. }
  47. else
  48. {
  49. sendCmd_4G("AT+CSQ\r\n", "ERROR", 1, 1);// 01查询信号值(信号强度,误码率) <+CSQ: 26,99\r\nOK>
  50. sendCmd_4G("AT+CEREG?\r\n", "ERROR", 1, 1);// 02查询附着成功(驻网:<*,@> @为1或5表示附着成功) <+CEREG: 0,1\r\nOK>
  51. //sendCmd_4G("AT+MIPCALL?\r\n", "ERROR", 1, 1);// 03查询PDP激活状态 <+MIPCALL: 1,1,"10.94.99.162"\r\nOK>
  52. //sendCmd_4G("AT+MQTTCFG=?\r\n", "ERROR", 1, 1);// 04查询MQTT
  53. printf("init_success\r\n");
  54. }
  55. }
  56. }
  57. }
  58. else
  59. {
  60. ML307AReset();
  61. }
  62. }
  63. uint8_t sendCmd_4G(char *pCmd, char *pRes, uint32_t timeOut, uint8_t sendNum)
  64. {
  65. uint8_t i = 0;
  66. uint32_t time;
  67. for(i = 0; i < sendNum; i++)
  68. {
  69. time = timeOut * 10;
  70. USART_SendString(&huart4, pCmd);
  71. HAL_Delay(50);
  72. printf("------->%s",g_ML307A_8buf_Down);
  73. while(time--)
  74. {
  75. if(strstr((const char *)g_ML307A_8buf_Down, pRes) != NULL) // 如果检索到关键词
  76. {
  77. return 1;
  78. }
  79. HAL_Delay(100);
  80. }
  81. }
  82. return 0;
  83. }
  84. /**
  85. @brief 重启模块
  86. @param 无
  87. @return 无
  88. */
  89. void ML307AReset(void)
  90. {
  91. reset4Gmodule=true;
  92. resetcount_4g+=1;
  93. printf("4G Module_Reset\n");
  94. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
  95. HAL_Delay(5000);
  96. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
  97. }