#include "uart1.h"
#include <QSerialPortInfo>
#include <QSerialPort>
#include <QList>
#include <QDebug>
#include <stdio.h>
#include <QObject>
#include <QDebug>
#include "communications.h"
#include <QWidget>
#include  "MCU_ARM.h"
#include <stdio.h>
#include <QFileInfo>
#include <QTime>
#include <QTimer>
#include "mcu_arm1.h"

uint16_t CRC1;
QSerialPortInfo *SerialPortInfo1;
QSerialPort SerialPort1;
QByteArray UART1_READ_DATA;
mcu_arm1 *ARM1;
uint16_t UART0_ini = 0;
uint16_t UART1_ini = 0;
uint8_t UART1_Enable = 0;
QByteArray abc = "TEST SUCESS";

struct UU_data1 SEC;
struct UU_dataana ANA;
struct UU_dataacc ACC;
struct UU_datadig DIG = {"48","48","48","48","48",0,0,0,0,0,"0","0",false,false,
                         false,false,false};
struct UU_datadig_compare DIGCOMPARE;
UART1::UART1(QWidget *parent) :
    QWidget(parent)
{
    UartFrameReset();
}

UART1::~UART1()
{
}

void Delay_MSec_Temp(unsigned int msec)
{
    QEventLoop eventloop;
    QTimer::singleShot((int)msec, &eventloop, SLOT(quit()));
    eventloop.exec();
}


void UART1::Delay_MSec(unsigned int msec)
{
    QEventLoop eventloop;
    QTimer::singleShot((int)msec, &eventloop, SLOT(quit()));
    eventloop.exec();
}

void UART1::UartFrameProcess(QByteArray & data)
{
    qDebug() <<"UartFrameProcess:"<< data.toHex();

    if(data.toStdString()!="")
    {
        QByteArray ba;
        QByteArray test;
        QByteArray data1;
        QByteArray data_war;
        QByteArray ZiJian_state;
        QString ttest;
        data1[0]=0xAA;
        data1[1]=0x55;
        QByteArray data_police;
        uint32_t dataLen = 0;
        uint32_t result = 0;
        uint32_t wireless_count = 0;
        uint32_t wireless_adress_count = 0;
        uint32_t wireless_data_count = 0;
        uint32_t concentrator_count = 0;
        uint32_t concentrator_adress_count = 0;
        uint32_t concentrator_data_count = 0;
        uint8_t lower_CRC = 0;
        uint32_t wireless_checkcount = 0;
        uint8_t temperature_order = 0;               //温度获取指令  23.8.16
        int year,month,day,hour,minute,seconds;
        QString ass_year,ass_month,ass_day,ass_hour,ass_minute,ass_seconds;
        QString Sys_time;
        char Check[2] = {0};
        int led_flage=0,led_flage1=0,led_flage2=0,led_flage3=0,led_flage4=0,led_flage5=0;
        int led_data;
        uint32_t waring_data;
        int zijian;
        bool ok;
        QString aaa = ",";
        QString led_set;
        QByteArray CrcCheck = data.left(data.size()-2);
        if((data[0] == data1[0])&&(data[1]==data1[1]))
        {
            ModBusCRC16(CrcCheck);
            lower_CRC = CRC1;
            CRC1 = CRC1>>8;
            Check[0] = lower_CRC;
            Check[1] = CRC1;
            if((Check[0] == data[data.size()-2])&&(Check[1] == data[data.size()-1])&&(data.data()!=NULL))
            {
                if(data.at(2) >= 0x20 && data.at(2) <=0x30)
                {
                    dataLen = data.size();
                    result = data.at(dataLen - 3);
                    if(result == 0)
                    {
                        printf("success...\n");
                    }
                    else
                    {
                        happenInfoError(data);
                    }
                }
                else
                {
                    char *temp = data.data();
                    temp = temp + 5;
                    char *Sec = data.data();
                    Sec = Sec + 4;
                    char *adress = data.data();
                    adress = adress + 6;
                    char *sec = data.data();
                    sec = sec + 6;
                    char *rtctime = data.data();
                    rtctime = rtctime + 6;
                    char *wireless_data = data.data();
                    char *concentrator_data = data.data();
                    switch (data.at(2))
                    {
                    case 0x01:
                        wireless_count = data.at(6);
                        wireless_adress_count = wireless_count*4;
                        wireless_data_count = wireless_count*2;
                        wireless_data =wireless_data + 6 +wireless_adress_count ;
                        SEC.wirelessTData_account.clear();
                        memcpySerialData(temp+1,SEC.wirelessTData_account,1);

                        SEC.wirelessTData_adress.clear();
                        memcpySerialData(adress+1,SEC.wirelessTData_adress,wireless_adress_count);

                        SEC.wirelessTData_data.clear();
                        memcpySerialData(wireless_data+1,SEC.wirelessTData_data,wireless_data_count);

                        wireless_data = NULL;
                        adress = NULL;
                        temp = NULL;
                        break;
                    case 0x02:
                        concentrator_count = data.at(5);
                        concentrator_adress_count = concentrator_count;
                        concentrator_data_count = concentrator_count*2;
                        concentrator_data =concentrator_data + 6 +concentrator_adress_count ;
                        SEC.concentratorTData_adress.clear();
                        memcpySerialData(adress,SEC.concentratorTData_adress,concentrator_adress_count);

                        SEC.concentratorTData_data.clear();
                        memcpySerialData(concentrator_data,SEC.concentratorTData_data,concentrator_data_count);

                        concentrator_data = NULL;
                        adress = NULL;
                        temp = NULL;
                        break;
                    case 0x03:
                        temp = temp+1;
                        dataLen = (data.at(4)) << 8 | data.at(5);
                        if(data.at(3) == 0x01)
                        {
                            /***************************************************************/
                            DIGCOMPARE.policeDataDig = DIG.policeDataDig;
                            DIG.policeDataDig.clear();
//                            SEC.policeData.clear();
//                            memcpySerialData(temp,SEC.policeData,1);
                            memcpySerialData(temp,DIG.policeDataDig,1);
                            if(DIGCOMPARE.policeDataDig != DIG.policeDataDig)
                            {
                                DIG.policeDataDig_flag = true;
                            }else{
                                DIG.policeDataDig_flag = false;
                            }

                            temp = temp +1;
                            data_police.clear();
                            data_police[0] = data[6];
                            if((data_police.toHex().toInt(&ok,16))%4/2){
                                QFile file("/home/data/Pop_ult.txt");
                                if(file.open(QFile::WriteOnly))
                                {
                                    QTextStream set_wifi_connect(&file);
                                }
                                file.close();
                            }
                            else
                            {
                                QFile file("/home/data/Pop_ult.txt");
                                file.remove();
                            }
                            if((data_police.toHex().toInt(&ok,16))%2/1){
                                QFile file("/home/data/Pop_tev.txt");
                                if(file.open(QFile::WriteOnly))
                                {
                                    QTextStream set_wifi_connect(&file);
                                }
                                file.close();
                            }
                            else
                            {
                                QFile file("/home/data/Pop_tev.txt");
                                file.remove();
                            }
                            /***************************************************************/
                            SEC.TevTop.clear();
                            memcpySerialData(temp,SEC.TevTop,2);
                            temp = temp +2;
                            /***************************************************************/
                            SEC.TevFreq.clear();
                            memcpySerialData(temp,SEC.TevFreq,2);
                            temp = temp +2;
                            /***************************************************************/
                            SEC.TevLevel.clear();
                            memcpySerialData(temp,SEC.TevLevel,2);
                            temp = temp +2;
                            /***************************************************************/
                            SEC.TevAve.clear();
                            memcpySerialData(temp,SEC.TevAve,2);
                            temp = temp +2;
                            /***************************************************************/
                            SEC.Tevperiodicity.clear();
                            memcpySerialData(temp,SEC.Tevperiodicity,2);
                            temp = temp +2;
                            /***************************************************************/
                            SEC.UltTop.clear();
                            memcpySerialData(temp,SEC.UltTop,2);
                            temp = temp +2;
                            /***************************************************************/
                            SEC.UltFreq.clear();
                            memcpySerialData(temp,SEC.UltFreq,2);
                            temp = temp + 2;
                            /***************************************************************/
                            SEC.UltLevel.clear();
                            memcpySerialData(temp,SEC.UltLevel,2);
                            temp = temp +2;
                            /***************************************************************/
                            SEC.UltAve.clear();
                            memcpySerialData(temp,SEC.UltAve,2);
                            temp = temp +2;
                            /***************************************************************/
                            SEC.UltEffective.clear();
                            memcpySerialData(temp,SEC.UltEffective,2);
                            temp = temp +2;
                        }
                        else if(data.at(3) == 0x02)
                        {
                            SEC.tevData.clear();
                            memcpySerialData(temp+1,SEC.tevData,dataLen);
                        }
                        else if(data.at(3) == 0x03)
                        {
                            SEC.ultData.clear();
                            memcpySerialData(temp+1,SEC.ultData,dataLen);
                        }
                        temp = NULL;
                        break;
                    case 0x04:
                        /***************************************************************/
                        SEC.SecFanStartTemperature.clear();
                        memcpySerialData(sec,SEC.SecFanStartTemperature,4);
                        sec = sec + 4;
                        /***************************************************************/
                        SEC.SecTripTemperature.clear();
                        memcpySerialData(sec,SEC.SecTripTemperature,4);
                        sec = sec + 4;
                        /***************************************************************/
                        SEC.SecAlarmTemperature.clear();
                        memcpySerialData(sec,SEC.SecAlarmTemperature,4);
                        sec = sec + 4;
                        /***************************************************************/
                        SEC.SecReturnTemperature.clear();
                        memcpySerialData(sec,SEC.SecReturnTemperature,4);
                        sec = sec + 4;
                        /***************************************************************/
                        SEC.SecFanStartTempeture.clear();
                        memcpySerialData(sec,SEC.SecFanStartTempeture,12);
                        sec = sec + 12;
                        /***************************************************************/
//                        SEC.SecWheelVol.clear();
//                        memcpySerialData(sec,SEC.SecWheelVol,12);
//                        sec = sec + 12;

                        SEC.SecWheelVolAA.clear();
                        memcpySerialData(sec,SEC.SecWheelVolAA,4);
                        sec = sec + 4;

                        SEC.SecWheelVolBB.clear();
                        memcpySerialData(sec,SEC.SecWheelVolBB,4);
                        sec = sec + 4;

                        SEC.SecWheelVolCC.clear();
                        memcpySerialData(sec,SEC.SecWheelVolCC,4);
                        sec = sec + 4;
                        /***************************************************************/
                        SEC.SecZeroSequenceVol.clear();
                        memcpySerialData(sec,SEC.SecZeroSequenceVol,4);
                        sec = sec + 4;
                        /***************************************************************/
//                        SEC.SecActivePower.clear();
//                        memcpySerialData(sec,SEC.SecActivePower,12);
//                        sec = sec + 12;

                        SEC.SecActivePowerAA.clear();
                        memcpySerialData(sec,SEC.SecActivePowerAA,4);
                        sec = sec + 4;

                        SEC.SecActivePowerBB.clear();
                        memcpySerialData(sec,SEC.SecActivePowerBB,4);
                        sec = sec + 4;

                        SEC.SecActivePowerCC.clear();
                        memcpySerialData(sec,SEC.SecActivePowerCC,4);
                        sec = sec + 4;
                        /***************************************************************/
                        SEC.SecActivePowerCom.clear();
                        memcpySerialData(sec,SEC.SecActivePowerCom,4);
                        sec = sec + 4;
                        /***************************************************************/
//                        SEC.SecReactivePower.clear();
//                        memcpySerialData(sec,SEC.SecReactivePower,12);
//                        sec = sec + 12;

                        SEC.SecReactivePowerAA.clear();
                        memcpySerialData(sec,SEC.SecReactivePowerAA,4);
                        sec = sec + 4;

                        SEC.SecReactivePowerBB.clear();
                        memcpySerialData(sec,SEC.SecReactivePowerBB,4);
                        sec = sec + 4;

                        SEC.SecReactivePowerCC.clear();
                        memcpySerialData(sec,SEC.SecReactivePowerCC,4);
                        sec = sec + 4;
                        /***************************************************************/
                        SEC.SecReactivePowerCom.clear();
                        memcpySerialData(sec,SEC.SecReactivePowerCom,4);
                        sec = sec + 4;
                        /***************************************************************/
                        SEC.SecWheelPowerFactor.clear();
                       memcpySerialData(sec,SEC.SecWheelPowerFactor,12);
//                        sec = sec + 12;

                        SEC.SecWheelPowerFactorAA.clear();
                       memcpySerialData(sec,SEC.SecWheelPowerFactorAA,4);
                        sec = sec + 4;

                        SEC.SecWheelPowerFactorBB.clear();
                       memcpySerialData(sec,SEC.SecWheelPowerFactorBB,4);
                        sec = sec + 4;

                        SEC.SecWheelPowerFactorCC.clear();
                       memcpySerialData(sec,SEC.SecWheelPowerFactorCC,4);
                        sec = sec + 4;
                        /***************************************************************/
                        SEC.SecWheelPowerFactorCom.clear();
                        memcpySerialData(sec,SEC.SecWheelPowerFactorCom,4);
                        sec = sec + 4;
                        /***************************************************************/
                        SEC.SecWheelFrequency.clear();
                        memcpySerialData(sec,SEC.SecWheelFrequency,4);
                        sec = sec + 4;
                        /***************************************************************/
                        SEC.SecWheelATemperature.clear();
                        memcpySerialData(sec,SEC.SecWheelATemperature,4);
                        sec = sec + 4;
                        /***************************************************************/
                        SEC.SecWheelBTemperature.clear();
                        memcpySerialData(sec,SEC.SecWheelBTemperature,4);
                        sec = sec + 4;
                        /***************************************************************/
                        SEC.SecWheelCTemperature.clear();
                        memcpySerialData(sec,SEC.SecWheelCTemperature,4);
                        sec = sec + 4;
                        /***************************************************************/
                        SEC.SecWheelTTemperature.clear();
                        memcpySerialData(sec,SEC.SecWheelTTemperature,4);
                        sec = sec + 4;
                        /***************************************************************/
//                        SEC.SecWheelPositiveElectricEnergy.clear();
//                        memcpySerialData(sec,SEC.SecWheelPositiveElectricEnergy,20);
//                        sec = sec + 20;

                        SEC.SecWheelPositiveElectricEnergyzong.clear();
                        memcpySerialData(sec,SEC.SecWheelPositiveElectricEnergyzong,4);
                        sec = sec + 4;

                        SEC.SecWheelPositiveElectricEnergyjian.clear();
                        memcpySerialData(sec,SEC.SecWheelPositiveElectricEnergyjian,4);
                        sec = sec + 4;

                        SEC.SecWheelPositiveElectricEnergyfeng.clear();
                        memcpySerialData(sec,SEC.SecWheelPositiveElectricEnergyfeng,4);
                        sec = sec + 4;

                        SEC.SecWheelPositiveElectricEnergyping.clear();
                        memcpySerialData(sec,SEC.SecWheelPositiveElectricEnergyping,4);
                        sec = sec + 4;

                        SEC.SecWheelPositiveElectricEnergygu.clear();
                        memcpySerialData(sec,SEC.SecWheelPositiveElectricEnergygu,4);
                        sec = sec + 4;
                        /***************************************************************/
//                        SEC.SecWheelNegativeElectricEnergy.clear();
//                        memcpySerialData(sec,SEC.SecWheelNegativeElectricEnergy,20);
//                        sec = sec + 20;

                        SEC.SecWheelNegativeElectricEnergyzong.clear();
                        memcpySerialData(sec,SEC.SecWheelNegativeElectricEnergyzong,4);
                        sec = sec + 4;

                        SEC.SecWheelNegativeElectricEnergyjian.clear();
                        memcpySerialData(sec,SEC.SecWheelNegativeElectricEnergyjian,4);
                        sec = sec + 4;

                        SEC.SecWheelNegativeElectricEnergyfeng.clear();
                        memcpySerialData(sec,SEC.SecWheelNegativeElectricEnergyfeng,4);
                        sec = sec + 4;

                        SEC.SecWheelNegativeElectricEnergyping.clear();
                        memcpySerialData(sec,SEC.SecWheelNegativeElectricEnergyping,4);
                        sec = sec + 4;

                        SEC.SecWheelNegativeElectricEnergygu.clear();
                        memcpySerialData(sec,SEC.SecWheelNegativeElectricEnergygu,4);
                        sec = sec + 4;
                        /***************************************************************/
                        SEC.SecWheelActivePowerCom.clear();
                        memcpySerialData(sec,SEC.SecWheelActivePowerCom,20);
                        sec = sec + 20;
                        /***************************************************************/
                        SEC.SecFirstReactivePower.clear();
                        memcpySerialData(sec,SEC.SecFirstReactivePower,16);
                        sec = sec + 20;
                        /***************************************************************/
                        SEC.SecSecondReactivePower.clear();
                        memcpySerialData(sec,SEC.SecSecondReactivePower,16);
                        sec = sec + 20;
                        /***************************************************************/
                        SEC.SecThirdReactivePower.clear();
                        memcpySerialData(sec,SEC.SecThirdReactivePower,16);
                        sec = sec + 20;
                        /***************************************************************/
                        SEC.SecFourthReactivePower.clear();
                        memcpySerialData(sec,SEC.SecFourthReactivePower,16);
                        sec = sec + 20;
                        /***************************************************************/
                        SEC.SecCT.clear();
                        memcpySerialData(sec,SEC.SecCT,2);
                        sec = sec + 2;
                        /***************************************************************/
                        SEC.SecCT_U.clear();
                        memcpySerialData(sec,SEC.SecCT_U,4);
                        sec = sec + 4;
                        /***************************************************************/
//                        SEC.WheelLineV.clear();
//                        memcpySerialData(sec,SEC.WheelLineV,12);
//                        sec = sec + 12;

                        SEC.WheelLineVAB.clear();
                        memcpySerialData(sec,SEC.WheelLineVAB,4);
                        sec = sec + 4;

                        SEC.WheelLineVBC.clear();
                        memcpySerialData(sec,SEC.WheelLineVBC,4);
                        sec = sec + 4;

                        SEC.WheelLineVCA.clear();
                        memcpySerialData(sec,SEC.WheelLineVCA,4);
                        sec = sec + 4;
                        /***************************************************************/
//                        SEC.UnstableVandI.clear();
//                        memcpySerialData(sec,SEC.UnstableVandI,8);
//                        sec = sec + 8;

                        SEC.UnstableVandIV.clear();
                        memcpySerialData(sec,SEC.UnstableVandIV,4);
                        sec = sec + 4;

                        SEC.UnstableVandIA.clear();
                        memcpySerialData(sec,SEC.UnstableVandIA,4);
                        sec = sec + 4;
                        /***************************************************************/
//                        SEC.WheelVam.clear();
//                        memcpySerialData(sec,SEC.WheelVam,12);
//                        sec = sec + 12;

                        SEC.WheelVamA.clear();
                        memcpySerialData(sec,SEC.WheelVamA,4);
                        sec = sec + 4;

                        SEC.WheelVamB.clear();
                        memcpySerialData(sec,SEC.WheelVamB,4);
                        sec = sec + 4;

                        SEC.WheelVamC.clear();
                        memcpySerialData(sec,SEC.WheelVamC,4);
                        sec = sec + 4;
                        /***************************************************************/
//                        SEC.PhaseAngle.clear();
//                        memcpySerialData(sec,SEC.PhaseAngle,12);
//                        sec = sec + 12;
                        SEC.PhaseAngleAA.clear();
                        memcpySerialData(sec,SEC.PhaseAngleAA,2);
                        sec = sec + 2;

                        SEC.PhaseAngleAB.clear();
                        memcpySerialData(sec,SEC.PhaseAngleAB,2);
                        sec = sec + 2;

                        SEC.PhaseAngleAC.clear();
                        memcpySerialData(sec,SEC.PhaseAngleAC,2);
                        sec = sec + 2;

                        SEC.PhaseAngleVA.clear();
                        memcpySerialData(sec,SEC.PhaseAngleVA,2);
                        sec = sec + 2;

                        SEC.PhaseAngleVB.clear();
                        memcpySerialData(sec,SEC.PhaseAngleVB,2);
                        sec = sec + 2;

                        SEC.PhaseAngleVC.clear();
                        memcpySerialData(sec,SEC.PhaseAngleVC,2);
                        sec = sec + 2;
                        /***************************************************************/
//                        SEC.AberrationRate.clear();
//                        memcpySerialData(sec,SEC.AberrationRate,12);
//                        sec = sec + 12;

                        SEC.AberrationRateVA.clear();
                        memcpySerialData(sec,SEC.AberrationRateVA,2);
                        sec = sec + 2;

                        SEC.AberrationRateVB.clear();
                        memcpySerialData(sec,SEC.AberrationRateVB,2);
                        sec = sec + 2;

                        SEC.AberrationRateVC.clear();
                        memcpySerialData(sec,SEC.AberrationRateVC,2);
                        sec = sec + 2;

                        SEC.AberrationRateAA.clear();
                        memcpySerialData(sec,SEC.AberrationRateAA,2);
                        sec = sec + 2;

                        SEC.AberrationRateAB.clear();
                        memcpySerialData(sec,SEC.AberrationRateAB,2);
                        sec = sec + 2;

                        SEC.AberrationRateAC.clear();
                        memcpySerialData(sec,SEC.AberrationRateAC,2);
                        sec = sec + 2;
                        /***************************************************************/
//                        SEC.HarmonicKFactor.clear();
//                        memcpySerialData(sec,SEC.HarmonicKFactor,24);
//                        sec = sec + 24;

                        SEC.HarmonicKFactorVA.clear();
                        memcpySerialData(sec,SEC.HarmonicKFactorVA,4);
                        sec = sec + 4;

                        SEC.HarmonicKFactorVB.clear();
                        memcpySerialData(sec,SEC.HarmonicKFactorVB,4);
                        sec = sec + 4;

                        SEC.HarmonicKFactorVC.clear();
                        memcpySerialData(sec,SEC.HarmonicKFactorVC,4);
                        sec = sec + 4;

                        SEC.HarmonicKFactorAA.clear();
                        memcpySerialData(sec,SEC.HarmonicKFactorAA,4);
                        sec = sec + 4;

                        SEC.HarmonicKFactorAB.clear();
                        memcpySerialData(sec,SEC.HarmonicKFactorAB,4);
                        sec = sec + 4;

                        SEC.HarmonicKFactorAC.clear();
                        memcpySerialData(sec,SEC.HarmonicKFactorAC,4);
                        sec = sec + 4;
                        /***************************************************************/
//                        SEC.HarmonicWavePeakFactor.clear();
//                        memcpySerialData(sec,SEC.HarmonicWavePeakFactor,24);
//                        sec = sec + 24;

                        SEC.HarmonicWavePeakFactorVA.clear();
                        memcpySerialData(sec,SEC.HarmonicWavePeakFactorVA,4);
                        sec = sec + 4;

                        SEC.HarmonicWavePeakFactorVB.clear();
                        memcpySerialData(sec,SEC.HarmonicWavePeakFactorVB,4);
                        sec = sec + 4;

                        SEC.HarmonicWavePeakFactorVC.clear();
                        memcpySerialData(sec,SEC.HarmonicWavePeakFactorVC,4);
                        sec = sec + 4;

                        SEC.HarmonicWavePeakFactorAA.clear();
                        memcpySerialData(sec,SEC.HarmonicWavePeakFactorAA,4);
                        sec = sec + 4;

                        SEC.HarmonicWavePeakFactorAB.clear();
                        memcpySerialData(sec,SEC.HarmonicWavePeakFactorAB,4);
                        sec = sec + 4;

                        SEC.HarmonicWavePeakFactorAC.clear();
                        memcpySerialData(sec,SEC.HarmonicWavePeakFactorAC,4);
                        sec = sec + 4;
                        /***************************************************************/
//                        SEC.PositiveReactivePower.clear();
//                        memcpySerialData(sec,SEC.PositiveReactivePower,40);
//                        sec = sec + 40;

                        SEC.PositiveReactivePowerzong.clear();
                        memcpySerialData(sec,SEC.PositiveReactivePowerzong,8);
                        sec = sec + 8;

                        SEC.PositiveReactivePowerjian.clear();
                        memcpySerialData(sec,SEC.PositiveReactivePowerjian,8);
                        sec = sec + 8;

                        SEC.PositiveReactivePowerfeng.clear();
                        memcpySerialData(sec,SEC.PositiveReactivePowerfeng,8);
                        sec = sec + 8;

                        SEC.PositiveReactivePowerping.clear();
                        memcpySerialData(sec,SEC.PositiveReactivePowerping,8);
                        sec = sec + 8;

                        SEC.PositiveReactivePowergu.clear();
                        memcpySerialData(sec,SEC.PositiveReactivePowergu,8);
                        sec = sec + 8;
                        /***************************************************************/
//                        SEC.ReverseReactivePower.clear();
//                       memcpySerialData(sec,SEC.ReverseReactivePower,40);
//                        sec = sec + 40;

                        SEC.ReverseReactivePowerzong.clear();
                        memcpySerialData(sec,SEC.ReverseReactivePowerzong,8);
                        sec = sec + 8;

                        SEC.ReverseReactivePowerjian.clear();
                        memcpySerialData(sec,SEC.ReverseReactivePowerjian,8);
                        sec = sec + 8;

                        SEC.ReverseReactivePowerfeng.clear();
                        memcpySerialData(sec,SEC.ReverseReactivePowerfeng,8);
                        sec = sec + 8;

                        SEC.ReverseReactivePowerping.clear();
                        memcpySerialData(sec,SEC.ReverseReactivePowerping,8);
                        sec = sec + 8;

                        SEC.ReverseReactivePowergu.clear();
                        memcpySerialData(sec,SEC.ReverseReactivePowergu,8);
                        sec = sec + 8;
                        /***************************************************************/
//                        SEC.Demand.clear();
//                        memcpySerialData(sec,SEC.Demand,8);
//                        sec = sec + 8;

                        SEC.Demandhave.clear();
                        memcpySerialData(sec,SEC.Demandhave,4);
                        qDebug()<<"SEC.Demandhave"<<SEC.Demandhave.toHex();
                        sec = sec + 4;

                        SEC.Demandwu.clear();
                        memcpySerialData(sec,SEC.Demandwu,4);
                        qDebug()<<"SEC.Demandwu"<<SEC.Demandwu.toHex();
                        sec = sec + 4;
                        /***************************************************************/
                        sec = NULL;
                        break;
                    case 0x05:
                        DIGCOMPARE.McuState = DIG.McuState;
                        DIG.McuState.clear();
//                        SEC.McuState.clear();
//                        memcpySerialData(temp,SEC.McuState,1);
                        memcpySerialData(temp,DIG.McuState,1);
                        if(DIGCOMPARE.McuState != DIG.McuState)
                        {
                            DIG.McuState_flag = true;
                        }else{
                            DIG.McuState_flag = false;
                        }

                        temp = NULL;

                        data_war.clear();
                        data_war[0] = data[5];
                        waring_data = data_war.toHex().toInt(&ok,16);

                        emit sigSelfTestMonitor(waring_data);

                        break;
                    case 0x07:
                        temp = temp + 1;
                        SEC.Led.clear();
                        memcpySerialData(temp,SEC.Led,1);
                        //share1.write(SH.qLed,"Led",SEC.Led);

                        led_data=SEC.Led.toHex().toInt(&ok,16);
                        if(led_data%32/16)//
                            led_flage5=0x10;
                        if(led_data%16/8)//
                            led_flage1=0x01;
                        if(led_data%8/4)//
                            led_flage2=0x02;
                        if(led_data%4/2)//
                            led_flage4=0x08;
                        if(led_data%2/1)//
                            led_flage3=0x04;
                        led_flage=led_flage1|led_flage2|led_flage3|led_flage4|led_flage5;
                        emit sigLedControl(led_flage);
//                        led_set="i2cset -y 1 0x34 "+QString::number(led_flage);
//                        qDebug()<<"led_set :"<<led_set;
//                        std::system(led_set.toLatin1());
                        temp = NULL;
                        break;
                    case 0x11:
                        //ARM1->testSerialPort(2);
                        UART1_ini = 1;
                        //Delay_MSec(1000);                                                
                        ARM1->Set_Init();
                        ARM1->setEnable(UART1_Enable);
                        qDebug()<<"111111111111222222222222222222222222";
                        break;

                    case 0x15:
                        DIGCOMPARE.McuAlarmState = DIG.McuAlarmState;
                        DIG.McuAlarmState.clear();
                        memcpySerialData(temp,DIG.McuAlarmState,1);
                        if(DIGCOMPARE.McuAlarmState != DIG.McuAlarmState)
                        {
                            DIG.McuAlarmState_flag = true;
                        }else {
                            DIG.McuAlarmState_flag = false;
                        }
                        temp = NULL;
                        data_war.clear();
                        data_war[0] = data[6];
                        waring_data = data_war.toHex().toInt(&ok,16);

                        emit sigAlarmMonitor(waring_data);

                        break;

                    case 0x17:
                        SEC.Rtc_time.clear();
                        Sys_time.clear();
                        memcpySerialData(rtctime,SEC.Rtc_time,7);
                        year=SEC.Rtc_time.toHex().mid(0,4).toInt(&ok,16);
                        month=SEC.Rtc_time.toHex().mid(4,2).toInt(&ok,16);
                        day=SEC.Rtc_time.toHex().mid(6,2).toInt(&ok,16);
                        hour=SEC.Rtc_time.toHex().mid(8,2).toInt(&ok,16);
                        minute=SEC.Rtc_time.toHex().mid(10,2).toInt(&ok,16);
                        seconds=SEC.Rtc_time.toHex().mid(12,2).toInt(&ok,16);
                        ass_year = QString::number(year);
                        if(month<10)
                        {
                            ass_month = "0"+QString::number(month);
                        }
                        else
                        {
                            ass_month =QString::number(month);
                        }
                        if(day<10)
                        {
                            ass_day = "0"+QString::number(day);
                        }
                        else
                        {
                            ass_day =QString::number(day);
                        }
                        if(hour<10)
                        {
                            ass_hour = "0"+QString::number(hour);
                        }
                        else
                        {
                            ass_hour =QString::number(hour);
                        }
                        if(minute<10)
                        {
                            ass_minute = "0"+QString::number(minute);
                        }
                        else
                        {
                            ass_minute =QString::number(minute);
                        }
                        if(seconds<10)
                        {
                            ass_seconds = "0"+QString::number(seconds);
                        }
                        else
                        {
                            ass_seconds =QString::number(seconds);
                        }
                        Sys_time = ass_year+"-"+ass_month+"-"+ass_day+" "+ass_hour+":"+ass_minute+":"+ass_seconds;
                        ba = Sys_time.toLatin1();
                        SetSystemTime(ba.data());
                        //Sys_time = "date -s \""+Sys_time+"\"";
                        //std::system(Sys_time.toLatin1());
                        //qDebug()<<"set_RTC="<<Sys_time.toLatin1()<<endl;
                        rtctime = NULL;
                        break;

                    case 0x18:
                        temp =temp+1;
                        wireless_checkcount = data.at(6);
                        SEC.WirelessT_CheckAccount.clear();
                        //SEC.wirelessTData_State.clear();
                        DIG.wirelessTData_State.clear();
                        memcpySerialData(temp,SEC.WirelessT_CheckAccount,1);

                        temp = temp+1;
                        DIGCOMPARE.wirelessTData_State = DIG.wirelessTData_State;
                        memcpySerialData(temp,DIG.wirelessTData_State,wireless_checkcount);
                        if(DIGCOMPARE.wirelessTData_State != DIG.wirelessTData_State)
                        {
                            DIG.wirelessTData_State_flag = true;
                        }else{
                            DIG.wirelessTData_State_flag = false;
                        }
                        //memcpySerialData(temp,SEC.wirelessTData_State,wireless_checkcount);

                        temp = NULL;
                        break;

                    case 0x19:
                        temp = temp + 1;
                        DIGCOMPARE.McuConnectState = DIG.McuConnectState;
                        DIG.McuConnectState.clear();
                        memcpySerialData(temp,DIG.McuConnectState,1);
                        if(DIGCOMPARE.McuConnectState != DIG.McuConnectState)
                        {
                            DIG.McuConnectState_flag = true;
                        }else{
                            DIG.McuConnectState_flag = false;
                        }
                        temp = NULL;
                        ZiJian_state.clear();
                        ZiJian_state[0] = data[6];
                        zijian = ZiJian_state.toHex().toInt(&ok,16);
                        if((zijian%128/64)==0){
                            QFile file("/home/data/V_0_state.txt");//文件路径
                            if(file.open(QFile::WriteOnly))
                            {
                                QTextStream set_wifi_connect(&file);
                                set_wifi_connect<<"1"<<endl;
                            }
                            file.close();
                        }
                        else{
                            QFile file("/home/data/V_0_state.txt");//文件路径
                            if(file.open(QFile::WriteOnly))
                            {

                                QTextStream set_wifi_connect(&file);
                                set_wifi_connect<<"0"<<endl;
                            }
                            file.close();
                        }
                        if(zijian%64/32){
                            QFile file("/home/data/V_C_state.txt");//文件路径
                            if(file.open(QFile::WriteOnly))
                            {
                                QTextStream set_wifi_connect(&file);
                                set_wifi_connect<<"1"<<endl;
                            }
                            file.close();
                        }
                        else{
                            QFile file("/home/data/V_C_state.txt");//文件路径
                            if(file.open(QFile::WriteOnly))
                            {

                                QTextStream set_wifi_connect(&file);
                                set_wifi_connect<<"0"<<endl;
                            }
                            file.close();
                        }
                        if(zijian%32/16){
                            QFile file("/home/data/V_B_state.txt");//文件路径
                            if(file.open(QFile::WriteOnly))
                            {
                                QTextStream set_wifi_connect(&file);
                                set_wifi_connect<<"1"<<endl;
                                file.close();
                            }

                        }
                        else{
                            QFile file("/home/data/V_B_state.txt");//文件路径
                            if(file.open(QFile::WriteOnly))
                            {

                                QTextStream set_wifi_connect(&file);
                                set_wifi_connect<<"0"<<endl;
                                file.close();
                            }

                        }
                        if(zijian%16/8){
                            QFile file("/home/data/V_A_state.txt");//文件路径
                            if(file.open(QFile::WriteOnly))
                            {
                                QTextStream set_wifi_connect(&file);
                                set_wifi_connect<<"1"<<endl;
                            }
                            file.close();
                        }
                        else{
                            QFile file("/home/data/V_A_state.txt");//文件路径
                            if(file.open(QFile::WriteOnly))
                            {

                                QTextStream set_wifi_connect(&file);
                                set_wifi_connect<<"0"<<endl;
                            }
                            file.close();
                        }
                        if(zijian%8/4){
                            QFile file("/home/data/I_state.txt");//文件路径
                            if(file.open(QFile::WriteOnly))
                            {
                                QTextStream set_wifi_connect(&file);
                                set_wifi_connect<<"1"<<endl;
                            }
                            file.close();
                        }
                        else{
                            QFile file("/home/data/I_state.txt");//文件路径
                            if(file.open(QFile::WriteOnly))
                            {
                                QTextStream set_wifi_connect(&file);
                                set_wifi_connect<<"0"<<endl;
                            }
                            file.close();
                        }
                        if(zijian%4/2){
                            QFile file("/home/data/JF_state.txt");//文件路径
                            if(file.open(QFile::WriteOnly))
                            {
                                QTextStream set_wifi_connect(&file);
                                set_wifi_connect<<"1"<<endl;
                            }
                            file.close();
                        }
                        else{
                            QFile file("/home/data/JF_state.txt");//文件路径
                            if(file.open(QFile::WriteOnly))
                            {
                                QTextStream set_wifi_connect(&file);
                                set_wifi_connect<<"0"<<endl;
                            }
                            file.close();
                        }
                        if(zijian%2/1){
                            QFile file("/home/data/CW_state.txt");//文件路径
                            if(file.open(QFile::WriteOnly))
                            {
                                QTextStream set_wifi_connect(&file);
                                set_wifi_connect<<"1"<<endl;
                            }
                            file.close();
                        }
                        else{
                            QFile file("/home/data/CW_state.txt");//文件路径
                            if(file.open(QFile::WriteOnly))
                            {
                                QTextStream set_wifi_connect(&file);
                                set_wifi_connect<<"0"<<endl;
                            }
                            file.close();
                        }
                        break;

                        //热像仪温度获取指令  23.8.16
                    case 0x31:
                        bool  rxy1_flag = true;
                        bool  rxy2_flag = true;
                        //热像仪报警状态部分
                        QFileInfo file_alarm6_1("/home/data/Pop_rxy1.txt");
                        QFileInfo file_alarm6_2("/home/data/Pop_rxy2.txt");

                        if(file_alarm6_2.exists())
                        {
                            rxy2_flag = false;
                        }

                        if(file_alarm6_1.exists()) //热像仪掉线
                        {
                            rxy1_flag = false;
                        }

                        //热像仪连接状态部分
                        QFile file_readr2("/home/data/connect_rxy2.txt");
                        if(file_readr2.exists()){
                            if(file_readr2.open(QIODevice::ReadOnly | QIODevice::Text))
                            {
                                QTextStream in(&file_readr2);//创建一个对象让它去读文件
                                if(in.readLine()=="1")
                                {

                                }
                                else
                                {
                                    rxy2_flag = false;
                                }
                            }
                            file_readr2.close();
                        }else {
                            QFile file_write_rxy2("/home/data/connect_rxy2.txt");//文件路径
                            if(file_write_rxy2.open(QFile::WriteOnly))
                            {
                                QTextStream setWIFI_data(&file_write_rxy2);
                                setWIFI_data<<"0"<<endl;
                            }
                            file_write_rxy2.close();
                            rxy2_flag = false;
                        }

                        QFile file_readr("/home/data/connect_rxy1.txt");
                        if(file_readr.exists()){
                            if(file_readr.open(QIODevice::ReadOnly | QIODevice::Text))
                            {
                                QTextStream in(&file_readr);//创建一个对象让它去读文件
                                if(in.readLine()=="1")    //代表用户需求开启热像仪1
                                {

                                }
                                else
                                {
                                    rxy1_flag = false;
                                }
                            }
                            file_readr.close();
                            }else{
                            QFile file_write_rxy1("/home/data/connect_rxy1.txt");//文件路径
                            if(file_write_rxy1.open(QFile::WriteOnly))
                            {
                                QTextStream setWIFI_data(&file_write_rxy1);
                                setWIFI_data<<"0"<<endl;

                            }
                            file_write_rxy1.close();
                            rxy1_flag = false;
                            }

                        temperature_order = data.at(6);

                        if(temperature_order == 1 || temperature_order == 3)
                        {
                            if(rxy1_flag)
                            {
                                QFile file_write1("/home/data/rxy_temperature_show1.txt");//文件路径
                                if(file_write1.open(QFile::WriteOnly))
                                {
                                    QTextStream data1(&file_write1);
                                }
                                file_write1.close();
                                QFileInfo file1("/home/data/Real-TimeTemperature.txt");
                                for(int i =0;i<10;i++)
                                {
                                    Delay_MSec_Temp(1000);

                                    if(file1.exists())
                                    {
                                        break;
                                    }
                                    else
                                    {
                                        QFile file_write1("/home/data/rxy_temperature_show1.txt");//文件路径

                                        if(file_write1.exists())
                                        {
                                        }
                                        else
                                        {
                                            if(file_write1.open(QFile::WriteOnly))
                                            {
                                                QTextStream data1(&file_write1);
                                            }
                                            file_write1.close();
                                        }
                                    }
                                }


                                //实时温度部分   数据正常读取，串口传输数据
                                QFile file_temperature("/home/data/Real-TimeTemperature.txt");
                                if(file_temperature.exists())
                                {
                                    if(file_temperature.open(QIODevice::ReadOnly))
                                    {
                                        QTextStream in(&file_temperature);
                                        QString maxTemperature = in.readLine();
                                        QString minTemperature = in.readLine();
                                        QString averageTemperature = in.readLine();
                                        qDebug() << maxTemperature << endl
                                                 <<minTemperature << endl
                                                <<averageTemperature;

                                        int max = maxTemperature.toFloat() * 100;
                                        int min = minTemperature.toFloat() * 100;
                                        int ave = averageTemperature.toFloat() * 100;
                                        ARM1->Realtime_temperature(max,min,ave);


                                    }
                                    file_temperature.close();
                                }

                                QFile file_temperature_temp("/home/data/Real-TimeTemperature.txt");//删除温度存储部分
                                file_temperature_temp.remove();
                            }
                            else
                            {
                                ARM1->rxy_state();
                            }
                        }
                        if(temperature_order == 2 || temperature_order == 3)
                            {
                            if(rxy2_flag)
                            {
                                QFile file_write2("/home/data/rxy_temperature_show2.txt");//文件路径
                                if(file_write2.open(QFile::WriteOnly))
                                {
                                    QTextStream data2(&file_write2);
                                }
                                file_write2.close();
                                QFileInfo file2("/home/data/Real-TimeTemperature1.txt");
                                for(int i =0;i<10;i++)
                                {
                                    Delay_MSec_Temp(1000);

                                    if(file2.exists())
                                    {
                                        break;
                                    }
                                    else
                                    {
                                        QFile file_write2("/home/data/rxy_temperature_show2.txt");//文件路径

                                        if(file_write2.exists())
                                        {
                                        }
                                        else
                                        {
                                            if(file_write2.open(QFile::WriteOnly))
                                            {
                                                QTextStream data2(&file_write2);
                                            }
                                            file_write2.close();
                                        }
                                    }
                                }


                                //实时温度部分   数据正常读取，串口传输数据
                                QFile file_temperature1("/home/data/Real-TimeTemperature1.txt");
                                if(file_temperature1.exists())
                                {
                                    if(file_temperature1.open(QIODevice::ReadOnly))
                                    {
                                        QTextStream in1(&file_temperature1);
                                        QString maxTemperature1 = in1.readLine();
                                        QString minTemperature1 = in1.readLine();
                                        QString averageTemperature1 = in1.readLine();
                                        qDebug() << maxTemperature1 << endl
                                                 <<minTemperature1 << endl
                                                <<averageTemperature1;

                                        int max1 = maxTemperature1.toFloat() * 100;
                                        int min1 = minTemperature1.toFloat() * 100;
                                        int ave1 = averageTemperature1.toFloat() * 100;
                                        ARM1->Realtime_temperature1(max1,min1,ave1);


                                    }
                                    file_temperature1.close();
                                }

                                QFile file_temperature_temp1("/home/data/Real-TimeTemperature1.txt");//删除温度存储部分
                                file_temperature_temp1.remove();
                            }
                            else
                            {
                                ARM1->rxy_state();
                            }
                        }

                       break;

                    }
                }
            }
        }
    }
    data = 0;
}

void UART1::UartFrameReset()
{
    FrameData.clear();
    FrameCount = 0;
    FrameLen = 0;
    qDebug() << "UartFrameReset";
}

void UART1::openserial(QString COM)
{
    QList<QSerialPortInfo> list3;
    list3=QSerialPortInfo::availablePorts();
    for(int i=0;i<list3.size();i++)
        foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
        {
            if(info.portName()==COM)
            {
                QSerialPortInfo SerialPortInfo= info;
                SerialPort1.setPort(info);
                setserial();
                break;
            }
        }
}

void UART1::uart_init(QString COM,uint8_t Enable)
{
    openserial(COM);
    //ARM1->testSerialPort(2);
    UART1_Enable = Enable;

#if 0
    //ARM1->testSerialPort(2);
    UART1_ini = 1;
    Delay_MSec(1000);
    ARM1->Set_Init();
    ARM1->setEnable(UART1_Enable);
#endif

}

void UART1::ShuJu_Read()
{
    if(SerialPort1.isOpen())
    {
        UART1_READ_DATA = SerialPort1.readAll();
    }
    else
    {
    }
}

void UART1:: ShuJu_Write(QByteArray data)
{
    SerialPort1.write(data);
}

void UART1::setserial()
{
    SerialPort1.open(QIODevice::ReadWrite);
    SerialPort1.setBaudRate(QSerialPort::Baud115200);
    SerialPort1.setDataBits(QSerialPort::Data8);
    SerialPort1.setParity(QSerialPort::NoParity);
    SerialPort1.setStopBits(QSerialPort::OneStop);
    SerialPort1.setFlowControl(QSerialPort::NoFlowControl);
    if(SerialPort1.isOpen())
        qDebug()<<"串口已经打开";
    connect(&SerialPort1,SIGNAL(readyRead()),this,SLOT(receverDataFromSerialPort()));
}

void UART1::serial_close()
{
    SerialPort1.close();
}

void UART1::ModBusCRC16(QByteArray data)
{
    CRC1 = 0;
    uint16_t len = data.size();
    uint16_t rc=0xffff;
    int i, j;
    uint8_t temp;
    for(i=0; i<len; i++)
    {
        temp=data.at(i);
        rc^=temp;
        for(j=0;j<8;j++){
            if(rc&0X0001){
                rc>>=1;
                rc^=0XA001;
            }
            else
                rc>>=1;
        }
    }
    CRC1 = rc;
    len = 0;rc = 0;i = 0;j = 0;temp = 0;
}

int UART1::SetSystemTime(char *dt)
{
    struct tm rtc_time;
        struct tm _tm;
        struct timeval tv;
        time_t timep;
        sscanf(dt, "%d-%d-%d %d:%d:%d", &rtc_time.tm_year,
            &rtc_time.tm_mon, &rtc_time.tm_mday,&rtc_time.tm_hour,
            &rtc_time.tm_min, &rtc_time.tm_sec);
        _tm.tm_sec = rtc_time.tm_sec;
        _tm.tm_min = rtc_time.tm_min;
        _tm.tm_hour = rtc_time.tm_hour;
        _tm.tm_mday = rtc_time.tm_mday;
        _tm.tm_mon = rtc_time.tm_mon - 1;
        _tm.tm_year = rtc_time.tm_year - 1900;

        timep = mktime(&_tm);
        tv.tv_sec = timep;
        tv.tv_usec = 0;
        if(settimeofday (&tv, (struct timezone *) 0) < 0)
        {
            printf("Set system datatime error!/n");
            return -1;
        }
        return 0;
}

void UART1::memcpySerialData(char *data,QByteArray &A,uint16_t len)
{
    if(A.isEmpty() != 0)
    {
        A = 0;
    }
     A = QByteArray(data,len);
}

void UART1::happenInfoError(QByteArray data){
    switch (data.at(2)) {
    case 0x20:
        printf("Concentrator set fail...\n");
        break;
    case 0x21:
        printf("this function unable to use, now...%d\n");
        break;
    case 0x22:
        printf("fan start T set fail...\n");
        break;
    case 0x23:
        printf("fan state set fail...\n");
        break;
    case 0x24:
        printf("fan timing set fail...\n");
        break;
    case 0x25:
        printf("buzzer state set fail...\n");
        break;
    }
}

bool UART1::UartFrameParseByte(uint8_t data)
{
    uint8_t data_type;
    switch(FrameCount){
    case 0:
        if (data == 0xAA)
        {
            FrameData.push_back(data);
            FrameCount++;
            //qDebug() << "get 0xAA";
        }
        break;

    case 1:
        if (data == 0x55)
        {
            FrameData.push_back(data);
            FrameCount++;
            //qDebug() << "get 0x55";
        }
        else
        {
            UartFrameReset();
        }
        break;

    case 2: //类型码
        data_type = data;
        if(data_type == 0x05)
        {
            FrameData.push_back(data);
            FrameCount = FrameCount + 2;
            //qDebug() << "get data_type 0x05";
            break;
        }else{
            FrameData.push_back(data);
            FrameCount++;
            break;
        }

    case 3: //子类型
        FrameData.push_back(data);
        FrameCount++;
        break;

    case 4:        
        //取帧长高8
        FrameLen = data<<8;
        FrameData.push_back(data);
        FrameCount++;
        //qDebug() << "get len h8"<<FrameLen;
        break;

    case 5:
        //取帧长低8
        FrameLen |= data;
        //qDebug() << "get len l8";
        //qDebug() << "len:" << FrameLen;

        if(FrameLen <= 0){
            UartFrameReset();
        }else{
            FrameData.push_back(data);
            FrameCount++;
        }
        break;

    case 6:
        if(FrameLen + 2 > 0){
            FrameData.push_back(data);
            FrameLen--;
        }

        if(FrameLen + 2 <= 0){
            qDebug() << "recv over";
            return true;
        }

    default:break;

    }

    return false;
}

void UART1::UartFrameParse(QByteArray & from)
{
    for(int i=0; i< from.size(); i++){
        if(UartFrameParseByte(from.at(i))){
            UartFrameProcess(FrameData);
            UartFrameReset();
        }
    }
}
void UART1::receverDataFromSerialPort(){
    //m_pPlaneAirline->blockSignals(true);
    QByteArray data1 = SerialPort1.readAll();
    UartFrameParse(data1);
}


