/******************************************************************************
* Copyright (c)
* Original Author	:
* Last rev by		:
* Last rev date		:
* Last change list	:
* Overview			:
* Description		:
* NOTE				:
*******************************************************************************/

//-----------------------------------------------------------------------------
// Include
//-----------------------------------------------------------------------------
//#include "sys_protect.h"

#include "mdb_Init.h"
#include "mdb_sci_RX.h"
#include "mdb_sci_TX.h"
#include "f28002x_device.h"
#include "f28002x_examples.h"
#include "MonitorSci.h"
#include "Protocol.h"
//-----------------------------------------------------------------------------
// Adding Conditional Definitions Stuff
//-----------------------------------------------------------------------------


//==================================================================================================
//
//==================================================================================================
Uint16      uiSciBufferDataID;

Uint16      uiSciComID;

Uint16 uiMonitorSciErrorCntNum;

Uint16      uiUpgradeAckFlag;

Uint16  uiMonitorSciRxDataBuff[RECEIVE_DATA_LENGTH+1]; //uiMonitorSciRxDataBuff

Uint16  uiMonitorSciTxDataBuff[SEND_DATA_LENGTH+1]; 

Uint16  uiMonitorSciRxDataBuff_lin[RECEIVE_DATA_LENGTH+1]; //uiMonitorSciRxDataBuff

Uint16  uiMonitorSciTxDataBuff_lin[SEND_DATA_LENGTH+1]; 

Uint16      uiSciBufferDataLength;

st_abcd    strCmdParaTable[ ];

st_abcd    strSetParaTable[ ];

Uint16  uiSetDataBag[eFirmwarDataLen+2];    //设置量

Uint16  uBatSetDataBag[eBatSetDataLen+2];    //设置量BAT 
Uint16  uBmsSetDataBag[eBmsSetDataLen+2];    //设置量BAT 

Uint16  uBusSetDataBag[eBUSSetDataLen+2];    //设置量BUS 
Uint16  uRegSetDataBag[eREGSetDataLen+2];    //设置量reg

//02/04指令

Uint16  uBATWarmDataBag[BAT_Warm_DataLen+2];  
Uint16  uBmsStaDataBag[Bms_staDataLen+2];  //bms 相关

Uint16  uBATFaultDataBag[BAT_Fault_DataLen+2];     
Uint16  uDCDCCharMesDataBag[DCDC_Char_Mes_DataLen+2];     
Uint16  uDCDCReCharMesDataBag[DCDC_ReChar_Mes_DataLen+2];    
Uint16  uMiscDataBag[Misc_DataLen+2];    
Uint16  uTempDataBag[Temp_DataLen+2];    
Uint16  uStaDataBag[Sta_DataLen+2]; 

//==================================================================================================
//
//==================================================================================================
/******************************************************************************
Function Name: SciProtocol_Sci_CommandIdentify
-----------------------------------
Function Descriptions:命令识别
Parameter Name list:
ReturnType:
Refer doctuments:
 ******************************************************************************/
void MonitorSci_CommandIdentify(void)
{
    Uint16 Addr1,Addr2,Addr3;
    if( uiMonitorSciRxDataBuff[0] == uiModbusSlaveAddr )   //从机地址匹配
    {
        MonitorSci_CrcCheck();
        if( unMonitorSciFlag.bit.bCrcCheckError == FALSE )
        {

            if     (uiMonitorSciRxDataBuff[1] == 0x02)     uiSciComID = eComUpRealStatusMonitorSci;
            else if(uiMonitorSciRxDataBuff[1] == 0x03)     uiSciComID = eComUpSetDataMonitorSci;
            else if(uiMonitorSciRxDataBuff[1] == 0x04)     uiSciComID = eComUpRealAnalogMonitorSci;
            else if(uiMonitorSciRxDataBuff[1] == 0x06)
                uiSciComID = eWriteSetSingleDataMonitorSci;
            else if(uiMonitorSciRxDataBuff[1] == 0x10)     uiSciComID = eWriteSetMultipleDataMonitorSci;
            else if(uiMonitorSciRxDataBuff[1] == 0x45)
            {
                uiSciComID = eUpdateProgramMonitorSci;
                return;
            }
            else
                uiSciComID = eInvalidMonitorSci;



            //判断数据查询设置是否超限
            if(uiSciComID != eInvalidMonitorSci)
            {
                uiSciBufferDataID     = ( uiMonitorSciRxDataBuff[2] << 8 ) + uiMonitorSciRxDataBuff[3];
                uiSciBufferDataLength = ( uiMonitorSciRxDataBuff[4] << 8 ) + uiMonitorSciRxDataBuff[5];
                
                Addr1 = uiSciBufferDataID;
                
//                Addr2 = uiSciBufferDataID % 16; //old
//                Addr3 = uiSciBufferDataID + uiSciBufferDataLength-1;  //old                
//                Addr2 = (uiSciBufferDataID + (uiSciBufferDataLength >>4));

                Addr3 = uiSciBufferDataID + uiSciBufferDataLength;


                if(
//                        ((uiSciComID == eComUpRealStatusMonitorSci)  &&(Addr2 > (DSPFAULT_NUMByBit>> 4)))  //02h
                       ((uiSciComID == eComUpRealAnalogMonitorSci)  //04h
                         && (!(
                                ((Addr1 >= Bms_sta_BeginID)&&(Addr1 <= Bms_sta_EndID))
                                ||((Addr1 >= Temp_BeginID)&&(Addr1 <= Temp_EndID))                                
                                ||((Addr1 >= DCDC_ReChar_Mes_BeginID)&&(Addr1 <= DCDC_ReChar_Mes_EndID))
                             )
                           )
                        )
                    || ((uiSciComID == eComUpSetDataMonitorSci)  //03h 读设置量
                         &&(!(
                                ((Addr1 >= AnalogFirmwareBeginID)&&(Addr1 <= AnalogFirmwareEndID))
                                ||((Addr1 >= Bms_SetMesAddrBegin)&&(Addr1 <= Bms_SetMesAddrEnd))
                             )
                           )
                       )
                   || ((uiSciComID == eWriteSetSingleDataMonitorSci)  //06 下达单个设置量     Addr3 为数据内容
                        && (!(
                                ((Addr1 >= AnalogFirmwareBeginID)&&(Addr1 <= AnalogFirmwareEndID))
                                ||((Addr1 >= Bms_SetMesAddrBegin)&&(Addr1 <= Bms_SetMesAddrEnd))
                                ||((Addr1 >= DCDC_Char_Mes_BeginID)&&(Addr1 <= DCDC_Char_Mes_EndID))
                             )
                           )
                      )


                      || (    (uiSciComID == eWriteSetMultipleDataMonitorSci)  //下达多个设置量  0x10
                           && (!(
                                ((Addr1 >= AnalogFirmwareBeginID)&&(Addr1 <= AnalogFirmwareEndID))
                                ||((Addr1 >= Bms_SetMesAddrBegin)&&(Addr1 <= Bms_SetMesAddrEnd))
                                ||((Addr1 >= DCDC_Char_Mes_BeginID)&&(Addr1 <= DCDC_Char_Mes_EndID))
                             )
                           )
                         )

                     || (     (uiSciComID == eComUpRealStatusMonitorSci)  //02h 上传实时告警量和故障量
                           && ( (Addr1 < FaultBeginID)|| (Addr1 > FaultEndID) || ( Addr2 > FaultEndID)
                              )
                        )

                )
                {
                    uiSciComID = eInvalidMonitorSci;
                     uiMonitorSciErrorCntNum++;
                }

            }



        }
        else
        {
            uiSciComID = eInvalidMonitorSci;
            uiMonitorSciErrorCntNum++;
        }



    }
    else
    {
        uiSciComID   = eSlaveAddressMismatchMonitorSci;//从机地址不匹配
    }
}


//==================================================================================================
//
//==================================================================================================
/******************************************************************************
Function Name: MonitorSci_CrcCheck
--------------------------------------
Function Descriptions   :
Parameter Name list :
ReturnTyp           :
Refer doctuments        :
 ******************************************************************************/
void MonitorSci_CrcCheck(void)
{
    static Uint16 uiCrcReciveTemp,uiCrcCheckTemp;
    if(uiReceiveDataLengthCnt < 3)
    {
        unMonitorSciFlag.bit.bCrcCheckError = FALSE;
    }
    else
    {
        uiCrcCheckTemp = MonitorSci_CrcCaculate( &uiMonitorSciRxDataBuff[0],uiReceiveDataLengthCnt - 2 );

        uiCrcReciveTemp = (uiMonitorSciRxDataBuff[uiReceiveDataLengthCnt - 1] << 8) + uiMonitorSciRxDataBuff[uiReceiveDataLengthCnt - 2];

        if( uiCrcReciveTemp == uiCrcCheckTemp ) unMonitorSciFlag.bit.bCrcCheckError = FALSE;
        else unMonitorSciFlag.bit.bCrcCheckError = TRUE;
    }
}


//==================================================================================================
//
//==================================================================================================


//==================================================================================================
//
//==================================================================================================
/******************************************************************************
Function Name: MonitorSci_CommandDeal
-----------------------------------
Function Descriptions:
Parameter Name list:
ReturnType:
Refer doctuments:
 ******************************************************************************/
void MonitorSci_CommandDeal(void)
{
    int16 iReceiveDataTemp,iReceiveDataError;
    switch( uiSciComID )
    {
    case eUpdateProgramMonitorSci:   //  8 升级指令
    {
        Uint16 uiAckLen = 0;
//        uiUpgradeAckFlag = Upgrade_Parse(&uiMonitorSciRxDataBuff[0], &uiMonitorSciTxDataBuff[0]);
        if(uiUpgradeAckFlag == TRUE)
        {
            if(uiMonitorSciTxDataBuff[1] == 0xC5)   //异常响应
            {
                //异常响应字节长度=地址(1)+功能码(1)+命令码(1)+数据内容(1)+CRC校验(2) = 6；
                uiAckLen = 6;
                MonitorSci_RenewTxBuffer(eInvalidMonitorSci,0,uiAckLen);
                uiMonitorSciErrorCntNum++;
            }
            else
            {
                //正常响应字节长度=地址(1)+功能码(1)+命令码(1)+有效数据字节长度(2)+数据内容(N)+CRC校验(2) = N+7；
                uiAckLen = (uiMonitorSciTxDataBuff[3]<<8) + uiMonitorSciTxDataBuff[4] + 7;
                MonitorSci_RenewTxBuffer(eUpdateProgramMonitorSci,0, uiAckLen);
            }
        }
        break;
    }
    case eComUpSlaveAddressMonitorSci: //0 上传从机地址
    {
        uiBuffLength = 1;
        uiMonitorSciTxDataBuff[0] = uiModbusSlaveAddr;
        break;
    }
    case eComUpRealStatusMonitorSci:  //1 上传实时告警量和故障量
    {
        MonitorSci_RenewTxBuffer(eComUpRealStatusMonitorSci,uiSciBufferDataID,uiSciBufferDataLength);
        break;
    }
    case eComUpRealAnalogMonitorSci: //2 上传实时模拟量
    {
        MonitorSci_RenewTxBuffer(eComUpRealAnalogMonitorSci,uiSciBufferDataID,uiSciBufferDataLength);
        break;
    }
    case eComUpSetDataMonitorSci: //3 上传设置量
    {
        MonitorSci_RenewTxBuffer(eComUpSetDataMonitorSci,uiSciBufferDataID,uiSciBufferDataLength);
        break;
    }
    case eWriteSetSingleDataMonitorSci: // 06下达单个设置量
    {
        iReceiveDataError = 0;

        if((uiSciBufferDataID <= AnalogFirmwareEndID)  && (uiSciBufferDataID >= AnalogFirmwareBeginID))
        {
            iReceiveDataTemp = (uiMonitorSciRxDataBuff[4]<<8) + uiMonitorSciRxDataBuff[5];
            
            uiSetDataBag[uiSciBufferDataID-AnalogFirmwareBeginID] = iReceiveDataTemp;
        }
        else if((uiSciBufferDataID <= DCDC_Char_Mes_EndID)&&(uiSciBufferDataID >= DCDC_Char_Mes_BeginID))
        {
            iReceiveDataTemp = (uiMonitorSciRxDataBuff[4]<<8) + uiMonitorSciRxDataBuff[5];
            uDCDCCharMesDataBag[uiSciBufferDataID - DCDC_Char_Mes_BeginID] = iReceiveDataTemp;           
        }
        else if((uiSciBufferDataID <= Bms_SetMesAddrEnd)&&(uiSciBufferDataID >= Bms_SetMesAddrBegin))
        {
            iReceiveDataTemp = (uiMonitorSciRxDataBuff[4]<<8) + uiMonitorSciRxDataBuff[5];
            uBmsSetDataBag[uiSciBufferDataID - Bms_SetMesAddrBegin] = iReceiveDataTemp;
        }
        
        if(iReceiveDataError == 0)
        {
            MonitorSci_RenewTxBuffer(eWriteSetSingleDataMonitorSci,0, 8 - 2);
        }
        else
        {
            MonitorSci_RenewTxBuffer(eInvalidMonitorSci,0,3);
            uiMonitorSciErrorCntNum++;
        }
        break;
    }
    case eWriteSetMultipleDataMonitorSci://10
    {
        Uint16 usLoop,pSciBuffAddr = 7;




        iReceiveDataError = 0;
        if( ((uiSciBufferDataID <= AnalogFirmwareEndID)  && (uiSciBufferDataID >= AnalogFirmwareBeginID))
           && ((uiSciBufferDataID + uiSciBufferDataLength) <= AnalogFirmwareEndID)
          )
        {
            for(usLoop = uiSciBufferDataID; usLoop < (uiSciBufferDataID + uiSciBufferDataLength); usLoop++)
            {
                iReceiveDataTemp = (uiMonitorSciRxDataBuff[pSciBuffAddr]<<8) + uiMonitorSciRxDataBuff[pSciBuffAddr+1];
                
                uiSetDataBag[uiSciBufferDataID-AnalogFirmwareBeginID] = iReceiveDataTemp;

                pSciBuffAddr = pSciBuffAddr + 2;
           }
        }
        else if(   uiSciBufferDataID < DCDC_Char_Mes_EndID
                && uiSciBufferDataID >= DCDC_Char_Mes_BeginID
                && ((uiSciBufferDataID + uiSciBufferDataLength) <= DCDC_Char_Mes_EndID)
        )
        {
            for(usLoop = uiSciBufferDataID; usLoop < (uiSciBufferDataID + uiSciBufferDataLength); usLoop++)
            {
                iReceiveDataTemp = (uiMonitorSciRxDataBuff[pSciBuffAddr]<<8) + uiMonitorSciRxDataBuff[pSciBuffAddr+1];
//                    uDCDCCharMesDataBag[uiSciBufferDataID - DCDC_Char_Mes_BeginID] = iReceiveDataTemp;           
                uDCDCCharMesDataBag[usLoop - DCDC_Char_Mes_BeginID] = iReceiveDataTemp;
   
                pSciBuffAddr = pSciBuffAddr + 2;
            }
        }
        else if(   uiSciBufferDataID < Bms_SetMesAddrEnd
                && uiSciBufferDataID >= Bms_SetMesAddrEnd
                && ((uiSciBufferDataID + uiSciBufferDataLength) <= Bms_SetMesAddrEnd)
        )
        {
            for(usLoop = uiSciBufferDataID; usLoop < (uiSciBufferDataID + uiSciBufferDataLength); usLoop++)
            {
                iReceiveDataTemp = (uiMonitorSciRxDataBuff[pSciBuffAddr]<<8) + uiMonitorSciRxDataBuff[pSciBuffAddr+1];
//uBmsSetDataBag[uiSciBufferDataID - Bms_SetMesAddrBegin] = iReceiveDataTemp;
                uBmsSetDataBag[usLoop - Bms_SetMesAddrBegin] = iReceiveDataTemp;
   
                pSciBuffAddr = pSciBuffAddr + 2;
            }
        }


        if( iReceiveDataError == 0 )
        {
            MonitorSci_RenewTxBuffer(eWriteSetMultipleDataMonitorSci,0,8 - 2);
        }
        else
        {
            MonitorSci_RenewTxBuffer(eInvalidMonitorSci,0,3);
            uiMonitorSciErrorCntNum++;
        }
        break;
    }
    case eInvalidMonitorSci:
    {
        uiBuffLength = 1;
        MonitorSci_RenewTxBuffer(eInvalidMonitorSci,0,3);
        uiMonitorSciErrorCntNum++;
        break;
    }
    case eSlaveAddressMismatchMonitorSci:
    {
        unMonitorSciFlag.bit.bCmdDealFinish = 1;
        uiBuffLength                    = 0;//从机地址不匹配需要清除发送数组长度
        break;
    }
    default:
        break;
    }
}
/******************************************************************************
Function Name: MonitorSci_RenewTxBuffer
--------------------------------
Function Descriptions:
Parameter Name list:
ReturnType:
Refer doctuments:
 ******************************************************************************/
Uint16  uiRenewTxBufferTemp;
Uint16  uiRenewTxBufferCnt;
Uint16  uiUpLoadDataFrameLength;
Uint16  uiUpLoadDataFinish;
Uint16  uiUpLoadDataID;
Uint16  *ptrRenewTxBufferBaseAddr;
Uint16  uiRenewTxBufferLength;
Uint16  *uiAddressEndLimit;
void MonitorSci_RenewTxBuffer(Uint16 uiUpLoadDataType,Uint16 uiUpLoadDataFirstAddr,Uint16 uiUpLoadDataLength)
{
#define SciUploadBufferNumOnce 5

    Uint16  uiRenewBufferLenTemp;
    Uint16 i;

    if( unMonitorSciFlag.bit.bToCmdDealFinish == FALSE )
    {
        switch(uiUpLoadDataType)
        {
            case eUpdateProgramMonitorSci:
            {
                ptrRenewTxBufferBaseAddr = &uiMonitorSciTxDataBuff[0];
                uiRenewTxBufferLength    = 0;
                uiUpLoadDataID           = 0;
                uiUpLoadDataFrameLength  = 0;
                uiAddressEndLimit        = &uiMonitorSciTxDataBuff[SEND_DATA_LENGTH-1];
                uiBuffLength             = uiUpLoadDataLength;
                unMonitorSciFlag.bit.bCmdDealFinish = 1;
                return;
            }
            case eComUpRealAnalogMonitorSci:  // 04 上传实时模拟量
            {
                if((uiUpLoadDataFirstAddr >= Bms_sta_BeginID)&&(uiUpLoadDataFirstAddr <= Bms_sta_EndID))
                {
                    ptrRenewTxBufferBaseAddr = &uBmsStaDataBag[0];
                    uiMonitorSciTxDataBuff[2]    = uiUpLoadDataLength*2;//上传数据的字节数
                    uiRenewTxBufferLength    = 3;
                    uiUpLoadDataID           = uiUpLoadDataFirstAddr - Bms_sta_BeginID;
                    uiUpLoadDataFrameLength  = uiUpLoadDataLength;                    
                    uiAddressEndLimit        = &uBmsStaDataBag[Bms_staDataLen];
                }
                else if((uiUpLoadDataFirstAddr >= Temp_BeginID)&&(uiUpLoadDataFirstAddr <= Temp_EndID))
                {
                    ptrRenewTxBufferBaseAddr = &uTempDataBag[0];
                    uiMonitorSciTxDataBuff[2]    = uiUpLoadDataLength*2;//上传数据的字节数
                    uiRenewTxBufferLength    = 3;
                    uiUpLoadDataID           = uiUpLoadDataFirstAddr - Temp_BeginID;
                    uiUpLoadDataFrameLength  = uiUpLoadDataLength;
                    uiAddressEndLimit        = &uTempDataBag[Temp_DataLen];
                } 
                else if((uiUpLoadDataFirstAddr >= DCDC_ReChar_Mes_BeginID)&&(uiUpLoadDataFirstAddr <= DCDC_ReChar_Mes_EndID))
                {
                   ptrRenewTxBufferBaseAddr = &uDCDCReCharMesDataBag[0];
                   uiMonitorSciTxDataBuff[2]    = uiUpLoadDataLength*2;//上传数据的字节数
                   uiRenewTxBufferLength    = 3;
                   uiUpLoadDataID          = uiUpLoadDataFirstAddr - DCDC_ReChar_Mes_BeginID;
                   uiUpLoadDataFrameLength  = uiUpLoadDataLength;
                   uiAddressEndLimit        = &uDCDCReCharMesDataBag[DCDC_ReChar_Mes_DataLen];
                } 
                
                break;
            }
            case eComUpRealStatusMonitorSci:   // 0x2    1 上传实时告警量和故障量
            {
                ptrRenewTxBufferBaseAddr = &UserAdminFault.uiDigital[0];

                if(uiUpLoadDataLength%8 == 0)
                    uiMonitorSciTxDataBuff[2] = (uiUpLoadDataLength>>3);  //上传数据的字节数
                else
                    uiMonitorSciTxDataBuff[2] = (uiUpLoadDataLength>>3) + 1;

                uiRenewTxBufferLength   = 3;
                uiUpLoadDataID           = uiUpLoadDataFirstAddr - FaultBeginID;
                uiUpLoadDataFrameLength = uiUpLoadDataLength    >> 4;//数据长度16位为一个单位  // 上传多少个16位数
                uiAddressEndLimit       = &UserAdminFault.uiDigital[(DIGITAL_NUM>>4) + 1];

                break;

    //            ptrRenewTxBufferBaseAddr = &strFaultFlag.unFault.uiFaultArray[0];
    //            if(uiUpLoadDataLength%8 == 0)uiMonitorSciTxDataBuff[2] = (uiUpLoadDataLength>>3);
    //            else                         uiMonitorSciTxDataBuff[2] = (uiUpLoadDataLength>>3) + 1;
    //            uiRenewTxBufferLength   = 3;
    //            uiUpLoadDataID          = uiUpLoadDataFirstAddr >> 4;//离散量以16为一个单位长度
    //            uiUpLoadDataFrameLength = uiUpLoadDataLength    >> 4;//数据长度16位为一个单位
    //            uiAddressEndLimit       = &strFaultFlag.unFault.uiFaultArray[DSPFAULT_NUM];
    //            break;
            }
            case eComUpSetDataMonitorSci:  //03 上传设置量
            {
                if((uiUpLoadDataFirstAddr<=AnalogFirmwareEndID)&&(uiUpLoadDataFirstAddr>=AnalogFirmwareBeginID))
                {
                   ptrRenewTxBufferBaseAddr = &uiSetDataBag[0];
                   uiMonitorSciTxDataBuff[2]= uiUpLoadDataLength*2;                 //上传数据的字节数
                   uiRenewTxBufferLength    = 3;
                   uiUpLoadDataID           = uiUpLoadDataFirstAddr-AnalogFirmwareBeginID;
                   uiUpLoadDataFrameLength  = uiUpLoadDataLength;
                   uiAddressEndLimit        = &uiSetDataBag[eFirmwarDataLen];
                }
                else if((uiUpLoadDataFirstAddr <= Bms_SetMesAddrEnd)&&(uiUpLoadDataFirstAddr >= Bms_SetMesAddrBegin))
                {
                     ptrRenewTxBufferBaseAddr = &uBmsSetDataBag[0];
                     uiMonitorSciTxDataBuff[2]    = uiUpLoadDataLength*2;  //上传数据的字节数
                     uiRenewTxBufferLength    = 3;
                     uiUpLoadDataID           = uiUpLoadDataFirstAddr-Bms_SetMesAddrBegin;
                     uiUpLoadDataFrameLength  = uiUpLoadDataLength;
                     uiAddressEndLimit        = &uBmsSetDataBag[eBmsSetDataLen];
                }               
                else
                {
                      ptrRenewTxBufferBaseAddr = &uiMonitorSciRxDataBuff[0];
                      uiRenewTxBufferLength    = 0;
                      uiUpLoadDataID           = 0;
                      uiUpLoadDataFrameLength  = uiUpLoadDataLength;
                      uiAddressEndLimit        = &uiMonitorSciRxDataBuff[RECEIVE_DATA_LENGTH-1];
                }
                break;
            }
            case eWriteSetSingleDataMonitorSci://06
            {
                ptrRenewTxBufferBaseAddr = &uiMonitorSciRxDataBuff[0];
                uiRenewTxBufferLength    = 0;
                uiUpLoadDataID           = 0;
                uiUpLoadDataFrameLength  = uiUpLoadDataLength;
                uiAddressEndLimit        = &uiMonitorSciRxDataBuff[RECEIVE_DATA_LENGTH-1];
                break;
            }
            case eWriteSetMultipleDataMonitorSci://10
            {
                ptrRenewTxBufferBaseAddr = &uiMonitorSciRxDataBuff[0];
                uiRenewTxBufferLength    = 0;
                uiUpLoadDataID           = 0;
                uiUpLoadDataFrameLength  = uiUpLoadDataLength;
                uiAddressEndLimit        = &uiMonitorSciRxDataBuff[RECEIVE_DATA_LENGTH-1];
                break;
            }
            case eInvalidMonitorSci:
            {
                ptrRenewTxBufferBaseAddr = &uiMonitorSciRxDataBuff[0];
                uiRenewTxBufferLength    = 0;
                uiUpLoadDataID           = 0;
                uiUpLoadDataFrameLength  = uiUpLoadDataLength;
                uiAddressEndLimit        = &uiMonitorSciRxDataBuff[RECEIVE_DATA_LENGTH-1];
                break;
            }
        }
        uiMonitorSciTxDataBuff[0] = uiMonitorSciRxDataBuff[0];
        uiMonitorSciTxDataBuff[1] = uiMonitorSciRxDataBuff[1];
        //-----------------------------------------------------
        //处理需更新的数据长度
        //-----------------------------------------------------
        uiUpLoadDataFinish = 0;
        uiRenewTxBufferCnt = 0;
        unMonitorSciFlag.bit.bToCmdDealFinish = TRUE;
    }
    
    if(uiUpLoadDataFrameLength > SCI_UPLOAD_FRAME_WORDS )
    {
        uiUpLoadDataFrameLength = SCI_UPLOAD_FRAME_WORDS;
    }
    //-----------------------------------------------------
    //更新发送缓冲区,每一次SciUploadBufferNumOnce个
    //-----------------------------------------------------
    if(uiUpLoadDataFrameLength <= SciUploadBufferNumOnce)
    {
        uiRenewBufferLenTemp    = uiUpLoadDataFrameLength;
        uiUpLoadDataFrameLength = 0;
        uiUpLoadDataFinish      = 1;
    }
    else
    {
        uiRenewBufferLenTemp    = SciUploadBufferNumOnce;
        uiUpLoadDataFrameLength = uiUpLoadDataFrameLength - SciUploadBufferNumOnce;
    }

    for(uiRenewTxBufferCnt=0;uiRenewTxBufferCnt<uiRenewBufferLenTemp;uiRenewTxBufferCnt++)
    {
        //for循环不能太大，否则造成中断响应出错，for是一条语句。
        if((ptrRenewTxBufferBaseAddr + uiUpLoadDataID)>uiAddressEndLimit)
        {
            uiUpLoadDataFinish  = 1;
            uiRenewTxBufferTemp = 0xFFFF;
            break;
        }
        else 
        {
            uiRenewTxBufferTemp = *(ptrRenewTxBufferBaseAddr + uiUpLoadDataID);
        }
        
        if(uiUpLoadDataType == eComUpRealAnalogMonitorSci||uiUpLoadDataType == eComUpSetDataMonitorSci) 
        //写设置数据的响应命令:原数据返回
        {
            uiMonitorSciTxDataBuff[uiRenewTxBufferLength ++] = uiRenewTxBufferTemp >> 8;    //先传数据的高字节
            uiMonitorSciTxDataBuff[uiRenewTxBufferLength ++] = uiRenewTxBufferTemp & 0xFF;  //再传数据的低字节
            uiUpLoadDataID ++;
        }
        else if(uiUpLoadDataType == eComUpRealStatusMonitorSci)
        {
            uiMonitorSciTxDataBuff[uiRenewTxBufferLength ++] = uiRenewTxBufferTemp & 0xFF;  //先传数据的高字节
            uiMonitorSciTxDataBuff[uiRenewTxBufferLength ++] = uiRenewTxBufferTemp >> 8;    //传数据的低字节
            uiUpLoadDataID ++;
        }
        else// if(uiUpLoadDataType == eWriteSetSingleDataID)||(uiUpLoadDataType == eWriteSetMultipleDataID)
        {
            uiMonitorSciTxDataBuff[uiRenewTxBufferLength ++] = uiRenewTxBufferTemp & 0xFF;  //再传数据的低字节
            uiUpLoadDataID ++;
        }       
    }

    //---------------------------------------------------------
    //接收异常或指令无效时返回对应故障码
    //---------------------------------------------------------
    if( uiUpLoadDataType == eInvalidMonitorSci )
    {
        uiMonitorSciTxDataBuff[uiRenewTxBufferLength] = UartFunCodeError;
    }
    //-----------------------------------------------------
    //是否更新完发送缓冲区，计算CRC
    //-----------------------------------------------------
    if( uiUpLoadDataFinish )
    {
        uiRenewTxBufferTemp = MonitorSci_CrcCaculate( &uiMonitorSciTxDataBuff[0], uiRenewTxBufferLength );
        uiMonitorSciTxDataBuff[uiRenewTxBufferLength ++] = uiRenewTxBufferTemp & 0xFF; //先传校验码的低字节
        uiMonitorSciTxDataBuff[uiRenewTxBufferLength ++] = uiRenewTxBufferTemp >> 8;   //再传校验码的高字节
        uiBuffLength = uiRenewTxBufferLength;
        unMonitorSciFlag.bit.bCmdDealFinish = 1;
    }
}


//==================================================================================================
//
//==================================================================================================


//==================================================================================================
//
//==================================================================================================


//==================================================================================================
//
//==================================================================================================

