From 458f6d4d74b74367125e16c2cb593252769fa7d6 Mon Sep 17 00:00:00 2001
From: zhyinch <zhyinch@gmail.com>
Date: 星期三, 25 八月 2021 22:49:29 +0800
Subject: [PATCH] V1.53 修改继电器控制BUG

---
 源码/核心板/Src/application/serial_at_cmd_app.c |   39 +++++++++++++++++++++++++++++----------
 1 files changed, 29 insertions(+), 10 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 ba2b77e..0d75284 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"
@@ -11,7 +11,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,
@@ -20,7 +20,8 @@
 				UsartReceiveWaitIndex,
 				UsartReceiveWaitDataLen,
 				UsartReceiveWaitData,
-				UsartReceiveWaitChecksum
+				UsartReceiveWaitChecksum0,
+				UsartReceiveWaitChecksum1
 }UsartRecvPackState;
 
 uint8_t mUsartReceivePack[100] = {0};
@@ -38,7 +39,7 @@
 	checksum = Checksum_u16(&send_frame[2],8);
 	memcpy(&send_frame[10],&checksum,2);
 	
-	USART_puts(send_frame, 12);	
+	UART_PushFrame(send_frame, 12);	
 }
 void SendComMap(uint8_t data_length, uint8_t index)
 {
@@ -62,6 +63,7 @@
 }
 u8 remotetag_para[30];
 u8 remotesend_state=0;
+u16 rec_checksum;
 void UsartParseDataHandler(uint8_t data)
 {
 	static UsartRecvPackState usart_receive_state = UsartReceiveWaitHead0;
@@ -71,7 +73,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];
@@ -81,7 +83,9 @@
 		checksum += pack_index;
 		checksum += pack_datalen;
 		checksum += pack_msgtype;
-		if(((data + checksum)&0xff) == 0xFF)				//校验通过
+		checksum = ~checksum;
+		rec_checksum = rec_checksum|(data<<8);
+		if(rec_checksum == checksum)				//校验通过
 		{
 			switch(pack_msgtype)
 			{
@@ -90,13 +94,25 @@
 					{
 						case CMD_WRITE:
 							//从mUsartReceivePack中读取pack_length长度的字节,放到全局变量中
-							memcpy((uint8_t*)&g_com_map + pack_index, mUsartReceivePack, pack_datalen);
+                        if(pack_index<512)
+                           //  if(pack_index>100)
+                        {
+                            memcpy((uint8_t*)&g_com_map + pack_index, mUsartReceivePack, pack_datalen);
 							//返回一个error状态
 							//SendComMap(pack_datalen,pack_index);
 							save_com_map_to_flash();
 							SendConfigConfirm(pack_msgtype,pack_length);
-							delay_ms(10);
-							SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
+							delay_ms(500);
+							SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader  
+                        
+                        }else{
+							memcpy((uint8_t*)&g_com_map + pack_index, mUsartReceivePack, pack_datalen);
+							//返回一个error状态
+							//SendComMap(pack_datalen,pack_index);
+							//save_com_map_to_flash();
+							SendConfigConfirm(pack_msgtype,pack_length);
+							
+                        }                            
 							break;
 						case CMD_READ:
 							//read包中data字节,即mUsartReceivePack[0]表示数据长度;
@@ -120,11 +136,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