Pārlūkot izejas kodu

1、修改4G异常相关处理

liyuezong 1 gadu atpakaļ
vecāks
revīzija
2cad5b678e
1 mainītis faili ar 54 papildinājumiem un 18 dzēšanām
  1. 54 18
      pro/src/Cellular.c

+ 54 - 18
pro/src/Cellular.c

@@ -31,7 +31,7 @@
 #endif
 
 
-#define RETRY_TIMES                 3
+#define RETRY_TIMES                 1
 #define BUF_LEN                     2000
 #define AT_PORT                     "/dev/ttyUSB1"
 #define BAUD_RATE                   B115200
@@ -485,7 +485,6 @@ static int port_init(cell_handle_t *h)
         return -1;
     }
     port_setup(h, BAUD_RATE);
-    //reset_var(h->pcell);
 
     return 0;
 }
@@ -494,7 +493,11 @@ static int port_deinit(cell_handle_t *h)
     if(h->fd<0) {
         return -1;
     }
-    return close(h->fd);
+    int r=close(h->fd);
+    
+    h->fd=-1;
+    sleep(2);
+    return r;
 }
 static int port_read(cell_handle_t *h, void *data, int len)
 {
@@ -619,6 +622,11 @@ static int send_cmd(cell_handle_t *h, int cmd, char *para, char *rcv, int rcvlen
 
                     if (pcmd->err && strlen(pcmd->err) && strstr(pbuf, pcmd->err))
                     {
+                         // 指令不通则重置模块
+                        if (cmd != AT_CMD_CFUN && cmd != AT_CMD_QIDEACT )
+                        {
+                            cell_reset(h);
+                        }
                         return AT_ERR;
                     }
                 }
@@ -627,9 +635,11 @@ static int send_cmd(cell_handle_t *h, int cmd, char *para, char *rcv, int rcvlen
             usleep(1000);
         }
     }
-    if (cmd != AT_CMD_CFUN )
+    //AT不通重启串口
+    if (cmd != AT_CMD_QIDEACT )
     {
-        cell_reset(h);
+        port_deinit(h);
+        port_init(h);
     }
 
     return AT_TOUT;
@@ -663,13 +673,14 @@ static int send_data(cell_handle_t *h, void *data, int len, int timeout)
 ////////////////////////////////////////////////////////////////////////////
 static int cell_reset(cell_handle_t *h)
 {
+     reset_var(h);
     int r;
-    reset_var(h);
-     h->stat = AT_STAT_UNINIT;
     r = send_cmd(h, AT_CMD_CFUN, "=1,1", NULL, 0);
    // if(r==AT_OK) {
       //  h->stat = AT_STAT_UNINIT;
     //}
+    sleep(5);//等待模块复位完成
+
     return (r==AT_OK)?0:-1;
 }
 static int cell_init(cell_handle_t *h)
@@ -727,10 +738,10 @@ static int cell_get_info(cell_handle_t *h, cell_info_t *info)
     }
 
     memset(info, 0, sizeof(cell_info_t));
-    r = send_cmd(h, AT_CMD_CNUM, NULL, buf, sizeof(buf));
-    if(r==AT_OK) {
-        r = sscanf(str_get(buf,'+'), "+CNUM: %*s,%s,%*d", info->cnum);
-    }
+   // r = send_cmd(h, AT_CMD_CNUM, NULL, buf, sizeof(buf));
+  //  if(r==AT_OK) {
+   //     r = sscanf(str_get(buf,'+'), "+CNUM: %*s,%s,%*d", info->cnum);
+ //   }
 
     r = send_cmd(h, AT_CMD_GSN, NULL, buf, sizeof(buf));
     if(r==AT_OK) {
@@ -770,12 +781,14 @@ static int cell_get_sig(cell_handle_t *h, cell_stat_t *stat)
     }
 
     r = send_cmd(h, AT_CMD_CSQ, NULL, buf, sizeof(buf));
-    if(r!=AT_OK) {
+    if (r != AT_OK)
+    {
         return -1;
     }
 
-    r = sscanf(str_get(buf,'+'), "%*s %d,%d", &rssi, &ber);
-    if(r<2) {
+    r = sscanf(str_get(buf, '+'), "%*s %d,%d", &rssi, &ber);
+    if (r < 2)
+    {
         return -1;
     }
     stat->dbm = rssi_to_dbm(rssi);
@@ -818,11 +831,13 @@ static int cell_get_ping(cell_handle_t *h, cell_stat_t *stat)
 
     r = send_cmd(h, AT_CMD_QPING, "=1,\"www.baidu.com\",3,2,", buf, sizeof(buf));
     if(r!=AT_OK) {
+        stat->linked = 0;
         return -1;
     }
 
     r = sscanf(str_get(buf,'+'), "+QPING: %d", &ret);
     if(r<1) {
+        stat->linked = 0;
         return -1;
     }
 
@@ -1281,7 +1296,7 @@ static int urc_handle(char *data, int len)
 static void* cellular_thread(void *arg)
 {
     int r;
-    uint32_t cnt=0;
+    uint32_t cnt=0,prelinkcnt=0;
     char rxbuf[BUF_LEN];
     thread_handle_t *th=(thread_handle_t*)arg;
     cell_handle_t *h=(cell_handle_t*)th->arg;
@@ -1291,18 +1306,39 @@ static void* cellular_thread(void *arg)
         cnt++;
         if(h->pcell->mode) {
 
+            if (h->fd<0)
+            {
+                port_init(h);
+            }
+            
             if(cnt%5==0) {
                 cell_init(h);
             }
 
-            if(cnt%5==0) {
-                //cell_get_info(h, &h->all.info);
+            if (cnt % 5 == 0)
+            {
+                // cell_get_info(h, &h->all.info);
                 cell_get_stat(h, &h->all.stat);
             }
+
+            
+            if (h->all.stat.linked == 1)
+            {
+                prelinkcnt = cnt;
+            }
+            if (cnt -prelinkcnt >=60 && h->all.stat.linked == 0)
+            {
+                prelinkcnt = cnt;
+                cell_reset(h);
+            }
         }
         else
         {
-            if(cnt%10==0) cell_deinit(h);
+            if( h->fd>=0) 
+            {
+                cell_deinit(h);
+                port_deinit(h);
+            }
             reset_var(h);
         }
         UpdateCellularManager(&h->all);