From 2e895d0a7b81c6216457ddd23690c1249ceb993d Mon Sep 17 00:00:00 2001
From: zhyinch <zhyinch@gmail.com>
Date: 星期五, 24 七月 2020 10:09:38 +0800
Subject: [PATCH] V1.22 去掉标签串口输出

---
 源码/核心板/Src/application/serial_at_cmd_app.c |  249 ++++++++++++++++++++++++-------------------------
 1 files changed, 124 insertions(+), 125 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 4adc99f..315fb89 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"
@@ -3,140 +3,139 @@
 #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;
+#define MSG_RW 		3
+#define MSG_WTAG  6
 
-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, "DIS1", 4))
+	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>>1], data_length);
+	for(int i = 0; i<(data_length+5); i++)
 	{
-		g_com_map[ALARM_DISTANCE1] =0;
-		for(uint8_t i=5; i < m_frame_data_len; i++)
-		{
-			if(m_frame_data[i]<58 && m_frame_data[i]>47)
-			{
-				g_com_map[ALARM_DISTANCE1] = g_com_map[ALARM_DISTANCE1]*10 + m_frame_data[i]- '0';
-			}
-			else
-			{
-				printf("Error: Wrong DISTANCE.\r\n");
-				break;
-			}
-		}
-		printf("When distance1 < %d cm, alarm1 is on.\n", g_com_map[ALARM_DISTANCE1]);
-		save_com_map_to_flash();
+		checksum += send_frame[2+i];
 	}
-	else if(!memcmp(m_frame_data, "DIS2", 4))
-	{
-		g_com_map[ALARM_DISTANCE2] =0;
-		for(uint8_t i=5; i < m_frame_data_len; i++)
-		{
-			if(m_frame_data[i]<58 && m_frame_data[i]>47)
-			{
-				g_com_map[ALARM_DISTANCE2] = g_com_map[ALARM_DISTANCE2]*10 + m_frame_data[i]- '0';
-			}
-			else
-			{
-				printf("Error: Wrong DISTANCE.\r\n");
-				break;
-			}
-		}
-		printf("When distance2 < %d cm, alarm2 is on.\n", g_com_map[ALARM_DISTANCE2]);
-		save_com_map_to_flash();
-	}
-	else if(!memcmp(m_frame_data, "DIS3", 4))
-	{
-		g_com_map[ALARM_DISTANCE3] =0;
-		for(uint8_t i=5; i < m_frame_data_len; i++)
-		{
-			if(m_frame_data[i]<58 && m_frame_data[i]>47)
-			{
-				g_com_map[ALARM_DISTANCE3] = g_com_map[ALARM_DISTANCE3]*10 + m_frame_data[i]- '0';
-			}
-			else
-			{
-				printf("Error: Wrong DISTANCE.\r\n");
-				break;
-			}
-		}
-		printf("When distance3 < %d cm, alarm3 is on.\n", g_com_map[ALARM_DISTANCE3]);
-		save_com_map_to_flash();
-	}
-	else if(!memcmp(m_frame_data, "DEV", 3))
-	{
-		g_com_map[ALARM_DEV] = m_frame_data[4]-'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=3; 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;
-			}
-		}
-		Dw1000_App_Init();
-		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);	
 }
-
+u8 remotetag_para[30];
+u8 remotesend_state=0;
 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_msgtype)
+			{
+				case MSG_RW:
+					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);		
+							break;
+						default:
+							break;
+					}
+					break;
+				case MSG_WTAG:
+					remotesend_state++;
+					if(remotesend_state>14)
+						remotesend_state=1;
+					memcpy(remotetag_para,mUsartReceivePack,REMOTEPARA_LEN);
+					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)) {
+		usart_receive_state = UsartReceiveWaitLength;
+		pack_msgtype = data;
 	}
+	else {
+		usart_receive_state = UsartReceiveWaitHead0;
+		pack_index = 0;
+		pack_length = 0;
+	}
+
 }

--
Gitblit v1.9.3