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. QSerialPort::open() returning permission error
QtWS25 Last Chance

QSerialPort::open() returning permission error

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 6 Posters 4.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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

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

    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      mzimmersM 1 Reply Last reply
      1
      • mzimmersM mzimmers

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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by
          #4

          Is it possible a zombie process has the port open?

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

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

            mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by
            #5

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

            M 1 Reply Last reply
            0
            • mzimmersM mzimmers

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

              M Offline
              M Offline
              MEMekaniske
              wrote on last edited by MEMekaniske
              #6

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

              mzimmersM 1 Reply Last reply
              0
              • M MEMekaniske

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

                mzimmersM Offline
                mzimmersM Offline
                mzimmers
                wrote on last edited by
                #7

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

                M J.HilkJ 2 Replies Last reply
                0
                • mzimmersM mzimmers

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

                  M Offline
                  M Offline
                  MEMekaniske
                  wrote on last edited by MEMekaniske
                  #8

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

                  1 Reply Last reply
                  0
                  • mzimmersM mzimmers

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

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #9

                    @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


                    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.

                    mzimmersM 1 Reply Last reply
                    0
                    • J.HilkJ J.Hilk

                      @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

                      mzimmersM Offline
                      mzimmersM Offline
                      mzimmers
                      wrote on last edited by
                      #10

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

                      M 1 Reply Last reply
                      0
                      • mzimmersM mzimmers

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

                        M Offline
                        M Offline
                        MEMekaniske
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0
                        • fcarneyF Offline
                          fcarneyF Offline
                          fcarney
                          wrote on last edited by
                          #12

                          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.

                          C++ is a perfectly valid school of magic.

                          mzimmersM 1 Reply Last reply
                          2
                          • fcarneyF fcarney

                            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.

                            mzimmersM Offline
                            mzimmersM Offline
                            mzimmers
                            wrote on last edited by
                            #13

                            @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().

                            1 Reply Last reply
                            0
                            • mzimmersM Offline
                              mzimmersM Offline
                              mzimmers
                              wrote on last edited by
                              #14

                              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.

                              1 Reply Last reply
                              3

                              • Login

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