Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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


  • Lifetime Qt Champion

    @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?


  • Lifetime Qt Champion

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

    m_availablePorts

    How do you initialise/fill it?



  • 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.


  • Lifetime Qt Champion

    @kkettinger Do you start your app with elevated permissions?



  • 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.


  • Lifetime Qt Champion

    @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?



  • 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.


Log in to reply