From b16ff1379d8ddbce73ccb46dec527b898effa340 Mon Sep 17 00:00:00 2001
From: zhyinch <zhyinch@gmail.com>
Date: 星期五, 06 十一月 2020 15:30:54 +0800
Subject: [PATCH] V2.11 修改基站间隔时间,修改时间片 频率输出医院测试版本

---
 源码/核心板/Src/application/serial_at_cmd_app.c |  335 ++++++++++++++++++++++++++-----------------------------
 1 files changed, 157 insertions(+), 178 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 4e82425..0ec6dc1 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"
@@ -4,188 +4,167 @@
 #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;
-uint8_t g_pairstart=0;
-void ParseFrame(void)
-{
-	if(!memcmp(m_frame_data, "DIS1", 4))
-	{
-		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();
-	}
-	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))
-	{
-	
-		for(uint8_t i=3; i < m_frame_data_len; i++)
-		{
-			if(m_frame_data[i]<='9' && m_frame_data[i]>='0')
-			{
-				dev_id = (dev_id<<4)|(m_frame_data[i]- '0');
-			}else if(m_frame_data[i]<='f' && m_frame_data[i]>='a')
-			{
-				dev_id = (dev_id<<4)|(m_frame_data[i]- 'a'+10);
-			}else if(m_frame_data[i]<='F' && m_frame_data[i]>='A')
-			{
-				dev_id = (dev_id<<4)|(m_frame_data[i]- 'A'+10);
-			}
-			else
-			{
-				printf("Error: Wrong ID.\r\n");
-				break;
-			}
-		}
-		g_com_map[DEV_ID_L] = dev_id;
-		g_com_map[DEV_ID_H] = dev_id>>16;
-		Dw1000_App_Init();
-		printf("set dev id = %x. \r\n", dev_id);
-		save_com_map_to_flash();
-	}else if(!memcmp(m_frame_data, "INT", 3))
-	{
-		g_com_map[COM_INTERVAL] = 0;
-		for(uint8_t i=4; i < m_frame_data_len; i++)
-		{
-			if(m_frame_data[i]<58 && m_frame_data[i]>47)
-			{
-				g_com_map[COM_INTERVAL] = g_com_map[COM_INTERVAL]*10 + m_frame_data[i]- '0';
-			}
-			else
-			{
-				printf("Error: Wrong COM_INTERVAL.\r\n");
-				break;
-			}
-		}
-		printf("set dev interval = %d ms. \r\n", g_com_map[COM_INTERVAL]);
-		RTC_Configuration(g_com_map[COM_INTERVAL]*20);
-		save_com_map_to_flash();
-	}else if(!memcmp(m_frame_data, "PAIRID", 6))
-	{
-		g_com_map[PAIR_ID] = 0;
-		for(uint8_t i=7; i < m_frame_data_len; i++)
-		{
-			if(m_frame_data[i]<58 && m_frame_data[i]>47)
-			{
-				g_com_map[PAIR_ID] = g_com_map[PAIR_ID]*10 + m_frame_data[i]- '0';
-			}
-			else
-			{
-				printf("Error: Wrong PAIR_ID.\r\n");
-				break;
-			}
-		}
-		printf("set dev PAIR_ID = %d . \r\n", g_com_map[PAIR_ID]);
-		save_com_map_to_flash();
-	}else if(!memcmp(m_frame_data, "STARTPAIR", 9))
-	{
-		g_pairstart=1;
-		printf("Start Pair Mode. \r\n");
-	}else{
-		printf("Error: Wrong Command.\r\n");
-	}
-}
 
+#define MSG_RW 		3
+#define MSG_WTAG  6
+#define MSG_PWTAG 8
+#define MSG_REGTAGRESPONSE 0x0b
+
+#define CMD_READ	1
+#define CMD_WRITE	2
+#define CMD_REPLY 3
+
+struct pwtag_structure pwtag;
+typedef enum{	UsartReceiveWaitHead0, 
+				UsartReceiveWaitHead1,
+				UsartReceiveWaitMsgType,
+				UsartReceiveWaitLength, 
+				UsartReceiveWaitCMD,
+				UsartReceiveWaitIndex,
+				UsartReceiveWaitDataLen,
+				UsartReceiveWaitData,
+				UsartReceiveWaitChecksum
+}UsartRecvPackState;
+uint8_t mUsartReceivePack[100] = {0};
+uint8_t send_frame[200];
+void SendConfigConfirm(uint8_t pack_msgtype,uint8_t pack_length)
+{
+	uint16_t checksum = 0;
+	send_frame[0] = 0x55;
+	send_frame[1] = 0xAA;
+	send_frame[2] = 0x09;
+	send_frame[3] = 8;
+	send_frame[4] = pack_msgtype;
+	send_frame[5] = pack_length;
+
+	checksum = Checksum_u16(&send_frame[2],8);
+	memcpy(&send_frame[10],&checksum,2);
+	
+	UART_PushFrame(send_frame, 12);	
+}
+void SendComMap(uint8_t data_length, uint8_t index)
+{
+	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);
+
+	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],remotetag_paralen;
+u8 remotesend_state=0;
+uint16_t configremotetagID;
+struct regtag_structure regtag_map;
 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;
+	uint8_t i;
+
 	
-	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-2; i++) { 
+			checksum += mUsartReceivePack[i];
+		}
+		checksum += pack_msgtype;
+		checksum += pack_length;
+
+		if(((data + checksum)&0xff) == 0xFF)				//校验通过
+		{
+			switch(pack_msgtype)
+			{
+				case MSG_RW:
+						 pack_cmd = mUsartReceivePack[0];
+						 pack_index = mUsartReceivePack[1];
+						 pack_datalen = mUsartReceivePack[2];
+					switch(pack_cmd)
+					{
+						case CMD_WRITE:
+							//从mUsartReceivePack中读取pack_length长度的字节,放到全局变量中
+							memcpy((uint8_t*)&g_com_map + pack_index, &mUsartReceivePack[3], 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=1;
+					memcpy(&configremotetagID,&mUsartReceivePack[0],2);
+					//configremotetagID = pack_cmd|pack_index<<8;
+					//remotetag_para[0] = mUsartReceivePack[2];
+					remotetag_paralen = mUsartReceivePack[4]+3;
+					memcpy(&remotetag_para[1],&mUsartReceivePack[3],remotetag_paralen);
+					break;
+				case MSG_PWTAG:		//批量修改标签参数	
+						pwtag.groupnum = mUsartReceivePack[0];
+						pwtag.index =  mUsartReceivePack[1];
+						pwtag.remain_time = 5;
+						for(i=0;i<pwtag.groupnum;i++)
+						{
+							memcpy(pwtag.groupid[i],&mUsartReceivePack[i*6+2],4);
+							memcpy(&pwtag.group_interval[i],&mUsartReceivePack[i*6+6],2);
+						}
+							break;
+				case MSG_REGTAGRESPONSE:
+					regtag_map.index++;
+				if(regtag_map.index==MAX_REGTAGNUM)
+				{regtag_map.index = 0;}
+					memcpy(&regtag_map.tagid[regtag_map.index],&mUsartReceivePack[0],2);
+					memcpy(&regtag_map.tag_frequency[regtag_map.index],&mUsartReceivePack[2],2);
+					memcpy(&regtag_map.tag_slotpos[regtag_map.index],&mUsartReceivePack[4],2);
+					break;
+		}
+			SendConfigConfirm(pack_msgtype,pack_length);
+		}
+		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-2) {		//如果收到的index与长度相等
+			usart_receive_state = UsartReceiveWaitChecksum;
+		}
+	} 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 = UsartReceiveWaitData;
+		
+	} 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