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

QSerialPort::open() returning permission error



  • Hi all -

    I recently discovered that a little used feature of an app is no longer working (sure would be nice to have regression testing).

    bool SerialPort::openPort(const QString &portName)
    {
        bool rc = false;
    
        do
        {
            if (portName.isEmpty())
            {
                continue;
            }
            if (m_serialPort.isOpen())
            {
                m_serialPort.close();  // in case we've opened a different one.
            }
            m_serialPort.setPortName(portName);
            if (!(rc = m_serialPort.open(QIODevice::ReadWrite)))
            {
                continue;
            }
    ...
    

    I get a QSerialPort::PermissionError on the open() attempt. I'm not aware of devices on Windows even having permissions...could this message mean something else?

    Nothing else (that is visible to me) ever uses this port.

    First noticed this in 5.15.0, but tried it with 5.14.2 and it fails there, too.

    Thanks for any ideas...


  • Lifetime Qt Champion

    Hi,

    You should print what QSerialPort::error returns if the open call fails.



  • @mzimmers
    After you have done as @SGaist says, https://forum.qt.io/topic/78678/qserialport-serialporterror-permissionerror says you can get permissions error under Windows if something else has it open, and another possibility might be it requires running elevated?



  • Is it possible a zombie process has the port open?



  • @SGaist I did leave some code out:

    connect(&m_serialPort, &QSerialPort::errorOccurred, this, &SerialPort::handleError);
    void SerialPort::handleError(QSerialPort::SerialPortError err)
    {
        qDebug() << "serial port error" << err << "encountered.";
    }
    

    And I added a line in here:

            if (!(rc = m_serialPort.open(QIODevice::ReadWrite)))
            {
                qDebug() << m_serialPort.error();
                continue;
            }
    

    When I run, I get this:

    serial port error QSerialPort::NoError encountered.
    serial port error QSerialPort::PermissionError encountered.
    QSerialPort::PermissionError
    

    I don't understand that first NoError line, but I confirmed that it's coming from the read(). My slot is hit twice on this read, which is odd.

    JonB: this used to work. I've closed all my other processes while running; nothing I'm doing is using that port.

    fcarney: highly, highly unlikely.

    Thanks...



  • @mzimmers I don't understand that first NoError line, but I confirmed that it's coming from the read(). My slot is hit twice on this read, which is odd.

    That line can come when you do not handle the NoError enum in a catch somewhere. It is usually when you initiate the connection and the startup of the com was a success.

    Permission error can come when you have not successfully closed the port, or when another program is using it. Have you checked if the com port is availiable from another software? serial monitor etc?

    Edit, seems I did not quite catch what I read;
    "JonB: this used to work. I've closed all my other processes while running; nothing I'm doing is using that port"

    I would still check it out with another serial monitor to test..



  • @MEMekaniske this isn't a conditional problem -- it happens first time, every time. But I'm curious as to what you'd expect to learn from a serial monitor?



  • @mzimmers If you're port is availiable there then the port should be availiable to QSerialPort too.

    Only time I've gotten that error is when I've forgot to close the connection in another software using the port.


  • Moderators

    @mzimmers can you verify, that it also happens with one of the examples, that fome with Qt?
    Can‘t test itvmyself right now

    https://doc.qt.io/qt-5/qtserialport-examples.html

    Would also help for a potential bugreport



  • @J-Hilk well, crud. I tried the terminal example, and it works perfectly.

    Its code and mine look practically identical, too. The only difference I see is the example explicitly sets a lot of parameters. I can try doing this, and see if it makes a difference, though how that would manifest as a permission error is beyond me.



  • @mzimmers Usually mismatch in the extra parameters only leads to wrong decoding



  • I tried the terminal program with this change:

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        MainWindow w2;
        w.show();
        w2.show();
        return a.exec();
    }
    

    Then on connecting a second time within the same program it shows a permission error message popup. Is it possible an object is getting created twice? Which might explain why you got SerialPort::handleError firing twice.



  • @fcarney I thought of that. I put breakpoints on the c'tor as well as the open() call; each only gets hit once, and I generate that error on the first call to open().



  • I found it -- totally my fault. I was opening ports in another thread, and not maintaining them well. Bad design, bad implementation.

    Thanks to everyone who looked.


Log in to reply