From 19dc67221ff98d8529fdff19be32391f537d21a1 Mon Sep 17 00:00:00 2001
From: zhyinch <zhyinch@gmail.com>
Date: 星期一, 14 十二月 2020 17:01:36 +0800
Subject: [PATCH] V1.39 固定MODBUS 地址 9600

---
 源码/核心板/Src/application/serial_at_cmd_app.c |   24 +++++++++++++++++-------
 1 files changed, 17 insertions(+), 7 deletions(-)

diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/serial_at_cmd_app.c" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/serial_at_cmd_app.c"
index 1f347bd..bb9b63a 100644
--- "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/serial_at_cmd_app.c"
+++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/serial_at_cmd_app.c"
@@ -9,7 +9,7 @@
 #define CMD_READ	1
 #define CMD_WRITE	2
 #define CMD_REPLY 3
-#define EUART_RX_BUF_SIZE 100
+#define EUART_RX_BUF_SIZE 200
 typedef enum{	UsartReceiveWaitHead0, 
 				UsartReceiveWaitHead1,
 				UsartReceiveWaitMsgType,
@@ -18,7 +18,8 @@
 				UsartReceiveWaitIndex,
 				UsartReceiveWaitDataLen,
 				UsartReceiveWaitData,
-				UsartReceiveWaitChecksum
+				UsartReceiveWaitChecksum0,
+				UsartReceiveWaitChecksum1
 }UsartRecvPackState;
 uint8_t mUsartReceivePack[100] = {0};
 
@@ -43,7 +44,7 @@
 	
 	UART_PushFrame(send_frame, data_length+9);	
 }
-
+u16 rec_checksum;
 void UsartParseDataHandler(uint8_t data)
 {
 	static UsartRecvPackState usart_receive_state = UsartReceiveWaitHead0;
@@ -53,7 +54,7 @@
 
 
 	
-	if(usart_receive_state == UsartReceiveWaitChecksum) {			//若收到校验和包
+	if(usart_receive_state == UsartReceiveWaitChecksum1) {			//若收到校验和包
 		checksum = 0;
 		for(int i = 0; i<pack_length-5; i++) { 
 			checksum += mUsartReceivePack[i];
@@ -63,8 +64,10 @@
 		checksum += pack_index;
 		checksum += pack_datalen;
 		checksum += pack_msgtype;
+		checksum = ~checksum;
 		cmd_mode = 0;
-		if(((data + checksum)&0xff) == 0xFF)				//校验通过
+		rec_checksum = rec_checksum|(data<<8);
+		if((rec_checksum) == (checksum))				//校验通过
 		{
 			switch(pack_cmd)
 			{
@@ -73,6 +76,10 @@
 					memcpy((uint8_t*)&g_com_map + pack_index, mUsartReceivePack, pack_datalen);
 					//返回一个error状态
 					//SendComMap(pack_datalen,pack_index);
+				if((g_com_map[CNT_REBOOT]&0xff)==1)
+				{
+					returnfactory_parameter();
+				}
 					save_com_map_to_flash();
 					delay_ms(100);
 					NVIC_SystemReset();
@@ -90,11 +97,14 @@
 		pack_index = 0;
 		pack_length = 0;
 		index=0;
-	} else if((usart_receive_state == UsartReceiveWaitData) ) {	//若果收到的是正常通讯包
+	}  else if((usart_receive_state == UsartReceiveWaitChecksum0) ) {	
+			rec_checksum = data;
+		usart_receive_state = UsartReceiveWaitChecksum1;
+	}else if((usart_receive_state == UsartReceiveWaitData) ) {	//若果收到的是正常通讯包
 		mUsartReceivePack[index] = data;
 		index++;
 		if(index == pack_length-5) {		//如果收到的index与长度相等
-			usart_receive_state = UsartReceiveWaitChecksum;
+			usart_receive_state = UsartReceiveWaitChecksum0;
 		}
 	} else if(usart_receive_state == UsartReceiveWaitDataLen) {						//收到指令类型字节
 		pack_datalen = data;

--
Gitblit v1.9.3