#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "SerialTool/SerialTool.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setFocusPolicy(Qt::StrongFocus);
    ui->Serial_comb->clear();
    foreach(const QSerialPortInfo &Info,QSerialPortInfo::availablePorts())
    {
        QSerialPort availablePort;
        QString str = Info.portName();
        availablePort.setPortName(Info.portName());
        if(availablePort.open(QIODevice::ReadWrite))
        {
            ui->Serial_comb->addItem(Info.portName());
            availablePort.close();
        }
    }
    ui->Serial_comb->setCurrentIndex(0);
    portIsOpen = false;




}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_connectBtn1_clicked()
{
    QString PortName = ui->Serial_comb->currentText();
    if(portIsOpen == false)
    {
        //打开串口
        if(PortName == "")
        {
            QMessageBox::information(NULL, "Title", "请选择有效的串口号");
            return ;
        }
        if(m_comport.isOpen())
        {
            m_comport.close();
        }
        m_comport.setReadBufferSize(500);
        if(SerialTool::SerialOpen(&(m_comport),PortName,"9600","8","无校验","1")== false)
        {
          QMessageBox::information(NULL, "Title", "串口打开失败");
        }
        else
        {
           portIsOpen = true;
        }
    }
    else
    {//收到
       //关闭串口
        portIsOpen = false;
        if(m_comport.isOpen())
        {
            m_comport.close();
        }
    }
    if(portIsOpen == true)
    {
        ui->connectBtn1->setText("关闭");
    }
    else
    {
        ui->connectBtn1->setText("连接");
    }
}

void MainWindow::on_readGrossBtn_clicked()
{
    QByteArray readArr;
    int RXD_Count;
    INT8U RxdBuff[200];
    INT16U I_temp;
    int cmdLength;
    ui->resultEdt->setText("");
    if(portIsOpen == false)
    {
        QMessageBox::information(NULL, "Title", "串口无效");
        return;
    }
    //准备发送数据
    INT8U txdbuff[20] = {0x01,0x03,0x00,0x01,0x00,0x04,0x15,0xc9};
    txdbuff[0]= 0x01;
    txdbuff[1]= 0x03;
    txdbuff[2]= 0x00;
    txdbuff[3]= 0x01;
    txdbuff[4]= 0x00;
    txdbuff[5]= 0x04;
    cmdLength =6;
    //产生校验码
    I_temp = ToolFunc::crc16(txdbuff,cmdLength);
    txdbuff[6]= (INT8U)((I_temp>>8)&0x00ff);
    txdbuff[7] = (INT8U)I_temp;
    //
    SerialTool::sendBytes(&m_comport,txdbuff,8);
    QThread::msleep(100);
    readArr.clear();
    RXD_Count = 0;
    if (m_comport.waitForReadyRead(20)) {
        qApp->processEvents();
        readArr = m_comport.readAll();
        //qDebug()<<"readArr = serial.readAll();";
        }
    RXD_Count = readArr.count();
    if(RXD_Count<0)
     {
         QMessageBox::information(NULL, "Title", "无应答");
     }
    else  if(RXD_Count != 13)
    {
        QMessageBox::information(NULL, "Title", "通讯数据错误");
    }
    else
     {
          ToolFunc::ByteArrayTocharArrCopy(RxdBuff,readArr,RXD_Count);
          //通讯数据包处理
          I_temp = ToolFunc::crc16(RxdBuff,RXD_Count-2);
          if(((INT8U)((I_temp>>8)&0x00ff)==RxdBuff[RXD_Count-2])&&((INT8U)I_temp ==RxdBuff[RXD_Count-1]))
          {
               //数据解析
               int value;
               value = RxdBuff[9]-0x30;
               value =value+int(RxdBuff[8]-0x30)*10;
               value =value+int(RxdBuff[7]-0x30)*100;
               value =value+int(RxdBuff[6]-0x30)*1000;
               value =value+int(RxdBuff[5]-0x30)*10000;
               value =value+int(RxdBuff[4]-0x30)*100000;
               if(RxdBuff[3] == 0x2d)
               {
                   value = -value;
               }
               else
               {
                  value =value+int(RxdBuff[3]-0x30)*1000000;
               }
               if(value == -999999)
               {
                   ui->resultEdt->setText("异常-999999");
               }
               else
               {
                   float point;
                   if(RxdBuff[10] == 0x31)
                   {
                       point = 0.1;
                   }
                   else if(RxdBuff[10] == 0x32)
                   {
                       point = 0.01;
                   }
                   else if(RxdBuff[10] == 0x33)
                   {
                       point = 0.001;
                   }
                   else
                   {
                       point = 1;
                   }
                   float result;

                   if(value<0)
                   {
                       value = -value;
                       result = value;
                       result = result*point;
                       result = result*(-1);
                   }
                   else
                   {
                       result = value;
                       result = result*point;
                   }
                   ui->resultEdt->setText(QString("%1").arg(result));
               }

          }
          else
          {
             QMessageBox::information(NULL, "Title", "通讯数据错误");
          }
     }



}

void MainWindow::on_readZISNBtn_clicked()
{
    QByteArray readArr;
    int RXD_Count;
    INT8U RxdBuff[200];
    INT16U I_temp;
    int cmdLength;
    ui->resultEdt->setText("");
    if(portIsOpen == false)
    {
        QMessageBox::information(NULL, "Title", "串口无效");
        return;
    }
    //准备发送数据
    INT8U txdbuff[20];
    txdbuff[0]= 0x01;
    txdbuff[1]= 0x03;
    txdbuff[2]= 0x00;
    txdbuff[3]= 0x0b;
    txdbuff[4]= 0x00;
    txdbuff[5]= 0x04;
    cmdLength =6;
    //产生校验码
    I_temp = ToolFunc::crc16(txdbuff,cmdLength);
    txdbuff[6]= (INT8U)((I_temp>>8)&0x00ff);
    txdbuff[7] = (INT8U)I_temp;
    //
    SerialTool::sendBytes(&m_comport,txdbuff,8);
    QThread::msleep(100);
    readArr.clear();
    RXD_Count = 0;
    if(m_comport.waitForReadyRead(20)) {
        qApp->processEvents();
        readArr = m_comport.readAll();
        //qDebug()<<"readArr = serial.readAll();";
        }
    RXD_Count = readArr.count();
    if(RXD_Count<0)
     {
         QMessageBox::information(NULL, "Title", "无应答");
     }
    else  if(RXD_Count != 13)
    {
        QMessageBox::information(NULL, "Title", "通讯数据错误");
    }
    else
     {
          ToolFunc::ByteArrayTocharArrCopy(RxdBuff,readArr,RXD_Count);
          //通讯数据包处理
          I_temp = ToolFunc::crc16(RxdBuff,RXD_Count-2);
          if(((INT8U)((I_temp>>8)&0x00ff)==RxdBuff[RXD_Count-2])&&((INT8U)I_temp ==RxdBuff[RXD_Count-1]))
          {
               //数据解析
               int value;
               value = RxdBuff[9]-0x30;
               value =value+int(RxdBuff[8]-0x30)*10;
               value =value+int(RxdBuff[7]-0x30)*100;
               value =value+int(RxdBuff[6]-0x30)*1000;
               value =value+int(RxdBuff[5]-0x30)*10000;
               value =value+int(RxdBuff[4]-0x30)*100000;
               if(RxdBuff[3] == 0x2d)
               {
                   value = -value;
               }
               else
               {
                  value =value+int(RxdBuff[3]-0x30)*1000000;
               }
               if(value == -999999)
               {
                   ui->resultEdt->setText("异常-999999");
               }
               else
               {

                   float result;

                   if(value<0)
                   {
                       value = -value;
                       result = value;
                       result = result*(-1);
                   }
                   else
                   {
                       result = value;
                       result = result;
                   }
                   ui->resultEdt->setText(QString("%1").arg(result));
               }

          }
          else
          {
             QMessageBox::information(NULL, "Title", "通讯数据错误");
          }
     }



}

void MainWindow::on_zeroBtn_clicked()
{
    QByteArray readArr;
    int RXD_Count;
    INT8U RxdBuff[200];
    INT16U I_temp;
    int cmdLength;
    ui->resultEdt->setText("");
    if(portIsOpen == false)
    {
        QMessageBox::information(NULL, "Title", "串口无效");
        return;
    }
    //准备发送数据
    INT8U txdbuff[20];
    txdbuff[0]= 0x01;
    txdbuff[1]= 0x06;
    txdbuff[2]= 0x00;
    txdbuff[3]= 0x01;
    txdbuff[4]= 0x00;
    txdbuff[5]= 0x17;
    cmdLength =6;
    //产生校验码
    I_temp = ToolFunc::crc16(txdbuff,cmdLength);
    txdbuff[6]= (INT8U)((I_temp>>8)&0x00ff);
    txdbuff[7] = (INT8U)I_temp;
    //
    SerialTool::sendBytes(&m_comport,txdbuff,8);
    QThread::msleep(100);
    readArr.clear();
    RXD_Count = 0;
    if(m_comport.waitForReadyRead(20)) {
        qApp->processEvents();
        readArr = m_comport.readAll();
        //qDebug()<<"readArr = serial.readAll();";
        }
    RXD_Count = readArr.count();
    if(RXD_Count<0)
     {
         QMessageBox::information(NULL, "Title", "无应答");
     }
    else  if(RXD_Count != 8)
    {
        QMessageBox::information(NULL, "Title", "通讯数据错误");
    }
    else
     {
          ToolFunc::ByteArrayTocharArrCopy(RxdBuff,readArr,RXD_Count);
          //通讯数据包处理
          I_temp = ToolFunc::crc16(RxdBuff,RXD_Count-2);
          if(((INT8U)((I_temp>>8)&0x00ff)==RxdBuff[RXD_Count-2])&&((INT8U)I_temp ==RxdBuff[RXD_Count-1]))
          {
               QMessageBox::information(NULL, "Title", "通讯成功");

          }
          else
          {
             QMessageBox::information(NULL, "Title", "通讯数据错误");
          }
     }



}
