From 147d2248cd39e7686e73313ef5408fda066fdb0a Mon Sep 17 00:00:00 2001
From: zhyinch <zhyinch@gmail.com>
Date: 星期二, 27 八月 2019 21:09:00 +0800
Subject: [PATCH] 增加标签确定序号

---
 源码/核心板/Src/application/serial_at_cmd_app.c |  197 +++++++++++++++++++++++++++---------------------
 1 files changed, 110 insertions(+), 87 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 da57aa0..ae83024 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"
@@ -2,103 +2,126 @@
 #include "global_param.h"
 #include <string.h>
 #include <stdio.h>
+#include "dw_app.h"
+#include "RTC.h"
 
-uint8_t m_frame_data[MAX_FRAME_LEN] = {0};
-uint8_t m_frame_data_len = 0;
 
-void ParseFrame(void)
+#define CMD_READ	1
+#define CMD_WRITE	2
+#define CMD_REPLY 3
+#define EUART_RX_BUF_SIZE 100
+typedef enum{	UsartReceiveWaitHead0, 
+				UsartReceiveWaitHead1,
+				UsartReceiveWaitMsgType,
+				UsartReceiveWaitLength, 
+				UsartReceiveWaitCMD,
+				UsartReceiveWaitIndex,
+				UsartReceiveWaitDataLen,
+				UsartReceiveWaitData,
+				UsartReceiveWaitChecksum
+}UsartRecvPackState;
+uint8_t mUsartReceivePack[100] = {0};
+
+void SendComMap(uint8_t data_length, uint8_t index)
 {
-	if(!memcmp(m_frame_data, "DIS", 3))
+	static uint8_t send_frame[EUART_RX_BUF_SIZE];
+	uint16_t checksum = 0;
+	send_frame[0] = 0x55;
+	send_frame[1] = 0xAA;
+	send_frame[2] = 0x03;
+	send_frame[3] = data_length+5;
+	send_frame[4] = CMD_REPLY;
+	send_frame[5] = index;
+	send_frame[6] = data_length;
+	memcpy(&send_frame[7], &g_com_map[index], data_length);
+	for(int i = 0; i<(data_length+5); i++)
 	{
-		g_com_map[ALARM_DISTANCE] =0;
-		for(uint8_t i=3; i < m_frame_data_len; i++)
-		{
-			if(m_frame_data[i]<58 && m_frame_data[i]>47)
-			{
-				g_com_map[ALARM_DISTANCE] = g_com_map[ALARM_DISTANCE]*10 + m_frame_data[i]- '0';
-			}
-			else
-			{
-				printf("Error: Wrong DISTANCE.\r\n");
-				break;
-			}
-		}
-		printf("When distance < %d cm, alarm is on.\n", g_com_map[ALARM_DISTANCE]);
-		save_com_map_to_flash();
+		checksum += send_frame[2+i];
 	}
-	else if(!memcmp(m_frame_data, "DEV", 3))
-	{
-		g_com_map[ALARM_DEV] = m_frame_data[3]-'0';
-		printf("Alarm device is %d. \n", g_com_map[ALARM_DEV]);
-		save_com_map_to_flash();
-	}
-	else if(!memcmp(m_frame_data, "ID", 2))
-	{
-		g_com_map[DEV_ID] = 0;
-		for(uint8_t i=2; i < m_frame_data_len; i++)
-		{
-			if(m_frame_data[i]<58 && m_frame_data[i]>47)
-			{
-				g_com_map[DEV_ID] = g_com_map[DEV_ID]*10 + m_frame_data[i]- '0';
-			}
-			else
-			{
-				printf("Error: Wrong ID.\r\n");
-				break;
-			}
-		}
-		printf("set dev id = %d. \r\n", g_com_map[DEV_ID]);
-		save_com_map_to_flash();
-	}
+	checksum = Checksum_u16(&send_frame[2],5+data_length);
+	memcpy(&send_frame[7+data_length],&checksum,2);
+	
+	UART_PushFrame(send_frame, data_length+9);	
 }
 
 void UsartParseDataHandler(uint8_t data)
 {
-	static uint8_t s_usart_state = 0;
-	static uint8_t s_data_pos;
+	static UsartRecvPackState usart_receive_state = UsartReceiveWaitHead0;
+	uint16_t checksum = 0;
+	static uint8_t pack_datalen = 0,pack_length = 0,pack_index = 0,pack_msgtype = 0,pack_cmd = CMD_READ;
+	static uint8_t index = 0;
+
+
 	
-	switch(s_usart_state)
-	{
-		case 0:
-			if(data=='A')	s_usart_state=  1;
-		break;
-			
-		case 1:
-			if(data=='T')	s_usart_state = 2;
-		break;
+	if(usart_receive_state == UsartReceiveWaitChecksum) {			//若收到校验和包
+		checksum = 0;
+		for(int i = 0; i<pack_length-5; i++) { 
+			checksum += mUsartReceivePack[i];
+		}
+		checksum += pack_cmd;
+		checksum += pack_length;
+		checksum += pack_index;
+		checksum += pack_datalen;
+		checksum += pack_msgtype;
+		if((data + checksum)&0xff == 0xFF)				//校验通过
+		{
+			switch(pack_cmd)
+			{
+				case CMD_WRITE:
+					//从mUsartReceivePack中读取pack_length长度的字节,放到全局变量中
+					memcpy((uint8_t*)&g_com_map + pack_index, mUsartReceivePack, pack_datalen);
+					//返回一个error状态
+					//SendComMap(pack_datalen,pack_index);
+					save_com_map_to_flash();
+					delay_ms(100);
+					NVIC_SystemReset();
+					break;
+				case CMD_READ:
+					//read包中data字节,即mUsartReceivePack[0]表示数据长度;
+				//从g_com_data结构体中的第index位置读取长度为mUsartReceivePack[0]的字节,发送出来
+					SendComMap(pack_datalen,pack_index>>1);		
+					break;
+				default:
+					break;
+			}
+		}
+		usart_receive_state = UsartReceiveWaitHead0;
+		pack_index = 0;
+		pack_length = 0;
+		index=0;
+	} else if((usart_receive_state == UsartReceiveWaitData) ) {	//若果收到的是正常通讯包
+		mUsartReceivePack[index] = data;
+		index++;
+		if(index == pack_length-5) {		//如果收到的index与长度相等
+			usart_receive_state = UsartReceiveWaitChecksum;
+		}
+	} else if(usart_receive_state == UsartReceiveWaitDataLen) {						//收到指令类型字节
+		pack_datalen = data;
+		usart_receive_state = UsartReceiveWaitData;
+	}else if(usart_receive_state == UsartReceiveWaitIndex) {						//收到指令类型字节
+		pack_index = data;
+		usart_receive_state = UsartReceiveWaitDataLen;
+	} else if(usart_receive_state == UsartReceiveWaitCMD) {							//收到指令类型字节
+		pack_cmd = data;
+		usart_receive_state = UsartReceiveWaitIndex;
+	} else if(usart_receive_state == UsartReceiveWaitLength) {						//收到长度字节
 		
-		case 2:
-			if(data=='+')
-			{ 
-				s_usart_state = 3;
-				s_data_pos = 0;
-				m_frame_data_len = 0;
-			}
-		break;
-			
-		case 3:
-			m_frame_data_len = s_data_pos;
-			m_frame_data[s_data_pos++] = data;
-			if(data == 0x0d)
-			{
-				s_usart_state = 4;
-			}
-			if(s_data_pos >= MAX_FRAME_LEN) //如果超过最大长度
-			{
-				m_frame_data_len = MAX_FRAME_LEN;
-				ParseFrame();
-				s_usart_state = 0;
-			}
-		break;
-			
-		case 4:
-			if(data == 0x0a)
-			{
-				ParseFrame();
-				s_usart_state = 0;
-			}
-		break;
-			
-		default: break;
+			pack_length = data;
+			pack_index = 0;			
+			usart_receive_state = UsartReceiveWaitCMD;
+		
+	} else if((usart_receive_state == UsartReceiveWaitHead0) && (data == 0x55)) {	//收到第一个包头
+		usart_receive_state = UsartReceiveWaitHead1;
+	} else if((usart_receive_state == UsartReceiveWaitHead1) && (data == 0xAA)) {	//收到第二个包头
+		usart_receive_state = UsartReceiveWaitMsgType;
+	}else if ((usart_receive_state == UsartReceiveWaitMsgType) && (data == 0x3)) {
+		usart_receive_state = UsartReceiveWaitLength;
+		pack_msgtype = data;
 	}
+	else {
+		usart_receive_state = UsartReceiveWaitHead0;
+		pack_index = 0;
+		pack_length = 0;
+	}
+
 }

--
Gitblit v1.9.3