优化通讯模块
diff --git a/supwisdom/sp_communicate.c b/supwisdom/sp_communicate.c
index c86690a..7b96183 100644
--- a/supwisdom/sp_communicate.c
+++ b/supwisdom/sp_communicate.c
@@ -8,41 +8,69 @@
 #include "sp_upgrade.h"
 #include "../sys_hw/drv_usart.h"
 
+static uint8 rxBuf[512] = {0};
+static uint16 rxBufLen = 0;
+
 static void sp_usart_send(sp_pos_t* pos, sp_protocol_request_t* req)
 {
-  uint8 sendBuff[264];
-  MEMCLEAR(sendBuff, sizeof(sendBuff));
+  uint8 buf[264];
+  MEMCLEAR(buf, sizeof(buf));
+  sp_protocol_crc((uint8*)req +2, req->datalen, (uint8*)req +2 +req->datalen);
+  req->datalen += 2;
+  MEMCPY(buf, req, req->datalen+2);
+  usart_send(buf, req->datalen+2);
 
   pos->last_comm_status.command = req->excmd;
   pos->last_comm_status.sendtime = sp_get_ticker();
-
-  sp_protocol_crc((uint8*)req +2, req->datalen, (uint8*)req +2 +req->datalen);
-  req->datalen += 2;
-  MEMCPY(sendBuff, req, req->datalen+2);
-  usart_send(sendBuff, req->datalen+2);
+  MEMCLEAR(rxBuf,sizeof rxBuf);
+  rxBufLen = 0;
 }
 
-static uint8 sp_usart_recv(sp_pos_t* pos, sp_protocol_response_t* resp, int32 timeout_ms)
+static uint8 sp_usart_recv(sp_pos_t* pos, sp_protocol_response_t* resp,
+                               uint32 timeout_ms)
 {
-  uint32 tick = 0;
+  uint32 tick;
+  uint16 len;
+  int16 datalen;
+  uint8 crc[2];
+  uint8 temp[sizeof(sp_protocol_response_t)];
   tick = sp_get_ticker();
+  MEMCLEAR(temp,sizeof temp);
   while(1)
   {
-    MEMCLEAR(resp, sizeof(sp_protocol_response_t));
-    usart_read((u8*)resp, sizeof(sp_protocol_response_t));
-    if(pos->last_comm_status.command == resp->excmd)
+
+    len = usart_read((u8*)temp,sizeof temp);
+    if(len > 0)
     {
-      MEMCLEAR(&(pos->last_comm_status), sizeof(sp_comm_status_t));
-      return resp->retcode;
+      MEMCPY(rxBuf +rxBufLen, temp,len);
+      rxBufLen += len;
+      datalen = get_2byte_int_le(rxBuf);
+      if((datalen+2) <= rxBufLen)
+      {
+        MEMCLEAR(resp, sizeof(sp_protocol_response_t));
+        MEMCPY((uint8*)resp,rxBuf,rxBufLen);
+        MEMCLEAR(rxBuf,sizeof rxBuf);
+        rxBufLen = 0;
+        if(pos->last_comm_status.command == resp->excmd)
+        {
+          sp_protocol_crc((uint8*)resp +2,datalen -2, crc);
+          if(MEMCMP((uint8*)resp +datalen,crc,2) == 0)
+          {
+            MEMCLEAR(&(pos->last_comm_status), sizeof(sp_comm_status_t));
+            return resp->retcode;
+          }
+        }
+      }
     }
     if((sp_get_ticker() - tick) >= timeout_ms)
     {
-      return 1;
+      return RC_COMM_TIMEOUT;
     }
   }
 }
+
 uint8 sp_comm_call(sp_pos_t* pos, sp_protocol_request_t* req,
-                   sp_protocol_response_t* resp, int32 timeout_ms)
+                   sp_protocol_response_t* resp, uint32 timeout_ms)
 {
   sp_usart_send(pos, req);
   return sp_usart_recv(pos, resp, timeout_ms);
@@ -163,6 +191,30 @@
   return 0;
 }
 
+void sp_login(sp_pos_t* pos)
+{
+  uint16 ret;
+  char msg[20];
+  sp_protocol_response_t resp;
+  disp_hint_info_two(pos,"ÕýÔڵǼ","ÇëÉÔµÈ...",0);
+  ret = sp_async_equipment_login(pos);
+  if(ret)
+  {
+    sprintf(msg,"´íÎóÂë=%d",ret);
+    disp_hint_info_two(pos,"µÇ¼ʧ°Ü",msg,DELAY_TIME3s);
+    return;
+  }
+  MEMCLEAR(&resp,sizeof resp);
+  sp_usart_recv(pos,&resp,COMM_WAIT_TIME);
+  ret = sp_confirm_login(&resp,pos);
+  if(ret)
+  {
+    disp_server_errmsg(pos,"µÇ¼ʧ°Ü",resp.data,resp.datalen);
+    return;
+  }
+  disp_hint_info(pos,"µÇ¼³É¹¦",DELAY_TIME3s);
+}
+
 //ÐÄÌøÈ·ÈÏ£¬¼ì²âÍøÂçÊÇ·ñÕý³£
 uint16 sp_async_heartbeat(sp_pos_t* pos)
 {
@@ -524,10 +576,9 @@
 {
   uint8 ret = 0;
   uint8 i = 0;
-  int32 timeout_ms = COMM_WAIT_TIME;
   sp_protocol_response_t resp;
 
-  ret = sp_usart_recv(pos, &resp, timeout_ms);
+  ret = sp_usart_recv(pos, &resp, 100);
   if(ret)
   {
     return ret;
@@ -562,8 +613,8 @@
 
 void sp_communicate(sp_pos_t* pos)
 {
-  uint16 ret = 0;
-  uint32 ticker = 0;
+  uint16 ret;
+  uint32 ticker;
   sp_transdtl_t transdtl;
 
   if(pos->deviceno == 0)
@@ -576,14 +627,14 @@
     if(pos->devlogin.last_login_ticker == 0 || pos->devlogin.login_flag == 0
         || ticker - pos->devlogin.last_login_ticker > DELAY_TIME60s*30)
     {
-        pos->devlogin.last_login_ticker = ticker;
-        sp_async_equipment_login(pos);
+      pos->devlogin.last_login_ticker = ticker;
+      sp_async_equipment_login(pos);
     }
     if(pos->heartbeat.last_heartbeat_ticker == 0
         || ticker - pos->heartbeat.last_heartbeat_ticker > DELAY_TIME60s)
     {
-        pos->heartbeat.last_heartbeat_ticker = ticker;
-        sp_async_heartbeat(pos);
+      pos->heartbeat.last_heartbeat_ticker = ticker;
+      sp_async_heartbeat(pos);
     }
     else
     {
diff --git a/supwisdom/sp_communicate.h b/supwisdom/sp_communicate.h
index 3258eff..8a6f59a 100644
--- a/supwisdom/sp_communicate.h
+++ b/supwisdom/sp_communicate.h
@@ -132,10 +132,11 @@
 uint16 sp_async_upload_transdtl(sp_pos_t* pos, sp_transdtl_t* dtl);
 uint16 sp_qrcode_init(sp_pos_t* pos, sp_card_t* card);
 uint16 sp_qrcode_query(sp_pos_t* pos, sp_card_t* card);
+void sp_login(sp_pos_t* pos);
 
 //ͨѶ
 uint8 sp_comm_call(sp_pos_t* pos, sp_protocol_request_t* req,
-                   sp_protocol_response_t* resp, int32 timeout_ms);
+                   sp_protocol_response_t* resp, uint32 timeout_ms);
 void sp_protocol_req_init(sp_protocol_request_t* req, uint8 command);
 #endif
 
diff --git a/supwisdom/sp_constant.h b/supwisdom/sp_constant.h
index a7dd95b..ea79d09 100644
--- a/supwisdom/sp_constant.h
+++ b/supwisdom/sp_constant.h
@@ -51,6 +51,7 @@
 #define RC_DEV_NOT_LOGIN				65	//É豸δǩµ½
 #define RC_DEV_FAULT					66  //É豸ÒÉËÆ¹ÊÕÏ
 #define RC_DEV_NOSET_FLOWSENSOR_UNIT	67  //É豸Á÷Á¿¼ÆË㵥λδÉèÖÃ
+#define RC_COMM_TIMEOUT					68  //ͨѶ³¬Ê±
 #define STATUS_KEEPOPEN					1	//³£¿ª¿¨
 #define STATUS_CLOSED					0	//È¡Ïû³£¿ª¿¨
 
diff --git a/supwisdom/sp_menu.c b/supwisdom/sp_menu.c
index 572f788..d9d1b0a 100644
--- a/supwisdom/sp_menu.c
+++ b/supwisdom/sp_menu.c
@@ -80,6 +80,7 @@
   ticker = sp_get_ticker();
   while(sp_get_ticker()-ticker <= DELAY_TIME60s)
   {
+    sp_feed_dog();
     keycode = sp_get_key();
     if(keycode != SP_KEY_NONE)
     {
@@ -296,6 +297,7 @@
   ticker = sp_get_ticker();
   while(sp_get_ticker() -ticker < DELAY_TIME60s)
   {
+    sp_feed_dog();
     kcode = sp_get_key();
     if(kcode != SP_KEY_NONE)
     {
@@ -354,41 +356,6 @@
   }
 }
 
-static void sp_manual_login(sp_pos_t* pos)
-{
-  uint16 ret;
-  uint32 tick;
-  sp_protocol_response_t resp;
-  MEMCLEAR(&resp, sizeof(resp));
-  disp_hint_info_two(pos,"ÕýÔڵǼ","ÇëÉÔµÈ...",0);
-  sp_async_equipment_login(pos);
-  tick = sp_get_ticker();
-  while(sp_get_ticker() - tick < DELAY_TIME3s*2)
-  {
-    MEMCLEAR(&resp, sizeof(resp));
-    usart_read((u8*)&resp, sizeof(resp));
-    if(resp.excmd == SP_CMD_LOGIN)
-    {
-      ret = sp_confirm_login(&resp, pos);
-      break;
-    }
-    else
-    {
-      ret = RC_DEV_LOGIN_FAIL;
-    }
-  }
-  if(ret)
-  {
-    char msg[32];
-    sprintf(msg,"´íÎóÂë=%d",ret);
-    disp_hint_info_two(pos,"Ç©µ½Ê§°Ü",msg,DELAY_TIME3s);
-  }
-  else
-  {
-    disp_hint_info(pos,"Ç©µ½³É¹¦",DELAY_TIME3s);
-  }
-}
-
 static void sp_set_devphyid(sp_pos_t* pos)
 {
   uint32 ticker;
@@ -407,6 +374,7 @@
   sp_hex_to_str(devphyid,4,str);
   while(sp_get_ticker() -ticker < DELAY_TIME60s)
   {
+    sp_feed_dog();
     kcode = sp_get_key();
     if(kcode != SP_KEY_NONE)
     {
@@ -485,6 +453,7 @@
   ticker = sp_get_ticker();
   while(sp_get_ticker() -ticker < DELAY_TIME60s)
   {
+    sp_feed_dog();
     kcode = sp_get_key();
     if(kcode != SP_KEY_NONE)
     {
@@ -605,6 +574,7 @@
   ticker = sp_get_ticker();
   while(sp_get_ticker() -ticker < DELAY_TIME60s)
   {
+    sp_feed_dog();
     sp_valve_control();
     kcode = sp_get_key();
     if(kcode != SP_KEY_NONE)
@@ -631,12 +601,11 @@
 
 static void test_factory_options(sp_pos_t* pos)
 {
-  uint32 ticker = 0;
+  uint32 ticker;
   uint8 keycode;
   uint8 page;
   uint8 max_cnt;
-  uint8 key_press = 1;
-
+  uint8 key_press;
   menu_t menus[] =
   {
     {"1.°´¼üУ׼", do_keyboard_calibrate},
@@ -648,8 +617,9 @@
   };
   max_cnt = (sizeof(menus)/sizeof(menu_t) -1)/3 + 1;
   page = 0;
+  key_press = 1;
   ticker = sp_get_ticker();
-  while(sp_get_ticker()-ticker <= DELAY_TIME60s*5)
+  while(sp_get_ticker()-ticker <= DELAY_TIME60s)
   {
     sp_feed_dog();
     keycode = sp_get_key();
@@ -676,6 +646,7 @@
         if(menus[keycode-SP_KEY_1].func != NULL)
         {
           menus[keycode-SP_KEY_1].func(pos);
+          key_press = 1;
         }
         break;
       case SP_KEY_CLEAR:
@@ -700,28 +671,29 @@
 
 void sp_menu_options(sp_pos_t* pos)
 {
-  uint32 ticker = 0;
+  uint32 ticker;
   uint8 keycode;
   uint8 page;
   uint8 max_cnt;
-  uint8 key_press = 1;
+  uint8 key_press;
 
   menu_t menus[] =
   {
     {"1.²é¿´²ÎÊý", sp_show_syspara},
     {"2.¹¤³§²âÊÔ", test_factory_options},
     {"3.ÉèÖÃʱ¼ä", sp_set_devtime},
-    {"4.ÊÖ¶¯µÇ¼", sp_manual_login},
+    {"4.ÊÖ¶¯µÇ¼", sp_login},
     {"5.ÉèÖÃÎïÀíid", sp_set_devphyid},
     {"6.ÉèÖûúºÅ", sp_set_deviceno},
     {"7.Çå¿ÕÁ÷Ë®", clear_transdtl},
     {"8.ÔÚÏßÉý¼¶", manual_upgrade},
     {"9.»Ö¸´³ö³§", reset_factory}
   };
+  key_press = 1;
   max_cnt = (sizeof(menus)/sizeof(menu_t) -1)/3 + 1;
   page = 0;
   ticker = sp_get_ticker();
-  while(sp_get_ticker()-ticker <= DELAY_TIME60s*5)
+  while(sp_get_ticker()-ticker <= DELAY_TIME60s)
   {
     sp_feed_dog();
     keycode = sp_get_key();
@@ -748,6 +720,7 @@
         if(menus[keycode-SP_KEY_1].func != NULL)
         {
           menus[keycode-SP_KEY_1].func(pos);
+          key_press = 1;
         }
         break;
       case SP_KEY_CLEAR: