#include "ctlwidget.h"
#include "qcheckbox.h"
#include "xlsxdocument.h"
#include "combobox.h"
#include "thermalwidget.h"
#include <QVBoxLayout>
#include <QComboBox>
#include <QtWidgets/QWidget>
#include <QtWidgets/QFormLayout>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QStackedLayout>
#include <QPushButton>
#include <QLabel>
#include <QTextEdit>
#include <QLineEdit>
#include <QGroupBox>
#include <QFileDialog>
#include <QApplication>
#include <QMessageBox>
#include <QSerialPortInfo>
#include <QButtonGroup>
#include <QRadioButton>
#include <QIcon>


extern QStringList g_list_serialinfo;
QStringList slist_temper;
QStringList slist_dsc;

extern QVector<float> g_qvecf_time;
extern QVector<float> g_qvecf_cur_time;
extern QVector<double> g_qvecd_temper;
extern QVector<double> g_qvecd_dsc;
extern QVector<double> g_qvecd_riserate;
extern QVector<float> g_qvecf_cooltemper;

extern int g_send_flag;

extern double g_quality;
extern QString g_sname_string;

extern QuadraticFitResult g_result1, g_result2;

CtlWidget::CtlWidget(QWidget *parent) : QWidget(parent)
{
    //设置背景
    QPalette pal(this->palette());
    pal.setColor(QPalette::Window, QColor (30, 30, 30, 50));
    this->setAutoFillBackground(true);
    this->setPalette(pal);

    QHBoxLayout *layout = new QHBoxLayout(this);
    layout->setAlignment(Qt::AlignLeft|Qt::AlignTop);

    QHBoxLayout *ctlHLayout = new QHBoxLayout();

    // 创建按钮并设置图标
    QPushButton *iconButton = new QPushButton(this);
    iconButton->setIcon(QIcon(":/images/deal16.ico"));
    iconButton->setIconSize(QSize(16, 16)); // 设置图标尺寸
    iconButton->setFixedSize(25, 25); // 设置按钮固定大小
    iconButton->setToolTip("参数设置");
    iconButton->setStyleSheet(
        "QPushButton { border: none; background: transparent; }"
        "QPushButton:hover { background: #f0f0f0; }"
        "QPushButton:pressed { background: #707070; }"
        );
    QPushButton *setcoffButton = new QPushButton(this);
    setcoffButton->setIcon(QIcon(":/images/realtime16.ico"));
    setcoffButton->setIconSize(QSize(16, 16)); // 设置图标尺寸
    setcoffButton->setFixedSize(25, 25); // 设置按钮固定大小
    setcoffButton->setToolTip("下发拟合系数");
    setcoffButton->setStyleSheet(
        "QPushButton { border: none; background: transparent; }"
        "QPushButton:hover { background: #f0f0f0; }"
        "QPushButton:pressed { background: #707070; }"
        );
    QPushButton *readcoffButton = new QPushButton(this);
    readcoffButton->setIcon(QIcon(":/images/file16.ico"));
    readcoffButton->setIconSize(QSize(16, 16)); // 设置图标尺寸
    readcoffButton->setFixedSize(25, 25); // 设置按钮固定大小
    readcoffButton->setToolTip("读取拟合系数");
    readcoffButton->setStyleSheet(
        "QPushButton { border: none; background: transparent; }"
        "QPushButton:hover { background: #f0f0f0; }"
        "QPushButton:pressed { background: #707070; }"
        );

    QPushButton *setButton = new QPushButton(this);
    setButton->setIcon(QIcon(":/images/set16.ico"));
    setButton->setIconSize(QSize(16, 16)); // 设置图标尺寸
    setButton->setFixedSize(25, 25); // 设置按钮固定大小
    setButton->setStyleSheet(
        "QPushButton { border: none; background: transparent; }"
        "QPushButton:hover { background: #f0f0f0; }"
        "QPushButton:pressed { background: #707070; }"
        );
    QPushButton *tbButton = new QPushButton(this);
    tbButton->setIcon(QIcon(":/images/tubiao16.ico"));
    tbButton->setIconSize(QSize(16, 16)); // 设置图标尺寸
    tbButton->setFixedSize(25, 25); // 设置按钮固定大小
    tbButton->setStyleSheet(
        "QPushButton { border: none; background: transparent; }"
        "QPushButton:hover { background: #f0f0f0; }"
        "QPushButton:pressed { background: #707070; }"
        );

    QPushButton *tb1Button = new QPushButton(this);
    tb1Button->setIcon(QIcon(":/images/tb16.ico"));
    tb1Button->setIconSize(QSize(16, 16)); // 设置图标尺寸
    tb1Button->setFixedSize(25, 25); // 设置按钮固定大小
    tb1Button->setStyleSheet(
        "QPushButton { border: none; background: transparent; }"
        "QPushButton:hover { background: #f0f0f0; }"
        "QPushButton:pressed { background: #707070; }"
        );
    QPushButton *tb2Button = new QPushButton(this);
    tb2Button->setIcon(QIcon(":/images/tb0116.ico"));
    tb2Button->setIconSize(QSize(16, 16)); // 设置图标尺寸
    tb2Button->setFixedSize(25, 25); // 设置按钮固定大小
    tb2Button->setStyleSheet(
        "QPushButton { border: none; background: transparent; }"
        "QPushButton:hover { background: #f0f0f0; }"
        "QPushButton:pressed { background: #707070; }"
        );
    QPushButton *tb3Button = new QPushButton(this);
    tb3Button->setIcon(QIcon(":/images/find16.ico"));
    tb3Button->setIconSize(QSize(16, 16)); // 设置图标尺寸
    tb3Button->setFixedSize(25, 25); // 设置按钮固定大小
    tb3Button->setStyleSheet(
        "QPushButton { border: none; background: transparent; }"
        "QPushButton:hover { background: #f0f0f0; }"
        "QPushButton:pressed { background: #707070; }"
        );
    QPushButton *tb4Button = new QPushButton(this);
    tb4Button->setIcon(QIcon(":/images/find1.ico"));
    tb4Button->setIconSize(QSize(16, 16)); // 设置图标尺寸
    tb4Button->setFixedSize(200, 1); // 设置按钮固定大小
    tb4Button->setStyleSheet(
        "QPushButton { border: none; background: transparent; }"
        );

    ctlHLayout->addWidget(iconButton,0,Qt::AlignTop);
    ctlHLayout->addWidget(setcoffButton,0,Qt::AlignTop);
    ctlHLayout->addWidget(readcoffButton,0,Qt::AlignTop);
    ctlHLayout->addWidget(setButton,0,Qt::AlignTop);
    ctlHLayout->addWidget(tbButton,0,Qt::AlignTop);
    ctlHLayout->addWidget(tb1Button,0,Qt::AlignTop);
    ctlHLayout->addWidget(tb2Button,0,Qt::AlignTop);
    ctlHLayout->addWidget(tb3Button,0,Qt::AlignTop);
    ctlHLayout->addWidget(tb4Button,0,Qt::AlignTop);
    QGroupBox *btnSettings = new QGroupBox("");
    btnSettings->setStyleSheet("color:rgb(50, 50, 50);font:bold 10px");
    btnSettings->setLayout(ctlHLayout);
    // serialVLayout->addSpacing(2);
    layout->addWidget(btnSettings);
    ///layout->addLayout(ctlHLayout);

    // QVBoxLayout *rwindVLayout = new QVBoxLayout();
    // QWidget* tmpWidget =new QWidget(this);
    // tmpWidget->setGeometry(140, 30, 200, 200);
    // tmpWidget->show();
    // rwindVLayout->addWidget(tmpWidget);
    // layout->addLayout(rwindVLayout);
    /*QVBoxLayout *rwindVLayout = new QVBoxLayout();
    QLabel* recvlabel =new QLabel(this);
    recvlabel->setGeometry(152,35,70,20);
    recvlabel->setText("接收窗口:");
    m_recvEdit =new QTextEdit(this);
    m_recvEdit->setGeometry(210,35,300,70);
    rwindVLayout->addWidget(recvlabel);
    rwindVLayout->addWidget(m_recvEdit);
    layout->addLayout(rwindVLayout);

    QVBoxLayout *swindVLayout = new QVBoxLayout();
    QLabel* sendlabel =new QLabel(this);
    sendlabel->setGeometry(518,35,70,20);
    sendlabel->setText("发送窗口:");
    m_sendEdit =new QTextEdit(this);
    m_sendEdit->setGeometry(580,35,120,40);
    m_sendEdit->setText("A55A0683500001E871");
    QPushButton* sendbutton = new QPushButton(this);
    sendbutton->setGeometry(705,35,70,20);
    sendbutton->setText("发送数据");
    swindVLayout->addWidget(sendlabel);
    swindVLayout->addWidget(m_sendEdit);
    swindVLayout->addWidget(sendbutton);
    layout->addLayout(swindVLayout);*/

    QVBoxLayout *serialVLayout = new QVBoxLayout();
    m_comboBox1 = new ComboBox(this);
    m_comboBox1->addItem("COM1");
    m_comboBox1->addItem("COM2");
    m_comboBox1->addItem("COM3");
    QComboBox *comboBox2 = new QComboBox(this);
    comboBox2->addItem("115200");
    comboBox2->addItem(" ");
    m_openbutton =new QPushButton(this);
    m_openbutton->setGeometry(5,35,70,20);
    m_openbutton->setText("打开端口");
    QPushButton* closebutton =new QPushButton(this);
    closebutton->setText("关闭端口");
    closebutton->setGeometry(5,58,70,20);
    QPushButton* savebutton =new QPushButton(this);
    savebutton->setGeometry(75,35,70,20);
    savebutton->setText("保存数据");
    serialVLayout->addSpacing(2);
    serialVLayout->addWidget(m_comboBox1);
    serialVLayout->addWidget(comboBox2);
    serialVLayout->addWidget(m_openbutton);
    serialVLayout->addWidget(closebutton);
    serialVLayout->addWidget(savebutton);
    QGroupBox *serialSettings = new QGroupBox("");
    serialSettings->setStyleSheet("color:rgb(50, 50, 50);font:bold 10px");
    serialSettings->setLayout(serialVLayout);
    // serialVLayout->addSpacing(2);
    layout->addWidget(serialSettings);

    QVBoxLayout *fileVLayout = new QVBoxLayout();
    m_comboBox4 = new QComboBox(this);
    m_comboBox4->addItem("C6H12");
    m_comboBox4->addItem("Hg");
    m_comboBox4->addItem("C6H5COOH");
    m_comboBox4->addItem("In");
    m_comboBox4->addItem("Sn");
    m_comboBox4->addItem("Bi");
    m_comboBox4->addItem("KC1O3");
    m_comboBox4->addItem("Pb");
    m_comboBox4->addItem("KNO3");
    m_comboBox4->addItem("Zn");
    m_comboBox4->addItem("Ag2SO4");
    m_comboBox4->addItem("CsCl");
    m_comboBox4->addItem("SiO2");
    m_comboBox4->addItem("K2SO4");
    m_comboBox4->addItem("K2CrO4");
    m_comboBox4->addItem("BaCO3");
    m_comboBox4->addItem("Ag");
    m_comboBox4->addItem("Cu");

    QPushButton* openbutton =new QPushButton(this);
    openbutton->setText("打开文件");
    m_pushbutton =new QPushButton(this);
    m_pushbutton->setText("载入数据");
    m_pushbutton->setDisabled(true);
    m_plotbutton =new QPushButton(this);
    m_plotbutton->setText("重新绘制");
    m_clearbutton =new QPushButton(this);
    m_clearbutton->setText("清理曲线");
    fileVLayout->addWidget(m_comboBox4);
    fileVLayout->addWidget(openbutton);
    fileVLayout->addWidget(m_pushbutton);
    fileVLayout->addWidget(m_plotbutton);
    fileVLayout->addWidget(m_clearbutton);
    ///layout->addLayout(fileVLayout);
    QGroupBox *fileSettings = new QGroupBox("");
    fileSettings->setStyleSheet("color:rgb(50, 50, 50);font:bold 10px");
    fileSettings->setLayout(fileVLayout);
    layout->addWidget(fileSettings);


    QVBoxLayout *rstaVLayout = new QVBoxLayout();
    QLabel* runstatelabel =new QLabel(this);
    runstatelabel->setGeometry(855,30,50,20);
    runstatelabel->setText("      运行状态");
    m_runstateBox = new QComboBox(this);
    m_runstateBox->setGeometry(820,50,120,20);
    m_runstateBox->addItem("空闲状态");
    m_runstateBox->addItem("加热运行");
    m_runstateBox->addItem("恒温运行");
    m_runstateBox->addItem("冷却中");
    m_runstateBox->addItem("升温速率自整定");
    m_runstateBox->setCurrentIndex(0);
    m_runstatebutton = new QPushButton(this);
    m_runstatebutton->setGeometry(845, 75, 70, 20);
    m_runstatebutton->setText("读取状态");

    rstaVLayout->addStretch();
    rstaVLayout->addWidget(runstatelabel);
    rstaVLayout->addWidget(m_runstateBox);
    rstaVLayout->addWidget(m_runstatebutton);
    rstaVLayout->addStretch();
    ///layout->addLayout(rstaVLayout);
    QGroupBox *runSettings = new QGroupBox("");
    runSettings->setStyleSheet("color:rgb(50, 50, 50);font:bold 10px");
    runSettings->setLayout(rstaVLayout);
    layout->addWidget(runSettings);

    QVBoxLayout *rstateVLayout = new QVBoxLayout();
    m_checkBox1 = new QCheckBox(this);
    m_checkBox1->setGeometry(1000,35,70,20);
    m_checkBox1->setText("停止");
    m_checkBox2 = new QCheckBox(this);
    m_checkBox2->setGeometry(1000,55,70,20);
    m_checkBox2->setText("启动运行");
    m_checkBox3 = new QCheckBox(this);
    m_checkBox3->setGeometry(1000,75,150,20);
    m_checkBox3->setText("启动升温速率自整定运行");
    m_checkBox4 = new QCheckBox(this);
    m_checkBox4->setGeometry(1000,95,150,20);
    m_checkBox4->setText("停止升温速率自整定");
    QButtonGroup *group = new QButtonGroup(this);
    group->addButton(m_checkBox1,1);
    group->addButton(m_checkBox2,2);
    group->addButton(m_checkBox3,3);
    group->addButton(m_checkBox4,4);
    // m_rstatebutton = new QPushButton(this);
    // m_rstatebutton->setGeometry(1180, 75, 70, 20);
    // m_rstatebutton->setText("读取状态");
    m_execubutton = new QPushButton(this);
    m_execubutton->setGeometry(1180, 95, 70, 20);
    m_execubutton->setText("执行配置");
    rstateVLayout->addStretch();
    rstateVLayout->addWidget(m_checkBox1);
    rstateVLayout->addWidget(m_checkBox2);
    rstateVLayout->addWidget(m_checkBox3);
    rstateVLayout->addWidget(m_checkBox4);
    rstateVLayout->addWidget(m_execubutton);
    rstateVLayout->addStretch();
    ///layout->addLayout(rstateVLayout);
    QGroupBox *execSettings = new QGroupBox("");
    execSettings->setStyleSheet("color:rgb(50, 50, 50);font:bold 10px");
    execSettings->setLayout(rstateVLayout);
    layout->addWidget(execSettings);


    QVBoxLayout *labelVLayout = new QVBoxLayout();
    QLabel* prolabel =new QLabel(this);
    prolabel->setGeometry(1300,25,60,20);
    prolabel->setText("项目/阶段");
    QLabel* statelabel =new QLabel(this);
    statelabel->setGeometry(1300,25,60,20);
    statelabel->setText("启用状态：");
    QLabel* untillabel =new QLabel(this);
    untillabel->setGeometry(1300,45,60,20);
    untillabel->setText("截止温度：");
    QLabel* riserlabel =new QLabel(this);
    riserlabel->setGeometry(1300,65,60,20);
    riserlabel->setText("升温速率：");
    QLabel* timelabel =new QLabel(this);
    timelabel->setGeometry(1300,85,60,20);
    timelabel->setText("恒温时间：");
    QLabel* chooselabel =new QLabel(this);
    chooselabel->setGeometry(1300,105,60,20);
    chooselabel->setText("气体选择:");
    QLabel* rlabel =new QLabel(this);
    rlabel->setGeometry(1300,105,60,20);
    rlabel->setText("读命令：");
    QLabel* wlabel =new QLabel(this);
    wlabel->setGeometry(1300,105,60,20);
    wlabel->setText("写命令：");
    rstaVLayout->addStretch();
    labelVLayout->addWidget(prolabel);
    labelVLayout->addWidget(statelabel);
    labelVLayout->addWidget(untillabel);
    labelVLayout->addWidget(riserlabel);
    labelVLayout->addWidget(timelabel);
    labelVLayout->addWidget(chooselabel);
    labelVLayout->addWidget(rlabel);
    labelVLayout->addWidget(wlabel);
    QGroupBox *labelSettings = new QGroupBox("");
    labelSettings->setStyleSheet("color:rgb(50, 50, 50);font:bold 10px;border: 0px solid gray");
    labelSettings->setLayout(labelVLayout);
    layout->addWidget(labelSettings);
    ///layout->addLayout(labelVLayout);

    QVBoxLayout *stage1VLayout = new QVBoxLayout();
    QLabel* stage1label =new QLabel(this);
    stage1label->setGeometry(1380,5,50,20);
    stage1label->setText("阶段一");
    m_stage1Box = new QComboBox(this);
    m_stage1Box->setGeometry(1375,25,80,20);
    m_stage1Box->addItem("关闭");
    m_stage1Box->addItem("开启");
    m_stage1Box->setCurrentIndex(0);
    m_untilEdit = new QLineEdit(this);
    m_untilEdit->setGeometry(1375,45,80,20);
    m_untilEdit->setText("128.6");
    m_riseEdit = new QLineEdit(this);
    m_riseEdit->setGeometry(1375,65,80,20);
    m_riseEdit->setText("10.0");
    m_timeEdit = new QLineEdit(this);
    m_timeEdit->setGeometry(1375,85,80,20);
    m_timeEdit->setText("0");
    m_airchBox = new QComboBox(this);
    m_airchBox->setGeometry(1375,105,80,20);
    m_airchBox->addItem("NC(无)");
    m_airchBox->addItem("N2(氮气)");
    m_airchBox->addItem("O2(氧气)");
    m_airchBox->setCurrentIndex(1);
    m_readStageBtn = new QPushButton(this);
    m_readStageBtn->setGeometry(1455,65,50,20);
    m_readStageBtn->setText("读取");
    m_writeStageBtn = new QPushButton(this);
    m_writeStageBtn->setGeometry(1455,85,50,20);
    m_writeStageBtn->setText("写入");
    // m_saveStageBtn = new QPushButton(this);
    // m_saveStageBtn->setGeometry(1455,105,50,20);
    // m_saveStageBtn->setText("保存");
    stage1VLayout->addWidget(stage1label);
    stage1VLayout->addWidget(m_stage1Box);
    stage1VLayout->addWidget(m_untilEdit);
    stage1VLayout->addWidget(m_riseEdit);
    stage1VLayout->addWidget(m_timeEdit);
    stage1VLayout->addWidget(m_airchBox);
    stage1VLayout->addWidget(m_readStageBtn);
    stage1VLayout->addWidget(m_writeStageBtn);
    QGroupBox *stage1Settings = new QGroupBox("");
    stage1Settings->setStyleSheet("color:rgb(50, 50, 50);font:bold 10px");
    stage1Settings->setLayout(stage1VLayout);
    layout->addWidget(stage1Settings);
    ///layout->addLayout(stage1VLayout);

    QVBoxLayout *stage2VLayout = new QVBoxLayout();
    QLabel* stage2label =new QLabel(this);
    stage2label->setGeometry(1525,5,50,20);
    stage2label->setText("阶段二");
    m_stage2Box = new QComboBox(this);
    m_stage2Box->setGeometry(1520,25,80,20);
    m_stage2Box->addItem("关闭");
    m_stage2Box->addItem("开启");
    m_stage2Box->setCurrentIndex(0);
    m_stage2Box->setDisabled(true);
    m_until2Edit = new QLineEdit(this);
    m_until2Edit->setGeometry(1520,45,80,20);
    m_until2Edit->setText("128.6");
    m_rise2Edit = new QLineEdit(this);
    m_rise2Edit->setGeometry(1520,65,80,20);
    m_rise2Edit->setText("10.0");
    m_time2Edit = new QLineEdit(this);
    m_time2Edit->setGeometry(1520,85,80,20);
    m_time2Edit->setText("0");
    m_airch2Box = new QComboBox(this);
    m_airch2Box->setGeometry(1520,105,80,20);
    m_airch2Box->addItem("NC(无)");
    m_airch2Box->addItem("N2(氮气)");
    m_airch2Box->addItem("O2(氧气)");
    m_airch2Box->setCurrentIndex(0);
    m_readStage2Btn = new QPushButton(this);
    m_readStage2Btn->setGeometry(1600,65,50,20);
    m_readStage2Btn->setText("读取");
    m_writeStage2Btn = new QPushButton(this);
    m_writeStage2Btn->setGeometry(1600,85,50,20);
    m_writeStage2Btn->setText("写入");
    // m_saveStage2Btn = new QPushButton(this);
    // m_saveStage2Btn->setGeometry(1600,105,50,20);
    // m_saveStage2Btn->setText("保存");
    stage2VLayout->addWidget(stage2label);
    stage2VLayout->addWidget(m_stage2Box);
    stage2VLayout->addWidget(m_until2Edit);
    stage2VLayout->addWidget(m_rise2Edit);
    stage2VLayout->addWidget(m_time2Edit);
    stage2VLayout->addWidget(m_airch2Box);
    stage2VLayout->addWidget(m_readStage2Btn);
    stage2VLayout->addWidget(m_writeStage2Btn);
    QGroupBox *stage2Settings = new QGroupBox("");
    stage2Settings->setStyleSheet("color:rgb(50, 50, 50);font:bold 10px");
    stage2Settings->setLayout(stage2VLayout);
    layout->addWidget(stage2Settings);
    ///layout->addLayout(stage2VLayout);

    QVBoxLayout *stage3VLayout = new QVBoxLayout();
    QLabel* stage3label =new QLabel(this);
    stage3label->setGeometry(1670,5,50,20);
    stage3label->setText("阶段三");
    m_stage3Box = new QComboBox(this);
    m_stage3Box->setGeometry(1665,25,80,20);
    m_stage3Box->addItem("关闭");
    m_stage3Box->addItem("开启");
    m_stage3Box->setCurrentIndex(0);
    m_stage3Box->setDisabled(true);
    m_until3Edit = new QLineEdit(this);
    m_until3Edit->setGeometry(1665,45,80,20);
    m_until3Edit->setText("128.6");
    m_rise3Edit = new QLineEdit(this);
    m_rise3Edit->setGeometry(1665,65,80,20);
    m_rise3Edit->setText("10.0");
    m_time3Edit = new QLineEdit(this);
    m_time3Edit->setGeometry(1665,85,80,20);
    m_time3Edit->setText("0");
    m_airch3Box = new QComboBox(this);
    m_airch3Box->setGeometry(1665,105,80,20);
    m_airch3Box->addItem("NC(无)");
    m_airch3Box->addItem("N2(氮气)");
    m_airch3Box->addItem("O2(氧气)");
    m_airch3Box->setCurrentIndex(0);
    m_readStage3Btn = new QPushButton(this);
    m_readStage3Btn->setGeometry(1745,65,50,20);
    m_readStage3Btn->setText("读取");
    m_writeStage3Btn = new QPushButton(this);
    m_writeStage3Btn->setGeometry(1745,85,50,20);
    m_writeStage3Btn->setText("写入");
    // m_saveStage3Btn = new QPushButton(this);
    // m_saveStage3Btn->setGeometry(1745,105,50,20);
    // m_saveStage3Btn->setText("保存");
    stage3VLayout->addWidget(stage3label);
    stage3VLayout->addWidget(m_stage3Box);
    stage3VLayout->addWidget(m_until3Edit);
    stage3VLayout->addWidget(m_rise3Edit);
    stage3VLayout->addWidget(m_time3Edit);
    stage3VLayout->addWidget(m_airch3Box);
    stage3VLayout->addWidget(m_readStage3Btn);
    stage3VLayout->addWidget(m_writeStage3Btn);
    QGroupBox *stage3Settings = new QGroupBox("");
    stage3Settings->setStyleSheet("color:rgb(50, 50, 50);font:bold 10px");
    stage3Settings->setLayout(stage3VLayout);
    layout->addWidget(stage3Settings);
    ///layout->addLayout(stage3VLayout);

    QVBoxLayout *stage4VLayout = new QVBoxLayout();
    QLabel* stage4label =new QLabel(this);
    stage4label->setGeometry(1820,5,50,20);
    stage4label->setText("阶段四");
    m_stage4Box = new QComboBox(this);
    m_stage4Box->setGeometry(1815,25,80,20);
    m_stage4Box->addItem("关闭");
    m_stage4Box->addItem("开启");
    m_stage4Box->setCurrentIndex(0);
    m_stage4Box->setDisabled(true);
    m_until4Edit = new QLineEdit(this);
    m_until4Edit->setGeometry(1815,45,80,20);
    m_until4Edit->setText("128.6");
    m_rise4Edit = new QLineEdit(this);
    m_rise4Edit->setGeometry(1815,65,80,20);
    m_rise4Edit->setText("10.0");
    m_time4Edit = new QLineEdit(this);
    m_time4Edit->setGeometry(1815,85,80,20);
    m_time4Edit->setText("0");
    m_airch4Box = new QComboBox(this);
    m_airch4Box->setGeometry(1815,105,80,20);
    m_airch4Box->addItem("NC(无)");
    m_airch4Box->addItem("N2(氮气)");
    m_airch4Box->addItem("O2(氧气)");
    m_airch4Box->setCurrentIndex(0);
    m_readStage4Btn = new QPushButton(this);
    m_readStage4Btn->setGeometry(1895,65,50,20);
    m_readStage4Btn->setText("读取");
    m_writeStage4Btn = new QPushButton(this);
    m_writeStage4Btn->setGeometry(1895,85,50,20);
    m_writeStage4Btn->setText("写入");
    // m_saveStage4Btn = new QPushButton(this);
    // m_saveStage4Btn->setGeometry(1895,105,50,20);
    // m_saveStage4Btn->setText("保存");
    stage4VLayout->addWidget(stage4label);
    stage4VLayout->addWidget(m_stage4Box);
    stage4VLayout->addWidget(m_until4Edit);
    stage4VLayout->addWidget(m_rise4Edit);
    stage4VLayout->addWidget(m_time4Edit);
    stage4VLayout->addWidget(m_airch4Box);
    stage4VLayout->addWidget(m_readStage4Btn);
    stage4VLayout->addWidget(m_writeStage4Btn);
    QGroupBox *stage4Settings = new QGroupBox("");
    stage4Settings->setStyleSheet("color:rgb(50, 50, 50);font:bold 10px");
    stage4Settings->setLayout(stage4VLayout);
    layout->addWidget(stage4Settings);
    ///layout->addLayout(stage4VLayout);


    QVBoxLayout *stage5VLayout = new QVBoxLayout();
    QLabel* stage5label =new QLabel(this);
    stage5label->setGeometry(1970,5,50,20);
    stage5label->setText("阶段五");
    m_stage5Box = new QComboBox(this);
    m_stage5Box->setGeometry(1965,25,80,20);
    m_stage5Box->addItem("关闭");
    m_stage5Box->addItem("开启");
    m_stage5Box->setCurrentIndex(0);
    m_stage5Box->setDisabled(true);
    m_until5Edit = new QLineEdit(this);
    m_until5Edit->setGeometry(1965,45,80,20);
    m_until5Edit->setText("128.6");
    m_rise5Edit = new QLineEdit(this);
    m_rise5Edit->setGeometry(1965,65,80,20);
    m_rise5Edit->setText("10.0");
    m_time5Edit = new QLineEdit(this);
    m_time5Edit->setGeometry(1965,85,80,20);
    m_time5Edit->setText("0");
    m_airch5Box = new QComboBox(this);
    m_airch5Box->setGeometry(1965,105,80,20);
    m_airch5Box->addItem("NC(无)");
    m_airch5Box->addItem("N2(氮气)");
    m_airch5Box->addItem("O2(氧气)");
    m_airch5Box->setCurrentIndex(0);
    m_readStage5Btn = new QPushButton(this);
    m_readStage5Btn->setGeometry(2045,65,50,20);
    m_readStage5Btn->setText("读取");
    m_writeStage5Btn = new QPushButton(this);
    m_writeStage5Btn->setGeometry(2045,85,50,20);
    m_writeStage5Btn->setText("写入");
    // m_saveStage5Btn = new QPushButton(this);
    // m_saveStage5Btn->setGeometry(2045,105,50,20);
    // m_saveStage5Btn->setText("保存");
    stage5VLayout->addWidget(stage5label);
    stage5VLayout->addWidget(m_stage5Box);
    stage5VLayout->addWidget(m_until5Edit);
    stage5VLayout->addWidget(m_rise5Edit);
    stage5VLayout->addWidget(m_time5Edit);
    stage5VLayout->addWidget(m_airch5Box);
    stage5VLayout->addWidget(m_readStage5Btn);
    stage5VLayout->addWidget(m_writeStage5Btn);
    QGroupBox *stage5Settings = new QGroupBox("");
    stage5Settings->setStyleSheet("color:rgb(50, 50, 50);font:bold 10px");
    stage5Settings->setLayout(stage5VLayout);
    layout->addWidget(stage5Settings);
    ///layout->addLayout(stage5VLayout);

    QVBoxLayout *stage6VLayout = new QVBoxLayout();
    QLabel* stage6label =new QLabel(this);
    stage6label->setGeometry(2120,5,50,20);
    stage6label->setText("阶段六");
    m_stage6Box = new QComboBox(this);
    m_stage6Box->setGeometry(2115,25,80,20);
    m_stage6Box->addItem("关闭");
    m_stage6Box->addItem("开启");
    m_stage6Box->setCurrentIndex(0);
    m_stage6Box->setDisabled(true);
    m_until6Edit = new QLineEdit(this);
    m_until6Edit->setGeometry(2115,45,80,20);
    m_until6Edit->setText("128.6");
    m_rise6Edit = new QLineEdit(this);
    m_rise6Edit->setGeometry(2115,65,80,20);
    m_rise6Edit->setText("10.0");
    m_time6Edit = new QLineEdit(this);
    m_time6Edit->setGeometry(2115,85,80,20);
    m_time6Edit->setText("0");
    m_airch6Box = new QComboBox(this);
    m_airch6Box->setGeometry(2115,105,80,20);
    m_airch6Box->addItem("NC(无)");
    m_airch6Box->addItem("N2(氮气)");
    m_airch6Box->addItem("O2(氧气)");
    m_airch6Box->setCurrentIndex(0);
    m_readStage6Btn = new QPushButton(this);
    m_readStage6Btn->setGeometry(2195,65,50,20);
    m_readStage6Btn->setText("读取");
    m_writeStage6Btn = new QPushButton(this);
    m_writeStage6Btn->setGeometry(2195,85,50,20);
    m_writeStage6Btn->setText("写入");
    // m_saveStage6Btn = new QPushButton(this);
    // m_saveStage6Btn->setGeometry(2195,105,50,20);
    // m_saveStage6Btn->setText("保存");
    stage6VLayout->addWidget(stage6label);
    stage6VLayout->addWidget(m_stage6Box);
    stage6VLayout->addWidget(m_until6Edit);
    stage6VLayout->addWidget(m_rise6Edit);
    stage6VLayout->addWidget(m_time6Edit);
    stage6VLayout->addWidget(m_airch6Box);
    stage6VLayout->addWidget(m_readStage6Btn);
    stage6VLayout->addWidget(m_writeStage6Btn);
    QGroupBox *stage6Settings = new QGroupBox("");
    stage6Settings->setStyleSheet("color:rgb(50, 50, 50);font:bold 10px");
    stage6Settings->setLayout(stage6VLayout);
    layout->addWidget(stage6Settings);
    ///layout->addLayout(stage6VLayout);
    setLayout(layout);

    sampleWidget = new SampleWidget(this);
    sampleWidget->setWindowFlags(Qt::Window);
    sampleWidget->resize(200,320);
    sampleWidget->setWindowTitle(tr("样本参数设置"));
    sampleWidget->setWindowFlags(sampleWidget->windowFlags() &~ Qt::WindowMinMaxButtonsHint);
    sampleWidget->hide();

    m_comboBox1->clear();
    //通过QSerialPortInfo查找可用串口
    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        m_comboBox1->addItem(info.portName());
    }

    QString selectedText = m_comboBox1->currentText();

    connect(this, SIGNAL(show_read_sample_param(QString, double)), sampleWidget, SLOT(show_read_sample_param_slot(QString, double)));

    connect(iconButton, SIGNAL(clicked()), this, SLOT(show_sample_parameter_set_window()));
    connect(setcoffButton, SIGNAL(clicked()), this, SLOT(set_fit_coff_to_target()));
    connect(readcoffButton, SIGNAL(clicked()), this, SLOT(read_fit_coff_from_target()));

    connect(openbutton, SIGNAL(clicked()), this, SLOT(read_xlsx_data()));
    connect(this, SIGNAL(enable_load_button()), this, SLOT(enable_load_data()));


    m_serialWorker = new SerialWorker;
    m_serialThread = new QThread(this);


    connect(this, SIGNAL(refresh_com_name(QString)), m_serialWorker, SLOT(init_port(QString)));
    connect(m_openbutton, SIGNAL(clicked()), m_serialWorker, SLOT(open_serial_port()));
    connect(closebutton, SIGNAL(clicked()), m_serialWorker, SLOT(close_serial_port()));

    ///connect(t4pushbutton, SIGNAL(clicked()), this, SLOT(test_on_receive()), Qt::QueuedConnection);      //测试使用
    ///connect(sendbutton, SIGNAL(clicked()), this, SLOT(send_text_data()));
    connect(savebutton, SIGNAL(clicked()), this, SLOT(save_data_to_xlsx()));

    connect(m_execubutton, SIGNAL(clicked()), this, SLOT(execute_send_plan()));
    ///connect(m_rstatebutton, SIGNAL(clicked()), this, SLOT(read_run_mode_to_checkbox()));
    connect(m_runstatebutton, SIGNAL(clicked()), this, SLOT(read_run_state_to_checkbox()));

    connect(m_readStageBtn, SIGNAL(clicked()), this, SLOT(change_box_index_stage1()));
    connect(m_writeStageBtn, SIGNAL(clicked()), this, SLOT(write_box_edit_to_stage1()));
    ///connect(m_saveStageBtn, SIGNAL(clicked()), this, SLOT(change_box_index_stage()));
    connect(m_readStage2Btn, SIGNAL(clicked()), this, SLOT(change_box_index_stage2()));
    connect(m_writeStage2Btn, SIGNAL(clicked()), this, SLOT(write_box_edit_to_stage2()));
    ///connect(m_saveStageBtn, SIGNAL(clicked()), this, SLOT(change_box_index_stage()));
    connect(m_readStage3Btn, SIGNAL(clicked()), this, SLOT(change_box_index_stage3()));
    connect(m_writeStage3Btn, SIGNAL(clicked()), this, SLOT(write_box_edit_to_stage3()));
    connect(m_readStage4Btn, SIGNAL(clicked()), this, SLOT(change_box_index_stage4()));
    connect(m_writeStage4Btn, SIGNAL(clicked()), this, SLOT(write_box_edit_to_stage4()));
    connect(m_readStage5Btn, SIGNAL(clicked()), this, SLOT(change_box_index_stage5()));
    connect(m_writeStage5Btn, SIGNAL(clicked()), this, SLOT(write_box_edit_to_stage5()));
    connect(m_readStage6Btn, SIGNAL(clicked()), this, SLOT(change_box_index_stage6()));
    connect(m_writeStage6Btn, SIGNAL(clicked()), this, SLOT(write_box_edit_to_stage6()));

    connect(m_checkBox1, SIGNAL(stateChanged(int)), this, SLOT(change_checkbox1_state(int)));
    connect(m_checkBox2, SIGNAL(stateChanged(int)), this, SLOT(change_checkbox2_state(int)));
    connect(m_checkBox3, SIGNAL(stateChanged(int)), this, SLOT(change_checkbox3_state(int)));
    connect(m_checkBox4, SIGNAL(stateChanged(int)), this, SLOT(change_checkbox4_state(int)));

    connect(m_stage1Box, SIGNAL(activated(int)), this, SLOT(deal_mutex_box1_logic(int)));
    connect(m_stage2Box, SIGNAL(activated(int)), this, SLOT(deal_mutex_box2_logic(int)));
    connect(m_stage3Box, SIGNAL(activated(int)), this, SLOT(deal_mutex_box3_logic(int)));
    connect(m_stage4Box, SIGNAL(activated(int)), this, SLOT(deal_mutex_box4_logic(int)));
    connect(m_stage5Box, SIGNAL(activated(int)), this, SLOT(deal_mutex_box5_logic(int)));
    connect(m_stage6Box, SIGNAL(activated(int)), this, SLOT(deal_mutex_box6_logic(int)));

    connect(this, SIGNAL(change_test_start_flag()), m_serialWorker, SLOT(change_test_start_flag_slot()));
    connect(this, SIGNAL(sent_text_array(QByteArray)), m_serialWorker, SLOT(write_data(QByteArray)));
    connect(this, SIGNAL(change_send_flag_to_port()), m_serialWorker, SLOT(change_send_flag()));
    connect(m_serialWorker, SIGNAL(receive_data(QByteArray)), this, SLOT(on_receive(QByteArray)));

    connect(m_serialWorker, SIGNAL(show_success_msgbox()), this, SLOT(show_port_success_msgbox()));
    connect(m_serialWorker, SIGNAL(show_failed_msgbox()), this, SLOT(show_port_failed_msgbox()));

    connect(m_serialWorker, SIGNAL(write_target_success()), this, SLOT(show_write_success_msgbox()));
    connect(m_serialWorker, SIGNAL(write_target_failed()), this, SLOT(show_write_failed_msgbox()));
    connect(m_serialWorker, SIGNAL(serial_port_not_open()), this, SLOT(show_open_port_failed_msgbox()));
    connect(m_serialWorker, SIGNAL(close_serial_port_success()), this, SLOT(show_close_port_success_msgbox()));

    ///connect(m_serialWorker, SIGNAL(show_checkbox_state(unsigned char)), this, SLOT(set_mode_checkbox_slot(unsigned char)));
    connect(m_serialWorker, SIGNAL(show_run_state_box_state(unsigned char)), this, SLOT(set_run_state_combox_slot(unsigned char)));

    connect(m_serialWorker, SIGNAL(set_stage1_edit_box(unsigned char, float, float, uint16_t, unsigned char)),
            this, SLOT(set_stage1_edit_box_slot(unsigned char, float, float, uint16_t, unsigned char)));
    connect(m_serialWorker, SIGNAL(set_stage2_edit_box(unsigned char, float, float, uint16_t, unsigned char)),
            this, SLOT(set_stage2_edit_box_slot(unsigned char, float, float, uint16_t, unsigned char)));
    connect(m_serialWorker, SIGNAL(set_stage3_edit_box(unsigned char, float, float, uint16_t, unsigned char)),
            this, SLOT(set_stage3_edit_box_slot(unsigned char, float, float, uint16_t, unsigned char)));
    connect(m_serialWorker, SIGNAL(set_stage4_edit_box(unsigned char, float, float, uint16_t, unsigned char)),
            this, SLOT(set_stage4_edit_box_slot(unsigned char, float, float, uint16_t, unsigned char)));
    connect(m_serialWorker, SIGNAL(set_stage5_edit_box(unsigned char, float, float, uint16_t, unsigned char)),
            this, SLOT(set_stage5_edit_box_slot(unsigned char, float, float, uint16_t, unsigned char)));
    connect(m_serialWorker, SIGNAL(set_stage6_edit_box(unsigned char, float, float, uint16_t, unsigned char)),
            this, SLOT(set_stage6_edit_box_slot(unsigned char, float, float, uint16_t, unsigned char)));


    connect(m_serialThread, &QThread::finished, m_serialWorker, &QObject::deleteLater);
    m_serialWorker->moveToThread(m_serialThread);
    m_serialThread->start();

    emit refresh_com_name(selectedText);

    qDebug() << "main thread is:" << QThread::currentThreadId();
}

CtlWidget::~CtlWidget()
{
}


void CtlWidget::set_fit_coff_to_target()
{
    coff_a.f = g_result1.a;
    coff_b.f = g_result1.b;
    coff_c.f = g_result1.c;

    QByteArray sendArray;
    QByteArray crcArray;
    sendArray.resize(12);
    crcArray.resize(7);
    //数据总长度
    crcArray[0] = 0x82;
    crcArray[1] = 0xE5;
    crcArray[2] = 0x00;
    crcArray[3] = coff_a.x[0];
    crcArray[4] = coff_a.x[1];
    crcArray[5] = coff_a.x[2];
    crcArray[6] = coff_a.x[3];
    qDebug()<<"crcArray"<<crcArray;
    uint16_t tempcrc = ModbusCRC16(crcArray);

    sendArray[0] = 0xA5;
    sendArray[1] = 0x5A;
    sendArray[2] = 0x09;
    sendArray[3] = 0x82;
    sendArray[4] = 0xE5;
    sendArray[5] = 0x00;
    sendArray[6] = coff_a.x[0];
    sendArray[7] = coff_a.x[1];
    sendArray[8] = coff_a.x[2];
    sendArray[9] = coff_a.x[3];
    sendArray[10] = tempcrc;
    sendArray[11] = tempcrc>>8;

    ///qDebug() << QString("write_box_edit_to_stage1 crc %1").arg(tempcrc);
    g_send_flag = 1;
    emit sent_text_array(sendArray);

    sendArray.resize(12);
    crcArray.resize(7);
    //数据总长度
    crcArray[0] = 0x82;
    crcArray[1] = 0xE9;
    crcArray[2] = 0x00;
    crcArray[3] = coff_b.x[0];
    crcArray[4] = coff_b.x[1];
    crcArray[5] = coff_b.x[2];
    crcArray[6] = coff_b.x[3];
    qDebug()<<"crcArray"<<crcArray;
    tempcrc = ModbusCRC16(crcArray);

    sendArray[0] = 0xA5;
    sendArray[1] = 0x5A;
    sendArray[2] = 0x09;
    sendArray[3] = 0x82;
    sendArray[4] = 0xE9;
    sendArray[5] = 0x00;
    sendArray[6] = coff_b.x[0];
    sendArray[7] = coff_b.x[1];
    sendArray[8] = coff_b.x[2];
    sendArray[9] = coff_b.x[3];
    sendArray[10] = tempcrc;
    sendArray[11] = tempcrc>>8;

    ///qDebug() << QString("write_box_edit_to_stage1 crc %1").arg(tempcrc);
    g_send_flag = 1;
    emit sent_text_array(sendArray);

    sendArray.resize(12);
    crcArray.resize(7);
    //数据总长度
    crcArray[0] = 0x82;
    crcArray[1] = 0xED;
    crcArray[2] = 0x00;
    crcArray[3] = coff_c.x[0];
    crcArray[4] = coff_c.x[1];
    crcArray[5] = coff_c.x[2];
    crcArray[6] = coff_c.x[3];
    qDebug()<<"crcArray"<<crcArray;
    tempcrc = ModbusCRC16(crcArray);

    sendArray[0] = 0xA5;
    sendArray[1] = 0x5A;
    sendArray[2] = 0x09;
    sendArray[3] = 0x82;
    sendArray[4] = 0xED;
    sendArray[5] = 0x00;
    sendArray[6] = coff_c.x[0];
    sendArray[7] = coff_c.x[1];
    sendArray[8] = coff_c.x[2];
    sendArray[9] = coff_c.x[3];
    sendArray[10] = tempcrc;
    sendArray[11] = tempcrc>>8;

    ///qDebug() << QString("write_box_edit_to_stage1 crc %1").arg(tempcrc);
    g_send_flag = 1;
    emit sent_text_array(sendArray);
}


void CtlWidget::show_sample_parameter_set_window()
{
    sampleWidget->show();
}


void CtlWidget::set_run_state_combox_slot(unsigned char run_state)
{
    m_runstateBox->setCurrentIndex(run_state);
}

void CtlWidget::set_mode_checkbox_slot(unsigned char mode_state)
{
    if(0==mode_state) m_checkBox1->setChecked(true);
    if(1==mode_state) m_checkBox2->setChecked(true);
    if(2==mode_state) m_checkBox3->setChecked(true);
    if(4==mode_state) m_checkBox4->setChecked(true);

}

void CtlWidget::set_stage1_edit_box_slot(unsigned char state, float until, float riserate, uint16_t time, unsigned char airchoose)
{

    m_stage1Box->setCurrentIndex(state);
    m_untilEdit->setText(QString::number(until, 'f', 6));
    m_riseEdit->setText(QString::number(riserate, 'f', 6));
    m_timeEdit->setText(QString::number(time));
    m_airchBox->setCurrentIndex(airchoose);

    qDebug()<<"set_stage1_edit_box_slot!"<<"state"<<state<<"until"
             <<until<<"riserate"<<riserate<<"time"<<time<<"airchoose"<<airchoose<<Qt::endl;
}

void CtlWidget::set_stage2_edit_box_slot(unsigned char state, float until, float riserate, uint16_t time, unsigned char airchoose)
{

    m_stage2Box->setCurrentIndex(state);
    m_until2Edit->setText(QString::number(until, 'f', 6));
    m_rise2Edit->setText(QString::number(riserate, 'f', 6));
    m_time2Edit->setText(QString::number(time));
    m_airch2Box->setCurrentIndex(airchoose);

    qDebug()<<"set_stage2_edit_box_slot!"<<"state"<<state<<"until"
             <<until<<"riserate"<<riserate<<"time"<<time<<"airchoose"<<airchoose<<Qt::endl;
}

void CtlWidget::set_stage3_edit_box_slot(unsigned char state, float until, float riserate, uint16_t time, unsigned char airchoose)
{

    m_stage3Box->setCurrentIndex(state);
    m_until3Edit->setText(QString::number(until, 'f', 6));
    m_rise3Edit->setText(QString::number(riserate, 'f', 6));
    m_time3Edit->setText(QString::number(time));
    m_airch3Box->setCurrentIndex(airchoose);

    qDebug()<<"set_stage3_edit_box_slot!"<<"state"<<state<<"until"
             <<until<<"riserate"<<riserate<<"time"<<time<<"airchoose"<<airchoose<<Qt::endl;
}

void CtlWidget::set_stage4_edit_box_slot(unsigned char state, float until, float riserate, uint16_t time, unsigned char airchoose)
{

    m_stage4Box->setCurrentIndex(state);
    m_until4Edit->setText(QString::number(until, 'f', 6));
    m_rise4Edit->setText(QString::number(riserate, 'f', 6));
    m_time4Edit->setText(QString::number(time));
    m_airch4Box->setCurrentIndex(airchoose);

    qDebug()<<"set_stage4_edit_box_slot!"<<"state"<<state<<"until"
             <<until<<"riserate"<<riserate<<"time"<<time<<"airchoose"<<airchoose<<Qt::endl;
}

void CtlWidget::set_stage5_edit_box_slot(unsigned char state, float until, float riserate, uint16_t time, unsigned char airchoose)
{

    m_stage5Box->setCurrentIndex(state);
    m_until5Edit->setText(QString::number(until, 'f', 6));
    m_rise5Edit->setText(QString::number(riserate, 'f', 6));
    m_time5Edit->setText(QString::number(time));
    m_airch5Box->setCurrentIndex(airchoose);

    qDebug()<<"set_stage5_edit_box_slot!"<<"state"<<state<<"until"
             <<until<<"riserate"<<riserate<<"time"<<time<<"airchoose"<<airchoose<<Qt::endl;
}

void CtlWidget::set_stage6_edit_box_slot(unsigned char state, float until, float riserate, uint16_t time, unsigned char airchoose)
{

    m_stage6Box->setCurrentIndex(state);
    m_until6Edit->setText(QString::number(until, 'f', 6));
    m_rise6Edit->setText(QString::number(riserate, 'f', 6));
    m_time6Edit->setText(QString::number(time));
    m_airch6Box->setCurrentIndex(airchoose);

    qDebug()<<"set_stage6_edit_box_slot!"<<"state"<<state<<"until"
             <<until<<"riserate"<<riserate<<"time"<<time<<"airchoose"<<airchoose<<Qt::endl;
}


void CtlWidget::send_read_command(unsigned char addr, unsigned char len)
{
    QByteArray sendArray;
    QByteArray crcArray;
    sendArray.resize(9);
    crcArray.resize(4);
    //数据总长度
    crcArray[0] = 0x83;
    crcArray[1] = addr;
    crcArray[2] = 0x00;
    crcArray[3] = len;
    uint16_t tempcrc = ModbusCRC16(crcArray);

    sendArray[0] = 0xA5;
    sendArray[1] = 0x5A;
    sendArray[2] = 0x06;
    sendArray[3] = 0x83;
    sendArray[4] = addr;
    sendArray[5] = 0x00;
    sendArray[6] = len;
    sendArray[7] = tempcrc;
    sendArray[8] = tempcrc>>8;

    qDebug() << QString("deal_mutex_box1_logic crc %1").arg(tempcrc);
    g_send_flag = 1;
    emit sent_text_array(sendArray);
}

// void CtlWidget::send_write_command(unsigned char addr, unsigned char len)
// {

// }
void CtlWidget::read_fit_coff_from_target()
{
    send_read_command(0xE5, 0x0C);
}

void CtlWidget::change_box_index_stage1()
{
    send_read_command(0x50, 0x0C);
}

void CtlWidget::change_box_index_stage2()
{
    send_read_command(0x5C, 0x0C);
}

void CtlWidget::change_box_index_stage3()
{
    send_read_command(0x68, 0x0C);
}
void CtlWidget::change_box_index_stage4()
{
    send_read_command(0x74, 0x0C);
}

void CtlWidget::change_box_index_stage5()
{
    send_read_command(0x80, 0x0C);
}

void CtlWidget::change_box_index_stage6()
{
    send_read_command(0x8C, 0x0C);
}

void CtlWidget::write_box_edit_to_stage1()
{

    unsigned char  state = m_stage1Box->currentIndex();
    QString untilString = m_untilEdit->text();
    QString riseString = m_riseEdit->text();
    QString timeString = m_timeEdit->text();
    unsigned char airchoose = m_airchBox->currentIndex();

    stage1_untilw.f = untilString.toFloat();
    stage1_riseratew.f = riseString.toFloat();
    uint16_t timeint = timeString.toUShort();

    ///send_write_command(0x50, 0x0C);
    ///qDebug()<<"write_box_edit_to_stage1!"<<"stage1_untilw.f "<<stage1_untilw.f<<"stage1_riseratew.f"<<stage1_riseratew.f<<"timeint"<<timeint<<Qt::endl;

    QByteArray sendArray;
    QByteArray crcArray;
    sendArray.resize(20);
    crcArray.resize(15);
    //数据总长度
    crcArray[0] = 0x82;
    crcArray[1] = 0x50;
    crcArray[2] = 0x00;
    crcArray[3] = state;
    crcArray[4] = stage1_untilw.x[0];
    crcArray[5] = stage1_untilw.x[1];;
    crcArray[6] = stage1_untilw.x[2];;
    crcArray[7] = stage1_untilw.x[3];;
    crcArray[8] = stage1_riseratew.x[0];
    crcArray[9] = stage1_riseratew.x[1];
    crcArray[10] = stage1_riseratew.x[2];
    crcArray[11] = stage1_riseratew.x[3];
    crcArray[12] = timeint>>8;
    crcArray[13] = timeint;
    crcArray[14] = airchoose;
    qDebug()<<"crcArray"<<crcArray;
    uint16_t tempcrc = ModbusCRC16(crcArray);

    sendArray[0] = 0xA5;
    sendArray[1] = 0x5A;
    sendArray[2] = 0x11;
    sendArray[3] = 0x82;
    sendArray[4] = 0x50;
    sendArray[5] = 0x00;
    sendArray[6] = state;
    sendArray[7] = stage1_untilw.x[0];
    sendArray[8] = stage1_untilw.x[1];;
    sendArray[9] = stage1_untilw.x[2];;
    sendArray[10] = stage1_untilw.x[3];;
    sendArray[11] = stage1_riseratew.x[0];
    sendArray[12] = stage1_riseratew.x[1];
    sendArray[13] = stage1_riseratew.x[2];
    sendArray[14] = stage1_riseratew.x[3];
    sendArray[15] = timeint>>8;
    sendArray[16] = timeint;
    sendArray[17] = airchoose;
    sendArray[18] = tempcrc;
    sendArray[19] = tempcrc>>8;

    ///qDebug() << QString("write_box_edit_to_stage1 crc %1").arg(tempcrc);
    g_send_flag = 1;
    emit sent_text_array(sendArray);
}

void CtlWidget::write_box_edit_to_stage2()
{

    unsigned char  state = m_stage2Box->currentIndex();
    QString untilString = m_until2Edit->text();
    QString riseString = m_rise2Edit->text();
    QString timeString = m_time2Edit->text();
    unsigned char airchoose = m_airch2Box->currentIndex();

    stage2_untilw.f = untilString.toFloat();
    stage2_riseratew.f = riseString.toFloat();
    uint16_t timeint = timeString.toUShort();

    ///send_write_command(0x50, 0x0C);
    ///qDebug()<<"write_box_edit_to_stage1!"<<"stage1_untilw.f "<<stage1_untilw.f<<"stage1_riseratew.f"<<stage1_riseratew.f<<"timeint"<<timeint<<Qt::endl;

    QByteArray sendArray;
    QByteArray crcArray;
    sendArray.resize(20);
    crcArray.resize(15);
    //数据总长度
    crcArray[0] = 0x82;
    crcArray[1] = 0x5C;
    crcArray[2] = 0x00;
    crcArray[3] = state;
    crcArray[4] = stage2_untilw.x[0];
    crcArray[5] = stage2_untilw.x[1];;
    crcArray[6] = stage2_untilw.x[2];;
    crcArray[7] = stage2_untilw.x[3];;
    crcArray[8] = stage2_riseratew.x[0];
    crcArray[9] = stage2_riseratew.x[1];
    crcArray[10] = stage2_riseratew.x[2];
    crcArray[11] = stage2_riseratew.x[3];
    crcArray[12] = timeint>>8;
    crcArray[13] = timeint;
    crcArray[14] = airchoose;
    qDebug()<<"crcArray"<<crcArray;
    uint16_t tempcrc = ModbusCRC16(crcArray);

    sendArray[0] = 0xA5;
    sendArray[1] = 0x5A;
    sendArray[2] = 0x11;
    sendArray[3] = 0x82;
    sendArray[4] = 0x5C;
    sendArray[5] = 0x00;
    sendArray[6] = state;
    sendArray[7] = stage2_untilw.x[0];
    sendArray[8] = stage2_untilw.x[1];
    sendArray[9] = stage2_untilw.x[2];
    sendArray[10] = stage2_untilw.x[3];
    sendArray[11] = stage2_riseratew.x[0];
    sendArray[12] = stage2_riseratew.x[1];
    sendArray[13] = stage2_riseratew.x[2];
    sendArray[14] = stage2_riseratew.x[3];
    sendArray[15] = timeint>>8;
    sendArray[16] = timeint;
    sendArray[17] = airchoose;
    sendArray[18] = tempcrc;
    sendArray[19] = tempcrc>>8;

    ///qDebug() << QString("write_box_edit_to_stage1 crc %1").arg(tempcrc);
    g_send_flag = 1;
    emit sent_text_array(sendArray);
}

void CtlWidget::write_box_edit_to_stage3()
{

    unsigned char  state = m_stage3Box->currentIndex();
    QString untilString = m_until3Edit->text();
    QString riseString = m_rise3Edit->text();
    QString timeString = m_time3Edit->text();
    unsigned char airchoose = m_airch3Box->currentIndex();

    stage3_untilw.f = untilString.toFloat();
    stage3_riseratew.f = riseString.toFloat();
    uint16_t timeint = timeString.toUShort();

    ///send_write_command(0x50, 0x0C);
    ///qDebug()<<"write_box_edit_to_stage1!"<<"stage1_untilw.f "<<stage1_untilw.f<<"stage1_riseratew.f"<<stage1_riseratew.f<<"timeint"<<timeint<<Qt::endl;

    QByteArray sendArray;
    QByteArray crcArray;
    sendArray.resize(20);
    crcArray.resize(15);
    //数据总长度
    crcArray[0] = 0x82;
    crcArray[1] = 0x68;
    crcArray[2] = 0x00;
    crcArray[3] = state;
    crcArray[4] = stage3_untilw.x[0];
    crcArray[5] = stage3_untilw.x[1];;
    crcArray[6] = stage3_untilw.x[2];;
    crcArray[7] = stage3_untilw.x[3];;
    crcArray[8] = stage3_riseratew.x[0];
    crcArray[9] = stage3_riseratew.x[1];
    crcArray[10] = stage3_riseratew.x[2];
    crcArray[11] = stage3_riseratew.x[3];
    crcArray[12] = timeint>>8;
    crcArray[13] = timeint;
    crcArray[14] = airchoose;
    qDebug()<<"crcArray"<<crcArray;
    uint16_t tempcrc = ModbusCRC16(crcArray);

    sendArray[0] = 0xA5;
    sendArray[1] = 0x5A;
    sendArray[2] = 0x11;
    sendArray[3] = 0x82;
    sendArray[4] = 0x68;
    sendArray[5] = 0x00;
    sendArray[6] = state;
    sendArray[7] = stage3_untilw.x[0];
    sendArray[8] = stage3_untilw.x[1];;
    sendArray[9] = stage3_untilw.x[2];;
    sendArray[10] = stage3_untilw.x[3];;
    sendArray[11] = stage3_riseratew.x[0];
    sendArray[12] = stage3_riseratew.x[1];
    sendArray[13] = stage3_riseratew.x[2];
    sendArray[14] = stage3_riseratew.x[3];
    sendArray[15] = timeint>>8;
    sendArray[16] = timeint;
    sendArray[17] = airchoose;
    sendArray[18] = tempcrc;
    sendArray[19] = tempcrc>>8;

    ///qDebug() << QString("write_box_edit_to_stage1 crc %1").arg(tempcrc);
    g_send_flag = 1;
    emit sent_text_array(sendArray);
}

void CtlWidget::write_box_edit_to_stage4()
{

    unsigned char  state = m_stage4Box->currentIndex();
    QString untilString = m_until4Edit->text();
    QString riseString = m_rise4Edit->text();
    QString timeString = m_time4Edit->text();
    unsigned char airchoose = m_airch4Box->currentIndex();

    stage4_untilw.f = untilString.toFloat();
    stage4_riseratew.f = riseString.toFloat();
    uint16_t timeint = timeString.toUShort();

    ///send_write_command(0x50, 0x0C);
    ///qDebug()<<"write_box_edit_to_stage1!"<<"stage1_untilw.f "<<stage1_untilw.f<<"stage1_riseratew.f"<<stage1_riseratew.f<<"timeint"<<timeint<<Qt::endl;

    QByteArray sendArray;
    QByteArray crcArray;
    sendArray.resize(20);
    crcArray.resize(15);
    //数据总长度
    crcArray[0] = 0x82;
    crcArray[1] = 0x74;
    crcArray[2] = 0x00;
    crcArray[3] = state;
    crcArray[4] = stage4_untilw.x[0];
    crcArray[5] = stage4_untilw.x[1];
    crcArray[6] = stage4_untilw.x[2];
    crcArray[7] = stage4_untilw.x[3];
    crcArray[8] = stage4_riseratew.x[0];
    crcArray[9] = stage4_riseratew.x[1];
    crcArray[10] = stage4_riseratew.x[2];
    crcArray[11] = stage4_riseratew.x[3];
    crcArray[12] = timeint>>8;
    crcArray[13] = timeint;
    crcArray[14] = airchoose;
    qDebug()<<"crcArray"<<crcArray;
    uint16_t tempcrc = ModbusCRC16(crcArray);

    sendArray[0] = 0xA5;
    sendArray[1] = 0x5A;
    sendArray[2] = 0x11;
    sendArray[3] = 0x82;
    sendArray[4] = 0x74;
    sendArray[5] = 0x00;
    sendArray[6] = state;
    sendArray[7] = stage4_untilw.x[0];
    sendArray[8] = stage4_untilw.x[1];
    sendArray[9] = stage4_untilw.x[2];
    sendArray[10] = stage4_untilw.x[3];
    sendArray[11] = stage4_riseratew.x[0];
    sendArray[12] = stage4_riseratew.x[1];
    sendArray[13] = stage4_riseratew.x[2];
    sendArray[14] = stage4_riseratew.x[3];
    sendArray[15] = timeint>>8;
    sendArray[16] = timeint;
    sendArray[17] = airchoose;
    sendArray[18] = tempcrc;
    sendArray[19] = tempcrc>>8;

    ///qDebug() << QString("write_box_edit_to_stage1 crc %1").arg(tempcrc);
    g_send_flag = 1;
    emit sent_text_array(sendArray);
}

void CtlWidget::write_box_edit_to_stage5()
{

    unsigned char  state = m_stage5Box->currentIndex();
    QString untilString = m_until5Edit->text();
    QString riseString = m_rise5Edit->text();
    QString timeString = m_time5Edit->text();
    unsigned char airchoose = m_airch5Box->currentIndex();

    stage5_untilw.f = untilString.toFloat();
    stage5_riseratew.f = riseString.toFloat();
    uint16_t timeint = timeString.toUShort();

    ///send_write_command(0x50, 0x0C);
    ///qDebug()<<"write_box_edit_to_stage1!"<<"stage1_untilw.f "<<stage1_untilw.f<<"stage1_riseratew.f"<<stage1_riseratew.f<<"timeint"<<timeint<<Qt::endl;

    QByteArray sendArray;
    QByteArray crcArray;
    sendArray.resize(20);
    crcArray.resize(15);
    //数据总长度
    crcArray[0] = 0x82;
    crcArray[1] = 0x80;
    crcArray[2] = 0x00;
    crcArray[3] = state;
    crcArray[4] = stage5_untilw.x[0];
    crcArray[5] = stage5_untilw.x[1];
    crcArray[6] = stage5_untilw.x[2];
    crcArray[7] = stage5_untilw.x[3];
    crcArray[8] = stage5_riseratew.x[0];
    crcArray[9] = stage5_riseratew.x[1];
    crcArray[10] = stage5_riseratew.x[2];
    crcArray[11] = stage5_riseratew.x[3];
    crcArray[12] = timeint>>8;
    crcArray[13] = timeint;
    crcArray[14] = airchoose;
    qDebug()<<"crcArray"<<crcArray;
    uint16_t tempcrc = ModbusCRC16(crcArray);

    sendArray[0] = 0xA5;
    sendArray[1] = 0x5A;
    sendArray[2] = 0x11;
    sendArray[3] = 0x82;
    sendArray[4] = 0x80;
    sendArray[5] = 0x00;
    sendArray[6] = state;
    sendArray[7] = stage5_untilw.x[0];
    sendArray[8] = stage5_untilw.x[1];
    sendArray[9] = stage5_untilw.x[2];
    sendArray[10] = stage5_untilw.x[3];
    sendArray[11] = stage5_riseratew.x[0];
    sendArray[12] = stage5_riseratew.x[1];
    sendArray[13] = stage5_riseratew.x[2];
    sendArray[14] = stage5_riseratew.x[3];
    sendArray[15] = timeint>>8;
    sendArray[16] = timeint;
    sendArray[17] = airchoose;
    sendArray[18] = tempcrc;
    sendArray[19] = tempcrc>>8;

    ///qDebug() << QString("write_box_edit_to_stage1 crc %1").arg(tempcrc);
    g_send_flag = 1;
    emit sent_text_array(sendArray);
}

void CtlWidget::write_box_edit_to_stage6()
{

    unsigned char  state = m_stage6Box->currentIndex();
    QString untilString = m_until6Edit->text();
    QString riseString = m_rise6Edit->text();
    QString timeString = m_time6Edit->text();
    unsigned char airchoose = m_airch6Box->currentIndex();

    stage6_untilw.f = untilString.toFloat();
    stage6_riseratew.f = riseString.toFloat();
    uint16_t timeint = timeString.toUShort();

    ///send_write_command(0x50, 0x0C);
    ///qDebug()<<"write_box_edit_to_stage1!"<<"stage1_untilw.f "<<stage1_untilw.f<<"stage1_riseratew.f"<<stage1_riseratew.f<<"timeint"<<timeint<<Qt::endl;

    QByteArray sendArray;
    QByteArray crcArray;
    sendArray.resize(20);
    crcArray.resize(15);
    //数据总长度
    crcArray[0] = 0x82;
    crcArray[1] = 0x8C;
    crcArray[2] = 0x00;
    crcArray[3] = state;
    crcArray[4] = stage6_untilw.x[0];
    crcArray[5] = stage6_untilw.x[1];
    crcArray[6] = stage6_untilw.x[2];
    crcArray[7] = stage6_untilw.x[3];
    crcArray[8] = stage6_riseratew.x[0];
    crcArray[9] = stage6_riseratew.x[1];
    crcArray[10] = stage6_riseratew.x[2];
    crcArray[11] = stage6_riseratew.x[3];
    crcArray[12] = timeint>>8;
    crcArray[13] = timeint;
    crcArray[14] = airchoose;
    qDebug()<<"crcArray"<<crcArray;
    uint16_t tempcrc = ModbusCRC16(crcArray);

    sendArray[0] = 0xA5;
    sendArray[1] = 0x5A;
    sendArray[2] = 0x11;
    sendArray[3] = 0x82;
    sendArray[4] = 0x8C;
    sendArray[5] = 0x00;
    sendArray[6] = state;
    sendArray[7] = stage6_untilw.x[0];
    sendArray[8] = stage6_untilw.x[1];
    sendArray[9] = stage6_untilw.x[2];
    sendArray[10] = stage6_untilw.x[3];
    sendArray[11] = stage6_riseratew.x[0];
    sendArray[12] = stage6_riseratew.x[1];
    sendArray[13] = stage6_riseratew.x[2];
    sendArray[14] = stage6_riseratew.x[3];
    sendArray[15] = timeint>>8;
    sendArray[16] = timeint;
    sendArray[17] = airchoose;
    sendArray[18] = tempcrc;
    sendArray[19] = tempcrc>>8;

    ///qDebug() << QString("write_box_edit_to_stage1 crc %1").arg(tempcrc);
    g_send_flag = 1;
    emit sent_text_array(sendArray);
}

void CtlWidget::deal_mutex_box1_logic(int index)
{
    if(1==index)
    {
        m_stage2Box->setDisabled(false);
    }else
    {
        m_stage2Box->setDisabled(true);
    }
    /*QByteArray sendArray;
    QByteArray crcArray;
    sendArray.resize(9);
    crcArray.resize(4);
    //数据总长度
    crcArray[0] = 0x82;
    crcArray[1] = 0x50;
    crcArray[2] = 0x00;
    if(1==index) crcArray[3] = 0x00;
    if(0==index) crcArray[3] = 0x01;
    uint16_t tempcrc = ModbusCRC16(crcArray);

    sendArray[0] = 0xA5;
    sendArray[1] = 0x5A;
    sendArray[2] = 0x06;
    sendArray[3] = 0x82;
    sendArray[4] = 0x50;
    sendArray[5] = 0x00;
    if(1==index) sendArray[6] = 0x00;
    if(0==index) sendArray[6] = 0x01;        // 0:关闭 1：启用
    sendArray[7] = tempcrc;
    sendArray[8] = tempcrc>>8;

    qDebug() << QString("deal_mutex_box1_logic crc %1").arg(tempcrc);
    emit sent_text_array(sendArray);*/
}

void CtlWidget::deal_mutex_box2_logic(int index)
{
    if(1==index)
    {
        m_stage3Box->setDisabled(false);
    }else
    {
        m_stage3Box->setDisabled(true);
    }

    /*QByteArray sendArray;
    QByteArray crcArray;
    sendArray.resize(9);
    crcArray.resize(4);
    //数据总长度
    crcArray[0] = 0x82;
    crcArray[1] = 0x5C;
    crcArray[2] = 0x00;
    if(1==index) crcArray[3] = 0x00;
    if(0==index) crcArray[3] = 0x01;
    uint16_t tempcrc = ModbusCRC16(crcArray);

    sendArray[0] = 0xA5;
    sendArray[1] = 0x5A;
    sendArray[2] = 0x06;
    sendArray[3] = 0x82;
    sendArray[4] = 0x5C;
    sendArray[5] = 0x00;
    if(1==index) sendArray[6] = 0x00;
    if(0==index) sendArray[6] = 0x01;        // 0:关闭 1：启用
    sendArray[7] = tempcrc;
    sendArray[8] = tempcrc>>8;

    qDebug() << QString("deal_mutex_box2_logic crc %1").arg(tempcrc);
    emit sent_text_array(sendArray);*/

}

void CtlWidget::deal_mutex_box3_logic(int index)
{
    if(1==index)
    {
        m_stage4Box->setDisabled(false);
    }else
    {
        m_stage4Box->setDisabled(true);
    }

   /* QByteArray sendArray;
    QByteArray crcArray;
    sendArray.resize(9);
    crcArray.resize(4);
    //数据总长度
    crcArray[0] = 0x82;
    crcArray[1] = 0x68;
    crcArray[2] = 0x00;
    if(1==index) crcArray[3] = 0x00;
    if(0==index) crcArray[3] = 0x01;
    uint16_t tempcrc = ModbusCRC16(crcArray);

    sendArray[0] = 0xA5;
    sendArray[1] = 0x5A;
    sendArray[2] = 0x06;
    sendArray[3] = 0x82;
    sendArray[4] = 0x68;
    sendArray[5] = 0x00;
    if(1==index) sendArray[6] = 0x00;
    if(0==index) sendArray[6] = 0x01;        // 0:关闭 1：启用
    sendArray[7] = tempcrc;
    sendArray[8] = tempcrc>>8;

    qDebug() << QString("deal_mutex_box3_logic crc %1").arg(tempcrc);
    emit sent_text_array(sendArray);*/
}

void CtlWidget::deal_mutex_box4_logic(int index)
{
    if(1==index)
    {
        m_stage5Box->setDisabled(false);
    }else
    {
        m_stage5Box->setDisabled(true);
    }

    /*QByteArray sendArray;
    QByteArray crcArray;
    sendArray.resize(9);
    crcArray.resize(4);
    //数据总长度
    crcArray[0] = 0x82;
    crcArray[1] = 0x74;
    crcArray[2] = 0x00;
    if(1==index) crcArray[3] = 0x00;
    if(0==index) crcArray[3] = 0x01;
    uint16_t tempcrc = ModbusCRC16(crcArray);

    sendArray[0] = 0xA5;
    sendArray[1] = 0x5A;
    sendArray[2] = 0x06;
    sendArray[3] = 0x82;
    sendArray[4] = 0x74;
    sendArray[5] = 0x00;
    if(1==index) sendArray[6] = 0x00;
    if(0==index) sendArray[6] = 0x01;        // 0:关闭 1：启用
    sendArray[7] = tempcrc;
    sendArray[8] = tempcrc>>8;

    qDebug() << QString("deal_mutex_box4_logic crc %1").arg(tempcrc);
    emit sent_text_array(sendArray);*/
}

void CtlWidget::deal_mutex_box5_logic(int index)
{
    if(1==index)
    {
        m_stage6Box->setDisabled(false);
    }else
    {
        m_stage6Box->setDisabled(true);
    }

    /*QByteArray sendArray;
    QByteArray crcArray;
    sendArray.resize(9);
    crcArray.resize(4);
    //数据总长度
    crcArray[0] = 0x82;
    crcArray[1] = 0x80;
    crcArray[2] = 0x00;
    if(1==index) crcArray[3] = 0x00;
    if(0==index) crcArray[3] = 0x01;
    uint16_t tempcrc = ModbusCRC16(crcArray);

    sendArray[0] = 0xA5;
    sendArray[1] = 0x5A;
    sendArray[2] = 0x06;
    sendArray[3] = 0x82;
    sendArray[4] = 0x80;
    sendArray[5] = 0x00;
    if(1==index) sendArray[6] = 0x00;
    if(0==index) sendArray[6] = 0x01;        // 0:关闭 1：启用
    sendArray[7] = tempcrc;
    sendArray[8] = tempcrc>>8;

    qDebug() << QString("deal_mutex_box5_logic crc %1").arg(tempcrc);
    emit sent_text_array(sendArray);*/
}

void CtlWidget::deal_mutex_box6_logic(int index)
{
    /*QByteArray sendArray;
    QByteArray crcArray;
    sendArray.resize(9);
    crcArray.resize(4);
    //数据总长度
    crcArray[0] = 0x82;
    crcArray[1] = 0x8C;
    crcArray[2] = 0x00;
    if(1==index) crcArray[3] = 0x00;
    if(0==index) crcArray[3] = 0x01;
    uint16_t tempcrc = ModbusCRC16(crcArray);

    sendArray[0] = 0xA5;
    sendArray[1] = 0x5A;
    sendArray[2] = 0x06;
    sendArray[3] = 0x82;
    sendArray[4] = 0x8C;
    sendArray[5] = 0x00;
    if(1==index) sendArray[6] = 0x00;
    if(0==index) sendArray[6] = 0x01;        // 0:关闭 1：启用
    sendArray[7] = tempcrc;
    sendArray[8] = tempcrc>>8;

    qDebug() << QString("deal_mutex_box6_logic crc %1").arg(tempcrc);
    emit sent_text_array(sendArray);*/
}


void CtlWidget::show_close_port_success_msgbox()
{
    QMessageBox mesg;
    mesg.information(this,tr("INFO"),tr("串口关闭成功!"));
    return;
}

void CtlWidget::show_open_port_failed_msgbox()
{
    QMessageBox mesg;
    mesg.critical(this,tr("ERR"),tr("串口未打开!"));
    return;
}

void CtlWidget::show_write_success_msgbox()
{
    QMessageBox mesg;
    mesg.information(this,tr("INFO"),tr("执行成功!"));
    return;
}

void CtlWidget::show_write_failed_msgbox()
{
    QMessageBox mesg;
    mesg.critical(this,tr("ERR"),tr("执行失败!"));
    return;
}

void CtlWidget::show_port_success_msgbox()
{
    QMessageBox mesg;
    mesg.information(this,tr("INFO"),tr("打开串口成功!"));
    return;
}

void CtlWidget::show_port_failed_msgbox()
{
    QMessageBox mesg;
    mesg.critical(this,tr("ERR"),tr("打开串口失败!"));
    return;
}

void CtlWidget::disable_load_button()
{
    m_pushbutton->setDisabled(true);
}

void CtlWidget::enable_load_data()
{
    m_pushbutton->setDisabled(false);
}

void CtlWidget::on_receive(QByteArray tmpdata)
{
   ///m_recvEdit->append(tmpdata.toHex().toUpper());
}

void CtlWidget::send_text_data()
{
    ///emit change_send_flag_to_port();
    g_send_flag = 1;
    QString sendString = m_sendEdit->toPlainText();
    qDebug()<<sendString<<Qt::endl;
    QByteArray sendArray = QByteArray::fromHex(sendString.toUtf8());;
    ///QByteArray sendArray = sendString.toUtf8();
    emit sent_text_array(sendArray);
}

void CtlWidget::read_run_state_to_checkbox()
{
    send_read_command(0x00, 0x01);
}


void CtlWidget::read_run_mode_to_checkbox()
{
    ///send_read_command(0x2C, 0x01);
}

void CtlWidget::execute_send_plan()
{
    QByteArray sendArray;
    QByteArray crcArray;
    sendArray.resize(9);
    crcArray.resize(4);
    //数据总长度
    crcArray[0] = 0x82;
    crcArray[1] = 0x2C;
    crcArray[2] = 0x00;
    if(1==m_checkbox1_flag) crcArray[3] = 0x00;
    if(1==m_checkbox2_flag) crcArray[3] = 0x01;        // 0:停止 1：启动 2：启动升温速率自整定运行 4：停止升温速率自整定
    if(1==m_checkbox3_flag) crcArray[3] = 0x02;
    if(1==m_checkbox4_flag) crcArray[3] = 0x03;
    uint16_t tempcrc = ModbusCRC16(crcArray);

    sendArray[0] = 0xA5;
    sendArray[1] = 0x5A;
    sendArray[2] = 0x06;
    sendArray[3] = 0x82;
    sendArray[4] = 0x2C;
    sendArray[5] = 0x00;
    if(1==m_checkbox1_flag) sendArray[6] = 0x00;
    if(1==m_checkbox2_flag) sendArray[6] = 0x01;        // 0:停止 1：启动 2：启动升温速率自整定运行 4：停止升温速率自整定
    if(1==m_checkbox3_flag) sendArray[6] = 0x02;
    if(1==m_checkbox4_flag) sendArray[6] = 0x03;
    sendArray[7] = tempcrc;
    sendArray[8] = tempcrc>>8;
    ///sendArray = crcArray.append((unsigned char)tempcrc);
    ///sendArray = crcArray.append(tempcrc>>8);
    qDebug() << QString("execute_send_plan crc %1").arg(tempcrc);
    emit sent_text_array(sendArray);
    if(m_checkBox2->isChecked()) emit change_test_start_flag();
}

uint16_t CtlWidget::ModbusCRC16(QByteArray senddata)
{
    int len=senddata.size();
    uint16_t wcrc=0XFFFF;//预置16位crc寄存器，初值全部为1
    uint8_t temp;//定义中间变量
    int i=0,j=0;//定义计数
    for(i=0;i<len;i++)//循环计算每个数据
    {
        temp=senddata.at(i);
        wcrc^=temp;
        for(j=0;j<8;j++){
            //判断右移出的是不是1，如果是1则与多项式进行异或。
            if(wcrc&0X0001){
                wcrc>>=1;//先将数据右移一位
                wcrc^=0XA001;//与上面的多项式进行异或
            }
            else//如果不是1，则直接移出
                wcrc>>=1;//直接移出
        }
    }
    temp=wcrc;//crc的值
    return wcrc;
}

//槽函数
void CtlWidget::read_xlsx_data()
{
    QString sourcFilePath = QFileDialog::getOpenFileName(nullptr,"请选择待处理的数据",qApp->applicationDirPath()+"/","*.xlsx;;all(*)");
    if(sourcFilePath.isEmpty()){
        QMessageBox mesg;
        mesg.critical(this,tr("Error"),tr("打开文件路径发生错误!"));
        return;
    }

    mutex.lock();
    slist_temper.clear();
    slist_dsc.clear();
    QXlsx::Document xlsx(sourcFilePath);
    xlsx.selectSheet(m_comboBox4->currentText());
    ///QXlsx::Workbook *workBook = xlsx.workbook();
    QXlsx::Worksheet *workSheet = xlsx.currentWorksheet();
    ///QXlsx::Worksheet *workSheet = static_cast<QXlsx::Worksheet*>(workBook->sheet(0));
    ulong xlsxrows=workSheet->dimension().rowCount();//读取文件行数
    ///ulong xlsxcloums=workSheet->dimension().columnCount();//读取文件列数
    g_sname_string  = workSheet->cellAt(1, 1)->value().toString();
    g_quality = workSheet->cellAt(1, 2)->value().toString().toDouble();
    emit show_read_sample_param(g_sname_string, g_quality);
    for (ulong i=2;i<=xlsxrows;i++){
        slist_temper.append(workSheet->cellAt(i, 3)->value().toString());
        slist_dsc.append(workSheet->cellAt(i, 4)->value().toString());
    }
    slist_temper.removeFirst();//去掉第一行标题行
    slist_dsc.removeFirst();//去掉第一行标题行
    mutex.unlock();

    qDebug() << "temper vale" << slist_temper.last()<< "end" << Qt::endl;
    qDebug() << "temper num" << slist_temper.size()<< "end" << Qt::endl;
    qDebug() << "dsc vale" << slist_dsc.last() << "end" << Qt::endl;
    qDebug() << "dsc num" << slist_dsc.size()<< "end" << Qt::endl;
    qDebug() << "xlsxrows" << xlsxrows << "end" << Qt::endl;
    xlsx.deleteLater();

    emit enable_load_button();

}

void CtlWidget::save_data_to_xlsx()
{
        QMessageBox::information(this,"提示","保存前请关闭串口！");

        bool res = false;
        QString dir = QFileDialog::getExistingDirectory(this, tr("选择信息导出文件目录"),"/home",QFileDialog::ShowDirsOnly|QFileDialog::DontResolveSymlinks);
        if(dir.isEmpty())
            QMessageBox::information(this,"提示","路径为空！设置保存报告路径失败！");
        else{
            bool exist;
            QDir *folder = new QDir();
            QString historydir="/histroy"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh-mm-ss");
            exist = folder->exists(dir+historydir);//文件夹是否存在
            if(!exist)folder->mkdir(dir+historydir);//不存在创建文件夹
            // 设置excel表头（第一行数据）
            QString groupname[6]={"RX Date/Time","温度控制/运行时间","温度控制/温度","温度控制/DSC","温度控制/升温速率","温度控制/占空比"};
            QStringList titleList;
            titleList.clear();
            for(int i=0;i<6;i++)
                titleList.append(groupname[i]);

            QXlsx::Document xlsx;
            ///xlsx.addSheet("铟");
            xlsx.addSheet(m_comboBox4->currentText());
            xlsx.setColumnWidth(1,6);//第1行,6列宽
            for (int i = 0; i < titleList.size(); i++){
                xlsx.write(1, 1, g_sname_string);
                xlsx.write(1, 2, g_quality);
                xlsx.write(2, i+1, titleList.at(i));     //这里写第2行，第n列数据
                //开始写第n行数据
                for(int j =3; j<g_qvecf_time.size(); j++)
                {
                    xlsx.write(j,2,g_qvecf_time[j]);
                }
                for(int j =3; j<g_qvecd_temper.size(); j++)
                {
                    xlsx.write(j,3,g_qvecd_temper[j]);
                }
                for(int j =3; j<g_qvecd_dsc.size(); j++)
                {
                    xlsx.write(j,4,g_qvecd_dsc[j]);
                }
                for(int j =3; j<g_qvecd_riserate.size(); j++)
                {
                    xlsx.write(j,5,g_qvecd_riserate[j]);
                }
            }
            /*xlsx.addSheet("锡");
            xlsx.setColumnWidth(1,6);//第1行,6列宽
            for (int i = 0; i < titleList.size(); i++){
                xlsx.write(1, i+1, titleList.at(i));     //这里写第1行，第n列数据
                //开始写第n行数据
                for(int j =2 ;j<10000;j++){
                    xlsx.write(j,i+1,0);
                }
            }
            xlsx.addSheet("铅");
            xlsx.setColumnWidth(1,6);//第1行,6列宽
            for (int i = 0; i < titleList.size(); i++){
                xlsx.write(1, i+1, titleList.at(i));     //这里写第1行，第n列数据
                //开始写第n行数据
                for(int j =2 ;j<10000;j++){
                    xlsx.write(j,i+1,0);
                }
            }
            xlsx.addSheet("锌");
            xlsx.setColumnWidth(1,6);//第1行,6列宽
            for (int i = 0; i < titleList.size(); i++){
                xlsx.write(1, i+1, titleList.at(i));     //这里写第1行，第n列数据
                //开始写第n行数据
                for(int j =2 ;j<10000;j++){
                    xlsx.write(j,i+1,0);
                }
            }*/
            // 最后，保存文件
            res=xlsx.saveAs(dir+historydir+"/"+(m_comboBox4->currentText())+QDateTime::currentDateTime().toString("yyyy-MM-dd hh-mm-ss")+".ana");
            xlsx.deleteLater();

        }
        if(res)
            QMessageBox::information(this,"提示","保存成功！");
        else
            QMessageBox::information(this,"提示","保存失败！");

}


void CtlWidget::change_checkbox1_state(int state)
{
    if (state == Qt::Checked)
    {
        m_checkbox1_flag = 1;
        qDebug() << "选中";
    }
    else if (state == Qt::Unchecked)
    {
        m_checkbox1_flag = 0;
        qDebug() << "未选中";
    }
    else if (state == Qt::PartiallyChecked)
    {
        m_checkbox1_flag = 0;
        qDebug() << "半选中";
    }

}
void CtlWidget::change_checkbox2_state(int state)
{
    if (state == Qt::Checked)
    {
        m_checkbox2_flag = 1;
        qDebug() << "选中";
    }
    else if (state == Qt::Unchecked)
    {
        m_checkbox2_flag = 0;
        qDebug() << "未选中";
    }
    else if (state == Qt::PartiallyChecked)
    {
        m_checkbox2_flag = 0;
        qDebug() << "半选中";
    }

}
void CtlWidget::change_checkbox3_state(int state)
{
    if (state == Qt::Checked)
    {
        m_checkbox3_flag = 1;
        qDebug() << "选中";
    }
    else if (state == Qt::Unchecked)
    {
        m_checkbox3_flag = 0;
        qDebug() << "未选中";
    }
    else if (state == Qt::PartiallyChecked)
    {
        m_checkbox3_flag = 0;
        qDebug() << "半选中";
    }

}
void CtlWidget::change_checkbox4_state(int state)
{
    if (state == Qt::Checked)
    {
        m_checkbox4_flag = 1;
        qDebug() << "选中";
    }
    else if (state == Qt::Unchecked)
    {
        m_checkbox4_flag = 0;
        qDebug() << "未选中";
    }
    else if (state == Qt::PartiallyChecked)
    {
        m_checkbox4_flag = 0;
        qDebug() << "半选中";
    }

}
