zhyinch
2020-11-07 11ba72b7f4c7a545a2053d03ac72f8219d145b22
Ô´Âë/ºËÐİå/Src/application/serial_at_cmd_app.c
@@ -5,6 +5,8 @@
#include "dw_app.h"
#include "RTC.h"
#define MSG_RW       3
#define MSG_WTAG  6
#define CMD_READ   1
#define CMD_WRITE   2
@@ -20,11 +22,26 @@
            UsartReceiveWaitData,
            UsartReceiveWaitChecksum
}UsartRecvPackState;
uint8_t mUsartReceivePack[100] = {0};
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)
{
   static uint8_t send_frame[EUART_RX_BUF_SIZE];
   uint16_t checksum = 0;
   send_frame[0] = 0x55;
   send_frame[1] = 0xAA;
@@ -33,7 +50,7 @@
   send_frame[4] = CMD_REPLY;
   send_frame[5] = index;
   send_frame[6] = data_length;
   memcpy(&send_frame[7], &g_com_map[index], 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];
@@ -43,7 +60,8 @@
   
   UART_PushFrame(send_frame, data_length+9);   
}
u8 remotetag_para[30];
u8 remotesend_state=0;
void UsartParseDataHandler(uint8_t data)
{
   static UsartRecvPackState usart_receive_state = UsartReceiveWaitHead0;
@@ -63,27 +81,39 @@
      checksum += pack_index;
      checksum += pack_datalen;
      checksum += pack_msgtype;
      if((data + checksum)&0xff == 0xFF)            //校验通过
      if(((data + checksum)&0xff) == 0xFF)            //校验通过
      {
         switch(pack_cmd)
         switch(pack_msgtype)
         {
            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();
            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();
                     SendConfigConfirm(pack_msgtype,pack_length);
                     Program_Init();
                     break;
                  case CMD_READ:
                     //read包中data字节,即mUsartReceivePack[0]表示数据长度;
                  //从g_com_data结构体中的第index位置读取长度为mUsartReceivePack[0]的字节,发送出来
                     SendComMap(pack_datalen,pack_index);
                     break;
                  default:
                     break;
               }
               break;
            case CMD_READ:
               //read包中data字节,即mUsartReceivePack[0]表示数据长度;
            //从g_com_data结构体中的第index位置读取长度为mUsartReceivePack[0]的字节,发送出来
               SendComMap(pack_datalen,pack_index>>1);
               case MSG_WTAG:
               remotesend_state++;
               if(remotesend_state>14)
                  remotesend_state=1;
               memcpy(remotetag_para,mUsartReceivePack,REMOTEPARA_LEN);
               break;
            default:
               break;
         }
      }
      }
      usart_receive_state = UsartReceiveWaitHead0;
      pack_index = 0;
@@ -114,7 +144,7 @@
      usart_receive_state = UsartReceiveWaitHead1;
   } else if((usart_receive_state == UsartReceiveWaitHead1) && (data == 0xAA)) {   //收到第二个包头
      usart_receive_state = UsartReceiveWaitMsgType;
   }else if ((usart_receive_state == UsartReceiveWaitMsgType) && (data == 0x3)) {
   }else if ((usart_receive_state == UsartReceiveWaitMsgType)) {
      usart_receive_state = UsartReceiveWaitLength;
      pack_msgtype = data;
   }