Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Automatically connect to serial port

Automatically connect to serial port

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 784 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    valerio.j
    wrote on last edited by
    #1

    I have simple Qt app that talks to a micro-controller over the serial port using QSerailPort. In my source I open up the port automatically when the application is launched. Right now I open a specific comport as shown on the code below. What I'm trying to do is have the Qt app find the port automatically. The reason is that when I connect to a different computer or change USB port etc, I have to either change the Qt app to call out the correct port or change port name in device manager. What is the method to deal with this?

    void MainWindow::openSerialPort()
    {
    
        QString status_msg;
    
        serial->setPortName("COM3");
        serial->setBaudRate(QSerialPort::Baud19200);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        if (serial->open(QIODevice::ReadWrite))
        {
            qDebug() << "Serial Port :" << serial->portName() << "Open";
            QTextStream(&status_msg)<< serial->portName()<< " OPEN ";
            ui->statusBar->showMessage(status_msg);
        }
        else {
                ui->statusBar->showMessage(tr("PORT NOT OPENED !!! ERROR !!!"));
    //            QMessageBox::critical(this, tr("Error"), serial->errorString());
                //showStatusMessage(tr("Open error"));
            }
                qDebug() << "Name :" << serial->portName();
    }
    
    jsulmJ 1 Reply Last reply
    0
    • V valerio.j

      I have simple Qt app that talks to a micro-controller over the serial port using QSerailPort. In my source I open up the port automatically when the application is launched. Right now I open a specific comport as shown on the code below. What I'm trying to do is have the Qt app find the port automatically. The reason is that when I connect to a different computer or change USB port etc, I have to either change the Qt app to call out the correct port or change port name in device manager. What is the method to deal with this?

      void MainWindow::openSerialPort()
      {
      
          QString status_msg;
      
          serial->setPortName("COM3");
          serial->setBaudRate(QSerialPort::Baud19200);
          serial->setDataBits(QSerialPort::Data8);
          serial->setParity(QSerialPort::NoParity);
          serial->setStopBits(QSerialPort::OneStop);
          serial->setFlowControl(QSerialPort::NoFlowControl);
          if (serial->open(QIODevice::ReadWrite))
          {
              qDebug() << "Serial Port :" << serial->portName() << "Open";
              QTextStream(&status_msg)<< serial->portName()<< " OPEN ";
              ui->statusBar->showMessage(status_msg);
          }
          else {
                  ui->statusBar->showMessage(tr("PORT NOT OPENED !!! ERROR !!!"));
      //            QMessageBox::critical(this, tr("Error"), serial->errorString());
                  //showStatusMessage(tr("Open error"));
              }
                  qDebug() << "Name :" << serial->portName();
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @valerio-j said in Automatically connect to serial port:

      What is the method to deal with this?

      To make it configurable in the app. You can use QSettings to store and read your app settings.

      You also can use https://doc.qt.io/qt-5/qserialportinfo.html to iterate over all found ports and try to connect to each of them. But if more than one device is connected you still need a way to tell the app which one to use.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • V Offline
        V Offline
        valerio.j
        wrote on last edited by
        #3

        Ok thanks.
        I've done it using the prouct id and vendor id of the usb-to-serial device. It works pretty well. I might also include a search through the ports available, send a data and wait for some type of acknowledgement in the reply to verify the port selected.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved