#include <LoUIClass/x_uimsgboxok.h>
#include "mguangyingpinwidget.h"
#include "ui_mguangyingpinwidget.h"
#include <QDateTime>
#include "globaldefine.h"
mGuangYingPinWidget::mGuangYingPinWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::mGuangYingPinWidget)
{
    ui->setupUi(this);
    ui->pushButton1->setProperty("ssType", "progManageTool");
    ui->pushButton1->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButton2->setProperty("ssType", "progManageTool");
    ui->pushButton2->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButton3->setProperty("ssType", "progManageTool");
    ui->pushButton3->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButton4->setProperty("ssType", "progManageTool");
    ui->pushButton4->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButton5->setProperty("ssType", "progManageTool");
    ui->pushButton5->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButton6->setProperty("ssType", "progManageTool");
    ui->pushButton6->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButton7->setProperty("ssType", "progManageTool");
    ui->pushButton7->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButton8->setProperty("ssType", "progManageTool");
    ui->pushButton8->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButton9->setProperty("ssType", "progManageTool");
    ui->pushButton9->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButton10->setProperty("ssType", "progManageTool");
    ui->pushButton10->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButton11->setProperty("ssType", "progManageTool");
    ui->pushButton11->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButton12->setProperty("ssType", "progManageTool");
    ui->pushButton12->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButton13->setProperty("ssType", "progManageTool");
    ui->pushButton13->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButton14->setProperty("ssType", "progManageTool");
    ui->pushButton14->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButton15->setProperty("ssType", "progManageTool");
    ui->pushButton15->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButton16->setProperty("ssType", "progManageTool");
    ui->pushButton16->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButton17->setProperty("ssType", "progManageTool");
    ui->pushButton17->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButton18->setProperty("ssType", "progManageTool");
    ui->pushButton18->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButton19->setProperty("ssType", "progManageTool");
    ui->pushButton19->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButton20->setProperty("ssType", "progManageTool");
    ui->pushButton20->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");

    ui->pushButtonSend->setProperty("ssType", "progManageTool");
    ui->pushButtonSend->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButtonPrevious->setProperty("ssType", "progManageTool");
    ui->pushButtonPrevious->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButtonPrevious->setIcon(QIcon(":/res/previous.png"));
    ui->pushButtonNext->setProperty("ssType", "progManageTool");
    ui->pushButtonNext->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButtonNext->setIcon(QIcon(":/res/next.png"));
    ui->pushButtonLoop->setProperty("ssType", "progManageTool");
    ui->pushButtonLoop->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButtonLoop->setIcon(QIcon(":/res/loop.png"));
    ui->pushButtonMainOpen->setProperty("ssType", "progManageTool");
    ui->pushButtonMainOpen->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButtonAuxOpen->setProperty("ssType", "progManageTool");
    ui->pushButtonAuxOpen->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButtonBrightnessSet->setProperty("ssType", "progManageTool");
    ui->pushButtonBrightnessSet->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButtonScreenOn->setProperty("ssType", "progManageTool");
    ui->pushButtonScreenOn->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButtonScreenOff->setProperty("ssType", "progManageTool");
    ui->pushButtonScreenOff->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButtonRefresh->setProperty("ssType", "progManageTool");
    ui->pushButtonRefresh->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButtonClearLog->setProperty("ssType", "progManageTool");
    ui->pushButtonClearLog->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    m_recvNum = 0;
    m_sendNum = 0;
    sPortThread = new TA_SerialThread;
    sPortThread->SearchPort();

    for(uint8_t i = 0;i < sPortThread->portCnt;i++)
    {
        ui->comboBox_SPortName->addItem(sPortThread->m_arrSerial[i].portName());
        ui->comboBox_SPortNameAux->addItem(sPortThread->m_arrSerial[i].portName());
    }

    //0:1200 1:2400 2:4800 3:9600 4:19200 5:38400 6:57600 7:115200
    if(ui->comboBox_SPortName->count()>=1)
    {
        ui->comboBox_SPortBaud->setCurrentIndex(3);
        //0:5 1:6 2:7 3:8
        ui->comboBox_SPortDataBit->setCurrentIndex(3);
        //0:no p 2:e p 3:o p 4:s p 5:m p
        ui->comboBox_SPortOEBit->setCurrentIndex(0);
        //0:1 1:2 2:1.5
        ui->comboBox_SPortStopBit->setCurrentIndex(0);

        ui->graphicsView_ComStatus->setStyleSheet("background-color: rgb(255, 0, 0);");
        ui->label_ComStatus->setText(tr("State:Off"));

        ui->comboBox_SPortBaudAux->setCurrentIndex(3);
        ui->comboBox_SPortDataBitAux->setCurrentIndex(3);
        ui->comboBox_SPortOEBitAux->setCurrentIndex(0);
        ui->comboBox_SPortStopBitAux->setCurrentIndex(0);
        ui->graphicsView_ComStatusAux->setStyleSheet("background-color: rgb(255, 0, 0);");
        ui->label_ComStatusAux->setText(tr("State:Off"));
        if(ui->comboBox_SPortName->count()>1)
        {
            ui->comboBox_SPortNameAux->setCurrentIndex(1);
        }
    }
    ui->lineEdit_ResendTimes->setValidator(new QIntValidator(1,1000,this));
    ui->lineEdit_DelayMill->setValidator(new QIntValidator(1,1000,this));


    connect(ui->pushButtonMainOpen, SIGNAL(clicked(bool)), this, SLOT(MWOnoffPort()));
    connect(ui->pushButtonAuxOpen, SIGNAL(clicked(bool)), this, SLOT(MWOnoffPortAux()));
    connect(ui->pushButton1, SIGNAL(clicked()), this, SLOT(OnAnsyProgram1()));
    connect(ui->pushButton2, SIGNAL(clicked()), this, SLOT(OnAnsyProgram2()));
    connect(ui->pushButton3, SIGNAL(clicked()), this, SLOT(OnAnsyProgram3()));
    connect(ui->pushButton4, SIGNAL(clicked()), this, SLOT(OnAnsyProgram4()));
    connect(ui->pushButton5, SIGNAL(clicked()), this, SLOT(OnAnsyProgram5()));
    connect(ui->pushButton6, SIGNAL(clicked()), this, SLOT(OnAnsyProgram6()));
    connect(ui->pushButton7, SIGNAL(clicked()), this, SLOT(OnAnsyProgram7()));
    connect(ui->pushButton8, SIGNAL(clicked()), this, SLOT(OnAnsyProgram8()));
    connect(ui->pushButton9, SIGNAL(clicked()), this, SLOT(OnAnsyProgram9()));
    connect(ui->pushButton10, SIGNAL(clicked()), this, SLOT(OnAnsyProgram10()));
    connect(ui->pushButton11, SIGNAL(clicked()), this, SLOT(OnAnsyProgram11()));
    connect(ui->pushButton12, SIGNAL(clicked()), this, SLOT(OnAnsyProgram12()));
    connect(ui->pushButton13, SIGNAL(clicked()), this, SLOT(OnAnsyProgram13()));
    connect(ui->pushButton14, SIGNAL(clicked()), this, SLOT(OnAnsyProgram14()));
    connect(ui->pushButton15, SIGNAL(clicked()), this, SLOT(OnAnsyProgram15()));
    connect(ui->pushButton16, SIGNAL(clicked()), this, SLOT(OnAnsyProgram16()));
    connect(ui->pushButton17, SIGNAL(clicked()), this, SLOT(OnAnsyProgram17()));
    connect(ui->pushButton18, SIGNAL(clicked()), this, SLOT(OnAnsyProgram18()));
    connect(ui->pushButton19, SIGNAL(clicked()), this, SLOT(OnAnsyProgram19()));
    connect(ui->pushButton20, SIGNAL(clicked()), this, SLOT(OnAnsyProgram20()));
    connect(ui->pushButtonClearLog, SIGNAL(clicked()), this, SLOT(OnClearLog()));
    connect(ui->pushButtonSend, SIGNAL(clicked()), this, SLOT(OnAnsyProgramCustom()));
    connect(ui->pushButtonNext, SIGNAL(clicked()), this, SLOT(OnAnsyProgramNext()));
    connect(ui->pushButtonPrevious, SIGNAL(clicked()), this, SLOT(OnAnsyProgramPrevious()));
    connect(ui->pushButtonLoop, SIGNAL(clicked()), this, SLOT(OnAnsyProgramLoop()));
    connect(ui->pushButtonBrightnessSet, SIGNAL(clicked()), this, SLOT(OnBrightnessSet()));
    connect(ui->pushButtonScreenOn, SIGNAL(clicked()), this, SLOT(OnScreenOn()));
    connect(ui->pushButtonScreenOff, SIGNAL(clicked()), this, SLOT(OnScreenOff()));
    connect(ui->spinBox_ProgramIndex, SIGNAL(valueChanged(int)), this, SLOT(OnAnsyProgramCustomByChanged(int)));
    connect(ui->spinBox_BrightnessValue, SIGNAL(valueChanged(int)), this, SLOT(OnBrightnessSetByChanged(int)));
    connect(ui->pushButtonRefresh, SIGNAL(clicked()), this, SLOT(OnRefresh()));



}

mGuangYingPinWidget::~mGuangYingPinWidget()
{

    delete ui;
}
void mGuangYingPinWidget::refreshLable()
{
    ui->pushButtonSend->setText(tr("Send"));
//    ui->pushButtonPrevious->setText(tr("Previous"));
//    ui->pushButtonNext->setText(tr("Next"));
    ui->pushButtonMainOpen->setText(tr("Open"));
    ui->pushButtonAuxOpen->setText(tr("Open"));
    ui->pushButtonBrightnessSet->setText(tr("Set"));
    ui->pushButtonScreenOn->setText(tr("Screen On"));
    ui->pushButtonScreenOff->setText(tr("Screen Off"));
    ui->pushButtonRefresh->setText(tr("Refresh"));
    ui->groupBoxMain->setTitle(tr("Main"));
    ui->groupBoxAux->setTitle(tr("Auxiliary"));
    ui->groupBoxParam->setTitle(tr("Param configuration"));
    ui->label_PortName->setText(tr("PortName"));
    ui->label_BaudRate->setText(tr("BaudRate"));
    ui->label_DataBit->setText(tr("DataBit"));
    ui->label_OEBit->setText(tr("OEBit"));
    ui->label_StopBit->setText(tr("StopBit"));
    ui->label_PortNameAux->setText(tr("PortName"));
    ui->label_BaudRateAux->setText(tr("BaudRate"));
    ui->label_DataBitAux->setText(tr("DataBit"));
    ui->label_OEBitAux->setText(tr("OEBit"));
    ui->label_StopBitAux->setText(tr("StopBit"));

    ui->label_ResendTimes->setText(tr("Resend times"));
    ui->label_DelayMill->setText(tr("Delay millsecond"));
    ui->label_ProgramNum->setText(tr("Program number"));
    ui->label_BrightNess->setText(tr("Brightness"));
    ui->label_ComStatus->setText(tr("State:Off"));
    ui->label_ComStatusAux->setText(tr("State:Off"));
    ui->pushButtonClearLog->setText(tr("Clear"));
    ui->checkBoxDebug->setText(tr("Debug"));
    ui->groupBox_Network->setTitle(tr("Network"));
    ui->groupBox_com->setTitle(tr("Com"));
}

void mGuangYingPinWidget::OnAnsyProgram1(void)
{
    MACRO_ANSY_PROGRAM(1)
}
void mGuangYingPinWidget::OnAnsyProgram2(void)
{
    MACRO_ANSY_PROGRAM(2)

}void mGuangYingPinWidget::OnAnsyProgram3(void)
{
    MACRO_ANSY_PROGRAM(3)
}void mGuangYingPinWidget::OnAnsyProgram4(void)
{
    MACRO_ANSY_PROGRAM(4)
}void mGuangYingPinWidget::OnAnsyProgram5(void)
{
    MACRO_ANSY_PROGRAM(5)
}void mGuangYingPinWidget::OnAnsyProgram6(void)
{
    MACRO_ANSY_PROGRAM(6)
}void mGuangYingPinWidget::OnAnsyProgram7(void)
{
    MACRO_ANSY_PROGRAM(7)
}void mGuangYingPinWidget::OnAnsyProgram8(void)
{
    MACRO_ANSY_PROGRAM(8)
}void mGuangYingPinWidget::OnAnsyProgram9(void)
{
    MACRO_ANSY_PROGRAM(9)
}void mGuangYingPinWidget::OnAnsyProgram10(void)
{
    MACRO_ANSY_PROGRAM(10)
}void mGuangYingPinWidget::OnAnsyProgram11(void)
{
    MACRO_ANSY_PROGRAM(11)
}void mGuangYingPinWidget::OnAnsyProgram12(void)
{
    MACRO_ANSY_PROGRAM(12)
}void mGuangYingPinWidget::OnAnsyProgram13(void)
{
    MACRO_ANSY_PROGRAM(13)
}void mGuangYingPinWidget::OnAnsyProgram14(void)
{
    MACRO_ANSY_PROGRAM(14)
}void mGuangYingPinWidget::OnAnsyProgram15(void)
{
    MACRO_ANSY_PROGRAM(15)
}void mGuangYingPinWidget::OnAnsyProgram16(void)
{
    MACRO_ANSY_PROGRAM(16)
}void mGuangYingPinWidget::OnAnsyProgram17(void)
{
    MACRO_ANSY_PROGRAM(17)
}void mGuangYingPinWidget::OnAnsyProgram18(void)
{
    MACRO_ANSY_PROGRAM(18)
}void mGuangYingPinWidget::OnAnsyProgram19(void)
{
    MACRO_ANSY_PROGRAM(19)
}
void mGuangYingPinWidget::OnAnsyProgram20(void)
{
    MACRO_ANSY_PROGRAM(20)
}
void mGuangYingPinWidget::OnClearLog(void)
{
    ui->textEditReadBuf->clear();
}
void mGuangYingPinWidget::OnAnsyProgramCustom(void)
{
    int itemp=ui->spinBox_ProgramIndex->value();
    MACRO_ANSY_PROGRAM(itemp)

    if(ui->checkBoxDebug->isChecked())
    {
        ui->textEditReadBuf->append("OnAnsyProgramCustom");
    }
}
void mGuangYingPinWidget::OnAnsyProgramCustomByChanged(int icurIndex)
{
    MACRO_ANSY_PROGRAM(icurIndex)
}
void mGuangYingPinWidget::OnBrightnessSetByChanged(int icurValue)
{
    if(ui->pushButtonMainOpen->text()==tr("Close"))
        Set_Brightness_buf_and_send(ui->comboBox_SPortName->currentIndex(),icurValue);
    if(ui->pushButtonAuxOpen->text()==tr("Close"))
        Set_Brightness_buf_and_send(ui->comboBox_SPortNameAux->currentIndex(),icurValue);
 }

void mGuangYingPinWidget::OnAnsyProgramNext(void)
{
    ui->spinBox_ProgramIndex->setValue(ui->spinBox_ProgramIndex->value()+1);
    OnAnsyProgramCustom();
//    int itemp=ui->spinBox_ProgramIndex->value();
//     MACRO_ANSY_PROGRAM(itemp)
//      if(ui->pushButtonMainOpen->text()==tr("Close"))
//           NextProgram_buf_and_send(ui->comboBox_SPortName->currentIndex());
//      if(ui->pushButtonAuxOpen->text()==tr("Close"))
//           NextProgram_buf_and_send(ui->comboBox_SPortNameAux->currentIndex());

}
void mGuangYingPinWidget::OnAnsyProgramPrevious(void)
{
    ui->spinBox_ProgramIndex->setValue(ui->spinBox_ProgramIndex->value()-1);
    OnAnsyProgramCustom();
//    int itemp=ui->spinBox_ProgramIndex->value();
//     MACRO_ANSY_PROGRAM(itemp)
}
void mGuangYingPinWidget::OnAnsyProgramLoop(void)
{
     MACRO_ANSY_PROGRAM(0)
}
void mGuangYingPinWidget::OnBrightnessSet(void)
{
    if(ui->groupBox_com->isChecked())
    {
        if(ui->pushButtonMainOpen->text()==tr("Close"))
            Set_Brightness_buf_and_send(ui->comboBox_SPortName->currentIndex(),ui->spinBox_BrightnessValue->value());
        if(ui->pushButtonAuxOpen->text()==tr("Close"))
            Set_Brightness_buf_and_send(ui->comboBox_SPortNameAux->currentIndex(),ui->spinBox_BrightnessValue->value());
    }

    if(ui->groupBox_Network->isChecked())
    {
        Set_Brightness_buf_and_send_by_udp(ui->spinBox_BrightnessValue->value());
    }
}
void mGuangYingPinWidget::OnScreenOn(void)
{
    if(ui->groupBox_com->isChecked())
    {
        if(ui->pushButtonMainOpen->text()==tr("Close"))
            Screen_OnOff_buf_and_send(ui->comboBox_SPortName->currentIndex(),0x11);
        if(ui->pushButtonAuxOpen->text()==tr("Close"))
            Screen_OnOff_buf_and_send(ui->comboBox_SPortNameAux->currentIndex(),0x11);
    }
    if(ui->groupBox_Network->isChecked())
    {
        Screen_OnOff_buf_and_send_by_udp(0x11);
    }
}
void mGuangYingPinWidget::OnScreenOff(void)
{
    if(ui->groupBox_com->isChecked())
    {
        if(ui->pushButtonMainOpen->text()==tr("Close"))
            Screen_OnOff_buf_and_send(ui->comboBox_SPortName->currentIndex(),0x10);
        if(ui->pushButtonAuxOpen->text()==tr("Close"))
            Screen_OnOff_buf_and_send(ui->comboBox_SPortNameAux->currentIndex(),0x10);
    }
    if(ui->groupBox_Network->isChecked())
    {
        Screen_OnOff_buf_and_send_by_udp(0x10);

    }
}
void mGuangYingPinWidget::OnRefresh()
{
    bool resetAllComboxFlag=true;
    if(ui->comboBox_SPortName->count()>0)
        resetAllComboxFlag=false;
//    ui->comboBox_SPortName->clear();
//    ui->comboBox_SPortNameAux->clear();

    sPortThread->SearchPort();
//    for (int m=0;m<sPortThread->portCnt;m++) {

//    }


    for(uint8_t i = 0;i < sPortThread->portCnt;i++)
    {
        bool iHaveFlag=false;
        for(int j=0;j<ui->comboBox_SPortName->count();j++)
        {
            if(ui->comboBox_SPortName->itemText(j)==sPortThread->m_arrSerial[i].portName())
            {
                iHaveFlag=true;
                break;
            }
        }
        if(iHaveFlag==false)
        {
            ui->comboBox_SPortName->addItem(sPortThread->m_arrSerial[i].portName());
        }
        bool iHaveAuxFlag=false;
        for(int jj=0;jj<ui->comboBox_SPortNameAux->count();jj++)
        {
            if(ui->comboBox_SPortNameAux->itemText(jj)==sPortThread->m_arrSerial[i].portName())
            {
                iHaveAuxFlag=true;
                break;
            }
        }
        if(iHaveAuxFlag==false)
        {
            ui->comboBox_SPortNameAux->addItem(sPortThread->m_arrSerial[i].portName());
            if(ui->comboBox_SPortNameAux->count()>1&&ui->pushButtonAuxOpen->text()==tr("Open"))
            {
                ui->comboBox_SPortNameAux->setCurrentIndex(i);
            }

        }
//                    ui->comboBox_SPortName->addItem(sPortThread->m_arrSerial[i].portName());
//                    ui->comboBox_SPortNameAux->addItem(sPortThread->m_arrSerial[i].portName());
    }

    if(resetAllComboxFlag)
    {
        //0:1200 1:2400 2:4800 3:9600 4:19200 5:38400 6:57600 7:115200
        if(ui->comboBox_SPortName->count()>=1)
        {
            ui->comboBox_SPortBaud->setCurrentIndex(3);
            //0:5 1:6 2:7 3:8
            ui->comboBox_SPortDataBit->setCurrentIndex(3);
            //0:no p 2:e p 3:o p 4:s p 5:m p
            ui->comboBox_SPortOEBit->setCurrentIndex(0);
            //0:1 1:2 2:1.5
            ui->comboBox_SPortStopBit->setCurrentIndex(0);

            ui->graphicsView_ComStatus->setStyleSheet("background-color: rgb(255, 0, 0);");
            ui->label_ComStatus->setText(tr("State:Off"));

            ui->comboBox_SPortBaudAux->setCurrentIndex(3);
            ui->comboBox_SPortDataBitAux->setCurrentIndex(3);
            ui->comboBox_SPortOEBitAux->setCurrentIndex(0);
            ui->comboBox_SPortStopBitAux->setCurrentIndex(0);
            ui->graphicsView_ComStatusAux->setStyleSheet("background-color: rgb(255, 0, 0);");
            ui->label_ComStatusAux->setText(tr("State:Off"));
            if(ui->comboBox_SPortName->count()>1)
            {
                ui->comboBox_SPortNameAux->setCurrentIndex(1);
            }
        }

    }

}
void mGuangYingPinWidget::MWOnoffPort(void)
{
    uint8_t comIndex = ui->comboBox_SPortName->currentIndex();

    if(ui->pushButtonMainOpen->text()==tr("Open"))
    {
        sPortThread->InitPortName(comIndex,ui->comboBox_SPortName->currentText());
        sPortThread->InitPortBaudRate(comIndex,ui->comboBox_SPortBaud->currentText().toInt());

        //qDebug()<<ui->comboBox_SPortDataBit->currentIndex();
        switch(ui->comboBox_SPortDataBit->currentIndex())
        {
            case 0: sPortThread->InitPortDataBits(comIndex,QSerialPort::Data5); break;//0:5
            case 1: sPortThread->InitPortDataBits(comIndex,QSerialPort::Data6); break;//1:6
            case 2: sPortThread->InitPortDataBits(comIndex,QSerialPort::Data7); break;//2:7
            case 3: sPortThread->InitPortDataBits(comIndex,QSerialPort::Data8); break;//3:8
            default: break;
        }

        switch(ui->comboBox_SPortStopBit->currentIndex())
        {
            case 0:sPortThread->InitPortStopBits(comIndex,QSerialPort::OneStop);break;
            case 1:sPortThread->InitPortStopBits(comIndex,QSerialPort::TwoStop);break;
            case 2:sPortThread->InitPortStopBits(comIndex,QSerialPort::OneAndHalfStop);break;
            default: break;
        }

        switch(ui->comboBox_SPortOEBit->currentIndex())
        {
            case 0:sPortThread->InitPortParity(comIndex,QSerialPort::NoParity);break;
            case 1:sPortThread->InitPortParity(comIndex,QSerialPort::EvenParity);break;
            case 2:sPortThread->InitPortParity(comIndex,QSerialPort::OddParity);break;
            case 3:sPortThread->InitPortParity(comIndex,QSerialPort::SpaceParity);break;
            case 4:sPortThread->InitPortParity(comIndex,QSerialPort::MarkParity);break;
            default: break;
        }
        if(sPortThread->OpenPort(comIndex))
        {
            connect(&(sPortThread->m_arrSerial[comIndex]), SIGNAL(readyRead()), this, SLOT(MWReadData()));

            //参数设置禁止
            ui->comboBox_SPortName->setEnabled(false);
            ui->comboBox_SPortBaud->setEnabled(false);
            ui->comboBox_SPortDataBit->setEnabled(false);
            ui->comboBox_SPortOEBit->setEnabled(false);
            ui->comboBox_SPortStopBit->setEnabled(false);
            ui->pushButtonMainOpen->setText(tr("Close"));
            //发送设置使能
            ui->graphicsView_ComStatus->setStyleSheet("background-color: rgb(0, 255, 0);");
            ui->label_ComStatus->setText(tr("State:On"));
        }
        else {
            X_UIMsgBoxOk *pDlg=new X_UIMsgBoxOk(tr("Tip"),tr("OpenPort COM failed"),this,1);
            pDlg->exec();
        }


    }
    else
    {
        sPortThread->ClosePort(comIndex);
        //参数设置使能
        ui->comboBox_SPortName->setEnabled(true);
        ui->comboBox_SPortBaud->setEnabled(true);
        ui->comboBox_SPortDataBit->setEnabled(true);
        ui->comboBox_SPortOEBit->setEnabled(true);
        ui->comboBox_SPortStopBit->setEnabled(true);
        ui->pushButtonMainOpen->setText(tr("Open"));
        //发送设置禁止
        ui->graphicsView_ComStatus->setStyleSheet("background-color: rgb(255, 0, 0);");
        ui->label_ComStatus->setText(tr("State:Off"));
    }
}
void mGuangYingPinWidget::MWOnoffPortAux(void)
{
    uint8_t comIndex = ui->comboBox_SPortNameAux->currentIndex();

    if(ui->pushButtonAuxOpen->text()==tr("Open"))
    {
        sPortThread->InitPortName(comIndex,ui->comboBox_SPortNameAux->currentText());
        sPortThread->InitPortBaudRate(comIndex,ui->comboBox_SPortBaudAux->currentText().toInt());

        //qDebug()<<ui->comboBox_SPortDataBit->currentIndex();
        switch(ui->comboBox_SPortDataBitAux->currentIndex())
        {
            case 0: sPortThread->InitPortDataBits(comIndex,QSerialPort::Data5); break;//0:5
            case 1: sPortThread->InitPortDataBits(comIndex,QSerialPort::Data6); break;//1:6
            case 2: sPortThread->InitPortDataBits(comIndex,QSerialPort::Data7); break;//2:7
            case 3: sPortThread->InitPortDataBits(comIndex,QSerialPort::Data8); break;//3:8
            default: break;
        }

        switch(ui->comboBox_SPortStopBitAux->currentIndex())
        {
            case 0:sPortThread->InitPortStopBits(comIndex,QSerialPort::OneStop);break;
            case 1:sPortThread->InitPortStopBits(comIndex,QSerialPort::TwoStop);break;
            case 2:sPortThread->InitPortStopBits(comIndex,QSerialPort::OneAndHalfStop);break;
            default: break;
        }

        switch(ui->comboBox_SPortOEBitAux->currentIndex())
        {
            case 0:sPortThread->InitPortParity(comIndex,QSerialPort::NoParity);break;
            case 1:sPortThread->InitPortParity(comIndex,QSerialPort::EvenParity);break;
            case 2:sPortThread->InitPortParity(comIndex,QSerialPort::OddParity);break;
            case 3:sPortThread->InitPortParity(comIndex,QSerialPort::SpaceParity);break;
            case 4:sPortThread->InitPortParity(comIndex,QSerialPort::MarkParity);break;
            default: break;
        }
        if(sPortThread->OpenPort(comIndex))
        {
            connect(&(sPortThread->m_arrSerial[comIndex]), SIGNAL(readyRead()), this, SLOT(MWReadDataAux()));

            //参数设置禁止
            ui->comboBox_SPortNameAux->setEnabled(false);
            ui->comboBox_SPortBaudAux->setEnabled(false);
            ui->comboBox_SPortDataBitAux->setEnabled(false);
            ui->comboBox_SPortOEBitAux->setEnabled(false);
            ui->comboBox_SPortStopBitAux->setEnabled(false);
            ui->pushButtonAuxOpen->setText(tr("Close"));
            //发送设置使能
            ui->graphicsView_ComStatusAux->setStyleSheet("background-color: rgb(0, 255, 0);");
            ui->label_ComStatusAux->setText(tr("State:On"));

        }
        else {
            //qDebug()<<tr("OpenPort COM")<<comIndex<<tr(":failed");
            X_UIMsgBoxOk *pDlg=new X_UIMsgBoxOk(tr("Tip"),tr("OpenPort COM failed"),this,1);
            pDlg->exec();

        }

    }
    else
    {
        sPortThread->ClosePort(comIndex);
        //参数设置使能
        ui->comboBox_SPortNameAux->setEnabled(true);
        ui->comboBox_SPortBaudAux->setEnabled(true);
        ui->comboBox_SPortDataBitAux->setEnabled(true);
        ui->comboBox_SPortOEBitAux->setEnabled(true);
        ui->comboBox_SPortStopBitAux->setEnabled(true);
        ui->pushButtonAuxOpen->setText(tr("Open"));
        //发送设置禁止
        ui->graphicsView_ComStatusAux->setStyleSheet("background-color: rgb(255, 0, 0);");
        ui->label_ComStatusAux->setText(tr("State:Off"));
    }
}
void mGuangYingPinWidget::Set_program_buf_and_send(int iComIndex,int iProgramIndex)
{
    unsigned char uctemp[3]={0};
    uctemp[0]=0x30+iProgramIndex;
    int m_uiResendTimes=ui->lineEdit_ResendTimes->text().toInt();
    int m_uimillisecond=ui->lineEdit_DelayMill->text().toInt();
    unsigned short iDelayTimes=m_uiResendTimes*m_uimillisecond;
    unsigned long  dwStart;
    unsigned long  dwStop;
    dwStart=QDateTime::currentDateTime().toMSecsSinceEpoch();
    dwStop=dwStart;
    for(int i=0;i<(int)m_uiResendTimes;i++)
    {
        int itiaozheng=dwStop-dwStart-m_uimillisecond;//时间发送间隔和参数发送间隔的差异
        if(itiaozheng<0)
            itiaozheng=0;
        unsigned short itemp=(iDelayTimes-i*m_uimillisecond)-itiaozheng;//补充差异，比如实际用了110毫秒，参数100毫秒，则发送过去的协议数据应该是让控制卡减少延时，所以在延时值上再减去补差
        if(itemp<0)
            itemp=0;
        dwStart=dwStop;
        memcpy(&uctemp[1],(unsigned char *)&itemp,2*sizeof(unsigned char));
            MWSendCmd(iComIndex,0x42,uctemp,3,g_iCountCmd);


        int itempDelay=0;
        if(itiaozheng>0)
            itempDelay=m_uimillisecond-itiaozheng;
        else
            itempDelay=m_uimillisecond;
        mymidelay(itempDelay);
        dwStop=QDateTime::currentDateTime().toMSecsSinceEpoch();
        if(ui->checkBoxDebug->isChecked())
        {
             ui->textEditReadBuf->append("dwStart="+QString::number(dwStart)+",dwStop="+QString::number(dwStop)+",dwStop-dwStart="+QString::number(dwStop-dwStart)+",Delay="+QString::number(itempDelay)+",buf delay value="+QString::number(itemp)+"\r\n");
        }
    }
    g_iCountCmd++;

}
void mGuangYingPinWidget::Set_program_buf_and_send_by_udp(int iProgramIndex)
{
    unsigned char uctemp[3]={0};
    uctemp[0]=0x30+iProgramIndex;
    int m_uiResendTimes=ui->lineEdit_ResendTimes->text().toInt();
    int m_uimillisecond=ui->lineEdit_DelayMill->text().toInt();
    unsigned short iDelayTimes=m_uiResendTimes*m_uimillisecond;
    unsigned long  dwStart;
    unsigned long  dwStop;
    dwStart=QDateTime::currentDateTime().toMSecsSinceEpoch();
    dwStop=dwStart;
    for(int i=0;i<(int)m_uiResendTimes;i++)
    {
        int itiaozheng=dwStop-dwStart-m_uimillisecond;//时间发送间隔和参数发送间隔的差异
        if(itiaozheng<0)
            itiaozheng=0;
        unsigned short itemp=(iDelayTimes-i*m_uimillisecond)-itiaozheng;//补充差异，比如实际用了110毫秒，参数100毫秒，则发送过去的协议数据应该是让控制卡减少延时，所以在延时值上再减去补差
        if(itemp<0)
            itemp=0;
        dwStart=dwStop;
        memcpy(&uctemp[1],(unsigned char *)&itemp,2*sizeof(unsigned char));
            MWSendCmdByUdp(0x42,uctemp,3,g_iCountCmd);


        int itempDelay=0;
        if(itiaozheng>0)
            itempDelay=m_uimillisecond-itiaozheng;
        else
            itempDelay=m_uimillisecond;
        mymidelay(itempDelay);
        dwStop=QDateTime::currentDateTime().toMSecsSinceEpoch();
        if(ui->checkBoxDebug->isChecked())
        {
             ui->textEditReadBuf->append("dwStart="+QString::number(dwStart)+",dwStop="+QString::number(dwStop)+",dwStop-dwStart="+QString::number(dwStop-dwStart)+",Delay="+QString::number(itempDelay)+",buf delay value="+QString::number(itemp)+"\r\n");
        }
    }
    g_iCountCmd++;

}
void mGuangYingPinWidget::Set_Brightness_buf_and_send(int iComIndex,int iBrightnessValue)
{
    unsigned char uctemp[2]={0};
    uctemp[0]=0xF1;
    uctemp[1]=iBrightnessValue;
    int m_uiResendTimes=ui->lineEdit_ResendTimes->text().toInt();
    int m_uimillisecond=ui->lineEdit_DelayMill->text().toInt();
    for(int i=0;i<(int)m_uiResendTimes;i++)
    {
        MWSendCmd(iComIndex,0x42,uctemp,2,g_iCountCmd);
        if(ui->checkBoxDebug->isChecked())
        {
             ui->textEditReadBuf->append("SetBrightNess="+QString::number(iBrightnessValue));
        }
        mymidelay(m_uimillisecond);
    }
}
void mGuangYingPinWidget::Set_Brightness_buf_and_send_by_udp(int iBrightnessValue)
{
    unsigned char uctemp[2]={0};
    uctemp[0]=0xF1;
    uctemp[1]=iBrightnessValue;
    int m_uiResendTimes=ui->lineEdit_ResendTimes->text().toInt();
    int m_uimillisecond=ui->lineEdit_DelayMill->text().toInt();
    for(int i=0;i<(int)m_uiResendTimes;i++)
    {
        MWSendCmdByUdp(0x42,uctemp,2,g_iCountCmd);
        if(ui->checkBoxDebug->isChecked())
        {
             ui->textEditReadBuf->append("SetBrightNess="+QString::number(iBrightnessValue));
        }
        mymidelay(m_uimillisecond);
    }
}
void mGuangYingPinWidget::Screen_OnOff_buf_and_send(int iComIndex,int iScreenOnOff)
{
    unsigned char uctemp[3]={0};
    uctemp[0]=iScreenOnOff;
    uctemp[1]=0;
    uctemp[2]=0;
    int m_uiResendTimes=ui->lineEdit_ResendTimes->text().toInt();
    int m_uimillisecond=ui->lineEdit_DelayMill->text().toInt();
    for(int i=0;i<(int)m_uiResendTimes;i++)
    {
        MWSendCmd(iComIndex,0x42,uctemp,3,g_iCountCmd);
        if(ui->checkBoxDebug->isChecked())
        {
            if(iScreenOnOff==0x11)
                ui->textEditReadBuf->append("Screen On");
            else
                ui->textEditReadBuf->append("Screen Off");
        }
        mymidelay(m_uimillisecond);
    }
}
void mGuangYingPinWidget::Screen_OnOff_buf_and_send_by_udp(int iScreenOnOff)
{
    unsigned char uctemp[3]={0};
    uctemp[0]=iScreenOnOff;
    uctemp[1]=0;
    uctemp[2]=0;
    int m_uiResendTimes=ui->lineEdit_ResendTimes->text().toInt();
    int m_uimillisecond=ui->lineEdit_DelayMill->text().toInt();
    for(int i=0;i<(int)m_uiResendTimes;i++)
    {
       MWSendCmdByUdp(0x42,uctemp,3,g_iCountCmd);
        if(ui->checkBoxDebug->isChecked())
        {
            if(iScreenOnOff==0x11)
                ui->textEditReadBuf->append("Screen On");
            else
                ui->textEditReadBuf->append("Screen Off");
        }
        mymidelay(m_uimillisecond);
    }
}
void mGuangYingPinWidget::NextProgram_buf_and_send(int iComIndex)
{
    unsigned char uctemp[1]={0};
    uctemp[0]=0xF3;
    int m_uiResendTimes=ui->lineEdit_ResendTimes->text().toInt();
    int m_uimillisecond=ui->lineEdit_DelayMill->text().toInt();
    for(int i=0;i<(int)m_uiResendTimes;i++)
    {
        MWSendCmd(iComIndex,0x42,uctemp,1,g_iCountCmd);
        if(ui->checkBoxDebug->isChecked())
        {
                ui->textEditReadBuf->append("Next Program");
        }
        mymidelay(m_uimillisecond);
    }
}
void mGuangYingPinWidget::NextProgram_buf_and_send_by_udp()
{
    unsigned char uctemp[1]={0};
    uctemp[0]=0xF3;
    int m_uiResendTimes=ui->lineEdit_ResendTimes->text().toInt();
    int m_uimillisecond=ui->lineEdit_DelayMill->text().toInt();
    for(int i=0;i<(int)m_uiResendTimes;i++)
    {
        MWSendCmdByUdp(0x42,uctemp,1,g_iCountCmd);
        if(ui->checkBoxDebug->isChecked())
        {
                ui->textEditReadBuf->append("Next Program");
        }
        mymidelay(m_uimillisecond);
    }
}
void mGuangYingPinWidget::mymidelay(int imlsec)
{
    unsigned long dwStart,dwStop;
    dwStart = QDateTime::currentDateTime().toMSecsSinceEpoch();
    do
    {
        dwStop = QDateTime::currentDateTime().toMSecsSinceEpoch();
    } while(dwStop-imlsec<dwStart);
}

void mGuangYingPinWidget::MWSendCmdByUdp(unsigned char cmd,unsigned char *pUcValue,unsigned int iLength,int iCountCmd)
{
    ST_ANSY_PROGRAM_PACKET tempStreadPakcet;
    tempStreadPakcet.SyncHead[0]=0x7e;
    tempStreadPakcet.SyncHead[1]=0x7e;
    tempStreadPakcet.SyncHead[2]=0x55;
    tempStreadPakcet.ucCommType=cmd;
    tempStreadPakcet.iBaoLiu=iCountCmd;
    tempStreadPakcet.iLength=iLength;
    memcpy(tempStreadPakcet.pDataBuffer,pUcValue,iLength);
    tempStreadPakcet.pDataBuffer[tempStreadPakcet.iLength]=GetCheckCodeIn8(&tempStreadPakcet.ucCommType,tempStreadPakcet.iLength+sizeof(tempStreadPakcet.iBaoLiu)+sizeof(tempStreadPakcet.ucCommType)+sizeof(tempStreadPakcet.iLength));
    int iLenPacket=3*sizeof(unsigned char)+sizeof(char)+sizeof(int)+sizeof(int)+tempStreadPakcet.iLength+sizeof(char);/////除正文外的协议结构大小；
    QByteArray databuf = QByteArray(reinterpret_cast<char*>(&tempStreadPakcet), iLenPacket);
    //sPortThread->SendDataBuf(iComIndex,databuf);


    QList<QNetworkInterface> networkinterfaces = QNetworkInterface::allInterfaces();
    foreach (QNetworkInterface interfaces, networkinterfaces) //networkinterfaces负责提供主机的IP地址和网络接口的列表
    {
            foreach (QNetworkAddressEntry entry, interfaces.addressEntries())//QNetworkAddressEntry存储了一个IP地址，子网掩码和广播地址
            {
                QHostAddress broadcastAddress("255.255.255.255");

                {
                    entry.setBroadcast(QHostAddress::Broadcast);
                    QUdpSocket *tempUdpSocket= new QUdpSocket(this);
                    if(!tempUdpSocket->bind(entry.ip()))
                        break;
                    int iSendLength=tempUdpSocket->writeDatagram((char *)&tempStreadPakcet,iLenPacket,broadcastAddress,31296);
                    if( iSendLength != iLenPacket)
                    {
//                        qDebug() << "error" << "RestoreLedCardIpByUdpCmd error!send restore led card ip by udp cmd";
//                       // return;
                        char *aa=reinterpret_cast<char*>(&tempStreadPakcet);
                        tempUdpSocket->writeDatagram(&aa[iSendLength],iLenPacket-iSendLength,broadcastAddress,31296);
                    }
                    else {

//                        strIpAddress =entry.ip().toString();// broadcastAddress.toString();
//                        qDebug() << strIpAddress << "send restore led card ip by udp cmd" ;
                    }
                    delete tempUdpSocket;
                }
        }
    }
    if(ui->checkBoxDebug->isChecked())
    {
         ui->textEditReadBuf->append("udp:"+tr(databuf.toHex()));
    }
}
void mGuangYingPinWidget::MWSendCmd(uint8_t iComIndex,unsigned char cmd,unsigned char *pUcValue,unsigned int iLength,int iCountCmd)
{
    ST_ANSY_PROGRAM_PACKET tempStreadPakcet;
    tempStreadPakcet.SyncHead[0]=0x7e;
    tempStreadPakcet.SyncHead[1]=0x7e;
    tempStreadPakcet.SyncHead[2]=0x55;
    tempStreadPakcet.ucCommType=cmd;
    tempStreadPakcet.iBaoLiu=iCountCmd;
    tempStreadPakcet.iLength=iLength;
    memcpy(tempStreadPakcet.pDataBuffer,pUcValue,iLength);
    tempStreadPakcet.pDataBuffer[tempStreadPakcet.iLength]=GetCheckCodeIn8(&tempStreadPakcet.ucCommType,tempStreadPakcet.iLength+sizeof(tempStreadPakcet.iBaoLiu)+sizeof(tempStreadPakcet.ucCommType)+sizeof(tempStreadPakcet.iLength));
    int iLenPacket=3*sizeof(unsigned char)+sizeof(char)+sizeof(int)+sizeof(int)+tempStreadPakcet.iLength+sizeof(char);/////除正文外的协议结构大小；
    QByteArray databuf = QByteArray(reinterpret_cast<char*>(&tempStreadPakcet), iLenPacket);
    sPortThread->SendDataBuf(iComIndex,databuf);
    if(ui->checkBoxDebug->isChecked())
    {
         ui->textEditReadBuf->append("com:"+tr(databuf.toHex()));
    }
//sPortThread->SendDataBuf(ui->comboBox_SPortName->currentIndex(),ui->textEdit->toPlainText().toLatin1());
//   m_sendNum +=  (ui->textEdit->toPlainText().toLatin1()).length();
//   MWRSNumDisplay();
}

void mGuangYingPinWidget::MWReadData(void)
{
    QByteArray buf;
    buf = sPortThread->GetDataBuf(ui->comboBox_SPortName->currentIndex());
    if(!buf.isEmpty())
    {
        QString str = tr(buf.toHex());
        if(ui->checkBoxDebug->isChecked())
        {
            ui->textEditReadBuf->moveCursor(QTextCursor::End);
               ui->textEditReadBuf->insertPlainText(str);
        }
        MWRSNumDisplay();
    }
    buf.clear();
}
void mGuangYingPinWidget::MWReadDataAux(void)
{
    QByteArray buf;
    buf = sPortThread->GetDataBuf(ui->comboBox_SPortNameAux->currentIndex());
    if(!buf.isEmpty())
    {
        QString str = tr(buf.toHex());
        if(ui->checkBoxDebug->isChecked())
        {
            ui->textEditReadBuf->moveCursor(QTextCursor::End);
               ui->textEditReadBuf->insertPlainText(str);
        }
        MWRSNumDisplay();
    }
    buf.clear();
}
QString mGuangYingPinWidget::ByteArrayToHexString(QByteArray &ba)
{
    QDataStream out(&ba,QIODevice::ReadWrite);   //将str的数据 读到out里面去
    QString buf;
    while(!out.atEnd())
    {
        qint8 outChar = 0;
        out >> outChar;   //每次一个字节的填充到 outchar
        QString str = QString("%1").arg(outChar&0xFF,2,16,QLatin1Char('0')).toUpper() + QString(" ");   //2 字符宽度
        buf += str;
    }
    return buf;
}
void mGuangYingPinWidget::MWClearRecv(void)
{
//    ui->textBrowser->clear();
//    m_recvNum = 0;
//    m_sendNum = 0;
//    MWRSNumDisplay();
}

void mGuangYingPinWidget::MWRSNumDisplay(void)
{
//    ui->label_RecvNum->setText(tr("已接收：") + QString::number(m_recvNum,10));
//    ui->label_SendNum->setText(tr("已发送：") + QString::number(m_sendNum,10));
}
