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. How to make QT automatically connect to correct COM Port

How to make QT automatically connect to correct COM Port

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.1k 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.
  • G Offline
    G Offline
    GunkutA
    wrote on last edited by
    #1

    I am trying to make QT automatically connect to my ST32 MCU by serial. MCU is keep sending data for each second. So I thought I can read all the available COM Ports and when the line is not null, I will detect that COM Port is MCU. But All COM Ports are null. Here is related piece of code I am using in QT:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QtSerialPort>
    #include <QDebug>
    QSerialPort *serial;
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        serial = new QSerialPort(this);
        foreach(QSerialPortInfo port, QSerialPortInfo::availablePorts())
        {
                serial->setPortName(port.portName());
                serial->setBaudRate(QSerialPort::Baud115200);
                serial->setDataBits(QSerialPort::Data8);
                serial->setParity(QSerialPort::NoParity);
                serial->setStopBits(QSerialPort::OneStop);
                serial->setFlowControl(QSerialPort::NoFlowControl);
                serial->open(QIODevice::ReadOnly);
     
                if(!(serial->readLine().isNull()))
                {
                    qDebug()<<port.portName()<<"is opened";
                }
     
        }
    }
    

    I believe 1 second is not enough for QT to detect that. Maybe I should use timeout in QT. Any idea how can I solve this problem?

    J.HilkJ 1 Reply Last reply
    0
    • G GunkutA

      I am trying to make QT automatically connect to my ST32 MCU by serial. MCU is keep sending data for each second. So I thought I can read all the available COM Ports and when the line is not null, I will detect that COM Port is MCU. But All COM Ports are null. Here is related piece of code I am using in QT:

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QtSerialPort>
      #include <QDebug>
      QSerialPort *serial;
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          serial = new QSerialPort(this);
          foreach(QSerialPortInfo port, QSerialPortInfo::availablePorts())
          {
                  serial->setPortName(port.portName());
                  serial->setBaudRate(QSerialPort::Baud115200);
                  serial->setDataBits(QSerialPort::Data8);
                  serial->setParity(QSerialPort::NoParity);
                  serial->setStopBits(QSerialPort::OneStop);
                  serial->setFlowControl(QSerialPort::NoFlowControl);
                  serial->open(QIODevice::ReadOnly);
       
                  if(!(serial->readLine().isNull()))
                  {
                      qDebug()<<port.portName()<<"is opened";
                  }
       
          }
      }
      

      I believe 1 second is not enough for QT to detect that. Maybe I should use timeout in QT. Any idea how can I solve this problem?

      J.HilkJ Online
      J.HilkJ Online
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @GunkutA
      point 1: you have no checks what so ever if the opening succeeds
      point 2: There is no guarantee that your serialport instance already received data after opening, in fact it's near impossible.

      point 3: PLEASE use the asynchronous api of QSerialPort with readyRead & bytesWritten Signals etc.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      G 1 Reply Last reply
      3
      • J.HilkJ J.Hilk

        @GunkutA
        point 1: you have no checks what so ever if the opening succeeds
        point 2: There is no guarantee that your serialport instance already received data after opening, in fact it's near impossible.

        point 3: PLEASE use the asynchronous api of QSerialPort with readyRead & bytesWritten Signals etc.

        G Offline
        G Offline
        GunkutA
        wrote on last edited by
        #3

        @J-Hilk Thank you for your feedback. To make things more clear:

        1. I checked and all ports are connected successfully.
        2. My MCU is trying to send data for every 1 second, so maybe I should check it with a Signal and Slot. But in this case how I will test it with all COM Ports and find the correct one?
        3. I don't know what is "asynchronous api of QSerialPort with readyRead & bytesWritten Signals etc.". I am newbie to QT and C++...
        jsulmJ 1 Reply Last reply
        0
        • G GunkutA

          @J-Hilk Thank you for your feedback. To make things more clear:

          1. I checked and all ports are connected successfully.
          2. My MCU is trying to send data for every 1 second, so maybe I should check it with a Signal and Slot. But in this case how I will test it with all COM Ports and find the correct one?
          3. I don't know what is "asynchronous api of QSerialPort with readyRead & bytesWritten Signals etc.". I am newbie to QT and C++...
          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @GunkutA said in How to make QT automatically connect to correct COM Port:

          I don't know what is "asynchronous api of QSerialPort with readyRead & bytesWritten Signals etc."

          Then please read documentation:
          https://doc.qt.io/qt-5/qserialport.html
          https://doc.qt.io/qt-5/qiodevice.html#readyRead
          https://doc.qt.io/qt-5/qiodevice.html#bytesWritten

          Qt is an asynchronous framework and should be used as such.

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

          1 Reply Last reply
          4

          • Login

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