วันอังคารที่ 14 กุมภาพันธ์ พ.ศ. 2560

Check Comport

quint16 serial_usb_vendor_id;
quint16 serial_usb_product_id;
QString vendor = "D:/vendor.txt";
bool chk_status_comport=true;


ui->setup

timer_A = new QTimer(this);
connect(timer_A,SIGNAL(timeout()),this,SLOT(check_usb_comport()));
timer_A->start(100);


serial = new QSerialPort(this);
connect(serial,SIGNAL(readyRead()),this,SLOT(readData()));
ui->bt_connect->click();


void MainWindow::check_usb_comport()
{

//    if(serial->isOpen())
//    {
//        serial->close();
//        ui->bt_connect->setText("Connect");
//    }

//    else
//    {
//        serial->setPortName("COM21");
//        serial->setBaudRate(QSerialPort::Baud115200);
//        serial->setDataBits(QSerialPort::Data8);
//        serial->setFlowControl(QSerialPort::NoFlowControl);
//        serial->setParity(QSerialPort::EvenParity);
//        serial->setStopBits(QSerialPort::OneStop);

//        if( serial->open(QIODevice::ReadWrite))
//        {
//            ui->bt_connect->setText("Disconnect");
//        }

//        else
//        {
//            // QMessageBox::critical(this,tr("Error"),serial->errorString());
//            QMessageBox::information(this,"Wrong com port" ,"Please check comport");
//        }
//    }

    if ( chk_status_comport == true)
    {
      bool serial_is_available = false;
      ui->btn_read_vendor_file->click();
      QString serial_uno_port_name;

      foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts())
      {
        if (serialPortInfo.hasProductIdentifier() && serialPortInfo.hasVendorIdentifier()) {
          if ((serialPortInfo.productIdentifier() == serial_usb_product_id)
              && (serialPortInfo.vendorIdentifier() == serial_usb_vendor_id)) {
            serial_is_available = true; //    serial uno is available on this port
            serial_uno_port_name = serialPortInfo.portName();
          }
        }
      }


      if (serial_is_available)
      {
        //qDebug() << "++++++++++++++++++++++++++++++++++";

        serial->setPortName(serial_uno_port_name);
        serial->open(QSerialPort::ReadWrite);
        serial->setBaudRate(QSerialPort::Baud115200);
        serial->setDataBits(QSerialPort::Data8);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setParity(QSerialPort::EvenParity);
         ui->label_status_comport->setText("Comport Connected");

      }

      else {
          qDebug() << "------------------------------------------";



        ui->label_status_comport->show();
        ui->label_status_comport->setText("Unable to Commicate with PLC");
        //QMessageBox::information(this, "Serial Port Error", "Unable to Commicate with MCU.");

        chk_status_comport = false;
      }
    }

    else {
      ui->btn_read_vendor_file->click();
      QString serial_uno_port_name;

      foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts())
      {
        //  check if the serialport has both a product identifier and a vendor identifier
        if (serialPortInfo.hasProductIdentifier() && serialPortInfo.hasVendorIdentifier()) {
          if ((serialPortInfo.productIdentifier() == serial_usb_product_id)
              && (serialPortInfo.vendorIdentifier() == serial_usb_vendor_id)) {   //  serial_is_available = true;
            serial_uno_port_name = serialPortInfo.portName();
          }
        }
      }


      if (serial->isOpen())
      {
        serial->close();
      }

      else
      {
        serial->setPortName(serial_uno_port_name);

        serial->setBaudRate(QSerialPort::Baud115200);
        serial->setDataBits(QSerialPort::Data8);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        serial->setParity(QSerialPort::EvenParity);

        if ( serial->open(QIODevice::ReadWrite))
        {
          //qDebug() << "==========================";

          chk_status_comport = true;
          ui->label_status_comport->show();
          ui->label_status_comport->setText("Comport Connected");
        }

        else
        {
          ui->label_status_comport->show();
          ui->label_status_comport->setText("Unable to Commicate with PLC");
          //QMessageBox::information(this, "Serial Port Error", "Unable to Commicate with MCU.");

        }
      }
    }


}

0 ความคิดเห็น:

แสดงความคิดเห็น

สมัครสมาชิก ส่งความคิดเห็น [Atom]

<< หน้าแรก