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 | 289 ++++++++++++++++++++++++++++++++-------------------------
1 files changed, 162 insertions(+), 127 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..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"
@@ -3,140 +3,175 @@
#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 200
+typedef enum{ UsartReceiveWaitHead0,
+ UsartReceiveWaitHead1,
+ UsartReceiveWaitMsgType,
+ UsartReceiveWaitLength,
+ UsartReceiveWaitCMD,
+ UsartReceiveWaitIndex,
+ UsartReceiveWaitDataLen,
+ UsartReceiveWaitData,
+ UsartReceiveWaitChecksum0,
+ UsartReceiveWaitChecksum1
+}UsartRecvPackState;
+
+uint8_t mUsartReceivePack[100] = {0};
+uint8_t send_frame[200];
+void SendConfigConfirm(uint8_t pack_msgtype,uint8_t pack_length)
{
- 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))
- {
- 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();
- }
-}
+ 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);
+ for(int i = 0; i<(data_length+5); i++)
+ {
+ checksum += send_frame[2+i];
+ }
+ 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;
+u16 rec_checksum;
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 == UsartReceiveWaitChecksum1) { //若收到校验和包.
+ 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;
+ checksum = ~checksum;
+ rec_checksum = rec_checksum|(data<<8);
+ if(rec_checksum == checksum) //校验通过
+ {
+ switch(pack_msgtype)
+ {
+ case MSG_RW:
+ switch(pack_cmd)
+ {
+ case CMD_WRITE:
+ //从mUsartReceivePack中读取pack_length长度的字节,放到全局变量中
+ 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(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]表示数据长度;
+ //从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 == 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 = UsartReceiveWaitChecksum0;
+ }
+ } 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