优化通讯模块
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
{