Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QSerialPortInfo doesn't return ports and connection doesn't work (Windows 10)
Forum Updated to NodeBB v4.3 + New Features

QSerialPortInfo doesn't return ports and connection doesn't work (Windows 10)

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 2 Posters 1.3k 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.
  • K Offline
    K Offline
    kkettinger
    wrote on last edited by kkettinger
    #1

    Hello,

    i'm currently developing an application with Qt Quick/QML using the QSerialPort und QSerialPortInfo library.

    My backend class is written in C++ and connected to QML with signal and slots. In my .pro file i've added "QT += serialport" and my backend class (.cpp) looks like this:

    #include <QSerialPort>
    #include <QSerialPortInfo>
    
    class {
    QList<QSerialPortInfo> m_availablePorts;
    ...
    void Backend::printAvailablePorts()
    {
       emit debugOutput("Available ports:");
        for (const auto &info : m_availablePorts)
        {
            emit debugOutput(info.portName());
        }
    }
    

    The class hierarchy is like this:

    1. Qt Quick/QML
    2. Backend_Adapter (Used as component block in QML)
    3. Backend_Worker (Thread which runs in Backend_Adapter for blocking operations, serial, ...)

    After sending the signal, i get no listing of ports. Even when running the application as administrator no ports are listed. When trying to connect to the port using the port name (COM7 e.g.), i get "Access denied" as error-string.

    The strange thing is, an application written as "Qt Core Application" (only console output) does list the ports and connection works as expected.

    What i am missing here in Qt Quick/QML?

    Thanks!

    Best regards,
    Kevin

    jsulmJ 1 Reply Last reply
    0
    • K kkettinger

      Yes, I directly start the .exe from the build folder (after using windeployqt.exe) with administrative right (Rightclick -> Run as administrator).

      The console app doesn't run with administrative right and still works though.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @kkettinger Are you sure you're not trying to open the port in your GUI app more than once?
      Did you compare your console app to your GUI app - maybe there are some differences in how you handle COM ports?

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

      1 Reply Last reply
      1
      • K kkettinger

        Hello,

        i'm currently developing an application with Qt Quick/QML using the QSerialPort und QSerialPortInfo library.

        My backend class is written in C++ and connected to QML with signal and slots. In my .pro file i've added "QT += serialport" and my backend class (.cpp) looks like this:

        #include <QSerialPort>
        #include <QSerialPortInfo>
        
        class {
        QList<QSerialPortInfo> m_availablePorts;
        ...
        void Backend::printAvailablePorts()
        {
           emit debugOutput("Available ports:");
            for (const auto &info : m_availablePorts)
            {
                emit debugOutput(info.portName());
            }
        }
        

        The class hierarchy is like this:

        1. Qt Quick/QML
        2. Backend_Adapter (Used as component block in QML)
        3. Backend_Worker (Thread which runs in Backend_Adapter for blocking operations, serial, ...)

        After sending the signal, i get no listing of ports. Even when running the application as administrator no ports are listed. When trying to connect to the port using the port name (COM7 e.g.), i get "Access denied" as error-string.

        The strange thing is, an application written as "Qt Core Application" (only console output) does list the ports and connection works as expected.

        What i am missing here in Qt Quick/QML?

        Thanks!

        Best regards,
        Kevin

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @kkettinger said in QSerialPortInfo doesn't return ports and connection doesn't work (Windows 10):

        m_availablePorts

        How do you initialise/fill it?

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

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kkettinger
          wrote on last edited by kkettinger
          #3

          Oh wow, i forget the initialization.. thanks. With m_availablePorts = QSerialPortInfo::availablePorts(); the port listing it is working now.

          For the connection part, i'm still getting "Access denied". The relevant code part:

          QSerialPort m_serialPort;
          ...
          void cBackendWorker::openSerialPort(
              QString serialPortName)
          {
              // ---- Port configuration
              m_serialPort.setPortName(serialPortName);
              m_serialPort.setBaudRate(QSerialPort::Baud9600);
          
              emit writeDebugOutput(QString("Open serial port %1...").arg(m_serialPort.portName()));
          
              // ---- Opening port
              if (m_serialPort.open(QIODevice::ReadWrite) == false)
              {
                  emit writeDebugOutput(QString("Failed to open port %1, error: %2").arg(m_serialPort.portName()).arg(m_serialPort.errorString()));
                  return;
              }
          
              emit writeDebugOutput("Port successfully opened");
          }
          

          This is the output i get:
          27-04-_2020_15-48-02.png

          Without m_serialPort.open(), busy is "No" for COM7. Other ports yield the same error string.

          Thanks.

          jsulmJ 1 Reply Last reply
          0
          • K kkettinger

            Oh wow, i forget the initialization.. thanks. With m_availablePorts = QSerialPortInfo::availablePorts(); the port listing it is working now.

            For the connection part, i'm still getting "Access denied". The relevant code part:

            QSerialPort m_serialPort;
            ...
            void cBackendWorker::openSerialPort(
                QString serialPortName)
            {
                // ---- Port configuration
                m_serialPort.setPortName(serialPortName);
                m_serialPort.setBaudRate(QSerialPort::Baud9600);
            
                emit writeDebugOutput(QString("Open serial port %1...").arg(m_serialPort.portName()));
            
                // ---- Opening port
                if (m_serialPort.open(QIODevice::ReadWrite) == false)
                {
                    emit writeDebugOutput(QString("Failed to open port %1, error: %2").arg(m_serialPort.portName()).arg(m_serialPort.errorString()));
                    return;
                }
            
                emit writeDebugOutput("Port successfully opened");
            }
            

            This is the output i get:
            27-04-_2020_15-48-02.png

            Without m_serialPort.open(), busy is "No" for COM7. Other ports yield the same error string.

            Thanks.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @kkettinger Do you start your app with elevated permissions?

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

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kkettinger
              wrote on last edited by
              #5

              Yes, I directly start the .exe from the build folder (after using windeployqt.exe) with administrative right (Rightclick -> Run as administrator).

              The console app doesn't run with administrative right and still works though.

              jsulmJ 1 Reply Last reply
              0
              • K kkettinger

                Yes, I directly start the .exe from the build folder (after using windeployqt.exe) with administrative right (Rightclick -> Run as administrator).

                The console app doesn't run with administrative right and still works though.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @kkettinger Are you sure you're not trying to open the port in your GUI app more than once?
                Did you compare your console app to your GUI app - maybe there are some differences in how you handle COM ports?

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

                1 Reply Last reply
                1
                • K Offline
                  K Offline
                  kkettinger
                  wrote on last edited by kkettinger
                  #7

                  Thank you jsulm, that was it. I accidentally had another backend instance for testing in the main.cpp, directly before using qmlRegisterType<BackendAdapter::cBackendAdapter>("com.ex.backend", 1, 0, "Backend");. This way two worker instances were instantiated and two emit signals were sent. But ony one was used in qml and was connected to the debug window, so i only saw one output.

                  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