zhangbo
4 天以前 64c36ecbd729ba99dcaba24c52512b0edb92a1b5
keil/include/drivers/serial_at_cmd_app.c
@@ -12,6 +12,8 @@
//#include "radio.h"
#include "DBG.h"
#define MSG_RW               3
#define MSG_RW_TAG_ANC       0x30
//#define EUART_RX_BUF_SIZE 100
#define Label_id_local    1     //标签id
@@ -249,7 +251,7 @@
//GPS解析数据
//   static ST_BLERecv BLE_recvive;
//        GGA_DataStruct jdrecv,wdrecv,Posstate;
        GPS_RecvFsm(data);
//        GPS_RecvFsm(data);
//        l_u8GPSBuff[l_u8GPSLen++] = data;
//        if(l_u8GPSLen >= sizeof(l_u8GPSBuff))
//        {
@@ -299,3 +301,184 @@
    }
}
double d_value;
uint16_t pwtag_idrec,pwanc_id,pwanc_value;
uint8_t taguserdata_recflag[TAG_NUM_IN_SYS],pwtag_index = 0;
extern uint16_t mubiao_anchor_ID;
struct pwtag_structure pwtag;
extern uint8_t Get_anchor_appdata_flag;
extern uint8_t Get_anchor_appdata_num;
void Usart1ParseDataHandler(uint8_t data)
{
    if(state5V_prase_flag&&!g_com_map[MODBUS_MODE])
    {   //升级程序
        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;
        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)
              {
                uint16_t serial_tag_id_recv;
                case MSG_RW:
                pack_cmd = mUsartReceivePack[0];
                pack_index = mUsartReceivePack[1];
                pack_datalen = mUsartReceivePack[2];
                switch(pack_cmd)
                {
                case CMD_WRITE:
//                  if(pack_index==MODBUS_MODE*2)
//                  {
//                      Uart1GpsRecDebugSend();
//                      g_com_map[MODBUS_MODE] = 1;
//                      return;
//                  }
                    //从mUsartReceivePack中读取pack_length长度的字节,放到全局变量中,赋值保存的参数并且存入flash
                    memcpy((uint8_t*)&g_com_map + pack_index, &mUsartReceivePack[3], pack_datalen);
                    if(g_com_map[YAOGEIANC_UPWENJIAN_FLAG]==1)
                    {
                    Get_anchor_appdata_flag=1;
                    Get_anchor_appdata_num=0;
                    save_com_map_to_flash();
                    delay_ms(300);
                    SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader
                    break;
                    }
                    if(mUsartReceivePack[0]==1)
                        UpdateProcess(pack_index);
                    //返回一个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>>1);
                    break;
                default:
                   break;
                }
                break;
                case MSG_RW_TAG_ANC:      //批量修改标签参数
                memcpy(&pwtag_idrec,&mUsartReceivePack[2],2);
                if(mUsartReceivePack[0]<0x10)  //判断是修改基站还是修改标签。
                {
                    if(mUsartReceivePack[0]==0x06)//如果是升级
                    {
                    for(uint8_t i=0; i<PWTAG_MAXGROUPNUM; i++)
                    {
                        if(pwtag.groupid[i][0]==pwtag_idrec)
                        {
                            pwtag.duorxie[i]=mUsartReceivePack[0];
                            pwtag.index[i] =  mUsartReceivePack[1];
                            pwtag.remain_time[i] = 5;
                            memcpy(pwtag.groupid[i],&mUsartReceivePack[2],4);
                            pwtag.len[pwtag_index] = pack_length-8;
                            memcpy(&pwtag.value[i][0],&g_com_map[YAOGEIANC_UPWENJIAN_DAXIAO],2);
                            memcpy(&mubiao_anchor_ID,&mUsartReceivePack[2],2);
                            break;
                        }
                        if(i==PWTAG_MAXGROUPNUM-1)
                        {
                            if(pwtag_index++>=PWTAG_MAXGROUPNUM)
                            {
                                pwtag_index = 0;
                            }
                            pwtag.index[pwtag_index] =  mUsartReceivePack[1];
                            pwtag.remain_time[pwtag_index] = 5;
                            pwtag.duorxie[pwtag_index]=mUsartReceivePack[0];
                            memcpy(pwtag.groupid[pwtag_index],&mUsartReceivePack[2],4);
                            pwtag.len[pwtag_index] = pack_length-8;
                            memcpy(&pwtag.value[pwtag_index][0],&g_com_map[YAOGEIANC_UPWENJIAN_DAXIAO],2);
                            memcpy(&mubiao_anchor_ID,&mUsartReceivePack[2],2);
                        }
                    }
                    }
                    else
                    {
                    for(uint8_t i=0; i<PWTAG_MAXGROUPNUM; i++)
                    {
                        if(pwtag.groupid[i][0]==pwtag_idrec)
                        {
                            pwtag.duorxie[i]=mUsartReceivePack[0];
                            pwtag.index[i] =  mUsartReceivePack[1];
                            pwtag.remain_time[i] = 5;
                            memcpy(pwtag.groupid[i],&mUsartReceivePack[2],4);
                            pwtag.len[pwtag_index] = pack_length-8;
                            memcpy(&pwtag.value[i][0],&mUsartReceivePack[6],pack_length-8);
                            break;
                        }
                        if(i==PWTAG_MAXGROUPNUM-1)
                        {
                            if(pwtag_index++>=PWTAG_MAXGROUPNUM)
                            {
                                pwtag_index = 0;
                            }
                            pwtag.index[pwtag_index] =  mUsartReceivePack[1];
                            pwtag.remain_time[pwtag_index] = 5;
                            pwtag.duorxie[pwtag_index]=mUsartReceivePack[0];
                            memcpy(pwtag.groupid[pwtag_index],&mUsartReceivePack[2],4);
                            pwtag.len[pwtag_index] = pack_length-8;
                            memcpy(&pwtag.value[pwtag_index][0],&mUsartReceivePack[6],pack_length-8);
                        }
                    }
                    }
                }
                 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-2) {      //如果收到的index与长度相等
            usart_receive_state = UsartReceiveWaitChecksum;
        }
    } else if(usart_receive_state == UsartReceiveWaitLength) {                  //收到长度字节
        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;
    }
    }
}