zhyinch
2021-01-20 61cbf077fdb7b00a961e4edb6bd9c4065bd3efae
Ô´Âë/ºËÐİå/Src/application/serial_at_cmd_app.c
@@ -11,7 +11,7 @@
#define CMD_READ   1
#define CMD_WRITE   2
#define CMD_REPLY 3
#define EUART_RX_BUF_SIZE 100
#define EUART_RX_BUF_SIZE 200
typedef enum{   UsartReceiveWaitHead0, 
            UsartReceiveWaitHead1,
            UsartReceiveWaitMsgType,
@@ -20,13 +20,29 @@
            UsartReceiveWaitIndex,
            UsartReceiveWaitDataLen,
            UsartReceiveWaitData,
            UsartReceiveWaitChecksum
            UsartReceiveWaitChecksum0,
            UsartReceiveWaitChecksum1
}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);
   USART_puts(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;
@@ -47,6 +63,7 @@
}
u8 remotetag_para[30];
u8 remotesend_state=0;
u16 rec_checksum;
void UsartParseDataHandler(uint8_t data)
{
   static UsartRecvPackState usart_receive_state = UsartReceiveWaitHead0;
@@ -56,7 +73,7 @@
   
   if(usart_receive_state == UsartReceiveWaitChecksum) {         //若收到校验和包
   if(usart_receive_state == UsartReceiveWaitChecksum1) {         //若收到校验和包.
      checksum = 0;
      for(int i = 0; i<pack_length-5; i++) { 
         checksum += mUsartReceivePack[i];
@@ -66,7 +83,9 @@
      checksum += pack_index;
      checksum += pack_datalen;
      checksum += pack_msgtype;
      if((data + checksum)&0xff == 0xFF)            //校验通过
      checksum = ~checksum;
      rec_checksum = rec_checksum|(data<<8);
      if(rec_checksum == checksum)            //校验通过
      {
         switch(pack_msgtype)
         {
@@ -79,8 +98,9 @@
                     //返回一个error状态
                     //SendComMap(pack_datalen,pack_index);
                     save_com_map_to_flash();
                     delay_ms(100);
                     NVIC_SystemReset();
                     SendConfigConfirm(pack_msgtype,pack_length);
                     delay_ms(500);
                     SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader
                     break;
                  case CMD_READ:
                     //read包中data字节,即mUsartReceivePack[0]表示数据长度;
@@ -91,23 +111,27 @@
                     break;
               }
               break;
            case MSG_WTAG:
               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) ) {   //若果收到的是正常通讯包
   } 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 = UsartReceiveWaitChecksum;
         usart_receive_state = UsartReceiveWaitChecksum0;
      }
   } else if(usart_receive_state == UsartReceiveWaitDataLen) {                  //收到指令类型字节
      pack_datalen = data;