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 permissions problem (No. Not the easy problem)
Qt 6.11 is out! See what's new in the release blog

QSerialPort permissions problem (No. Not the easy problem)

Scheduled Pinned Locked Moved Unsolved General and Desktop
qserialport
17 Posts 6 Posters 7.3k Views 2 Watching
  • 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.
  • D Offline
    D Offline
    dhkaplan
    wrote on last edited by dhkaplan
    #1

    Qt 11.2 on CentOS 7. I'm having a problem with permissions on a set of /dev/ttyUSB? devices while running Qt apps. They act like a user somehow gets an invisible exclusive ownership of a device which prevents other users from using the device, errno is set to EPERM (operation not permitted). The devices are all crw-rw-rw-, and all the users are in the dialout group. This problem persists even after unplugging/plugging the device, reboots, and power cycles. Non Qt apps like screen have no problems accessing the devices.

    More information: I wrote a stub class TSerialPort : public QIODevice { } which uses calls ::open() to open the serial device. This has no problem, so I know that the base functionality of QIODevice isn't the problem.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      See https://doc.qt.io/qt-5/qserialport.html#details

      Note: The serial port is always opened with exclusive access (that is, no other process or thread can access an already opened serial port).
      

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      D 1 Reply Last reply
      6
      • Christian EhrlicherC Christian Ehrlicher

        See https://doc.qt.io/qt-5/qserialport.html#details

        Note: The serial port is always opened with exclusive access (that is, no other process or thread can access an already opened serial port).
        
        D Offline
        D Offline
        dhkaplan
        wrote on last edited by
        #3

        @Christian-Ehrlicher This is not an problem with accessing the ports at the same time. But this brings up a question, how is this exclusivity implemented? Is it possible that a lock is left hanging if the a program exits improperly?

        aha_1980A 1 Reply Last reply
        0
        • D dhkaplan

          @Christian-Ehrlicher This is not an problem with accessing the ports at the same time. But this brings up a question, how is this exclusivity implemented? Is it possible that a lock is left hanging if the a program exits improperly?

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @dhkaplan

          Is it possible that a lock is left hanging if the a program exits improperly?

          Could be, QSP uses lock files. Does your app exit improperly?

          Qt has to stay free or it will die.

          1 Reply Last reply
          1
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @dhkaplan said in QSerialPort permissions problem (No. Not the easy problem):

            Is it possible that a lock is left hanging if the a program exits improperly?

            At least on linux yes - see /tmp/lockTTYS0 (or something similar - don't remember exactly the naming of the file but it's obvious when you see it).

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            D 1 Reply Last reply
            3
            • Christian EhrlicherC Christian Ehrlicher

              @dhkaplan said in QSerialPort permissions problem (No. Not the easy problem):

              Is it possible that a lock is left hanging if the a program exits improperly?

              At least on linux yes - see /tmp/lockTTYS0 (or something similar - don't remember exactly the naming of the file but it's obvious when you see it).

              D Offline
              D Offline
              dhkaplan
              wrote on last edited by
              #6

              @Christian-Ehrlicher Ah ha! There they are. Are they left hanging if the devices are not explicitly closed before the program exits?

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                I think they're created as QTemporaryFile so they should only be there when the program crashes - at least I would expect this.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                D 1 Reply Last reply
                2
                • Christian EhrlicherC Christian Ehrlicher

                  I think they're created as QTemporaryFile so they should only be there when the program crashes - at least I would expect this.

                  D Offline
                  D Offline
                  dhkaplan
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher Apparently, the lock file is removed only if the QSeialPort::close() method is called, not if implicitly closed by (proper) program termination. I'd prefer it if this lock feature were optional.

                  aha_1980A K 2 Replies Last reply
                  0
                  • D Offline
                    D Offline
                    dhkaplan
                    wrote on last edited by
                    #9

                    OK I know how to deal with this now. Thanks for your help.

                    1 Reply Last reply
                    0
                    • Kent-DorfmanK Offline
                      Kent-DorfmanK Offline
                      Kent-Dorfman
                      wrote on last edited by
                      #10

                      @dhkaplan said in QSerialPort permissions problem (No. Not the easy problem):

                      Apparently, the lock file is removed only if the QSeialPort::close() method is called, not if implicitly closed by (proper) program termination. I'd prefer it if this lock feature were optional.

                      And keep in mind that lock files implemented by the QSerialPort are specific to Qt, and NOT Linux system level..IOW, they only protect exclusive access in Qt applications.

                      The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

                      D 1 Reply Last reply
                      1
                      • Kent-DorfmanK Kent-Dorfman

                        @dhkaplan said in QSerialPort permissions problem (No. Not the easy problem):

                        Apparently, the lock file is removed only if the QSeialPort::close() method is called, not if implicitly closed by (proper) program termination. I'd prefer it if this lock feature were optional.

                        And keep in mind that lock files implemented by the QSerialPort are specific to Qt, and NOT Linux system level..IOW, they only protect exclusive access in Qt applications.

                        D Offline
                        D Offline
                        dhkaplan
                        wrote on last edited by
                        #11

                        @Kent-Dorfman That's correct. No issues with non-Qt access to the serial ports.

                        1 Reply Last reply
                        0
                        • D dhkaplan

                          @Christian-Ehrlicher Apparently, the lock file is removed only if the QSeialPort::close() method is called, not if implicitly closed by (proper) program termination. I'd prefer it if this lock feature were optional.

                          aha_1980A Offline
                          aha_1980A Offline
                          aha_1980
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @dhkaplan

                          Apparently, the lock file is removed only if the QSeialPort::close() method is called, not if implicitly closed by (proper) program termination.

                          If that's the case, then it would be a bug. Because the design is as follows:

                          The lock file is removed in QSerialPortPrivate::close(), which is called from QSerialPort::close() which is called from the destructor ~QSerialPort()

                          Result: the lock file should be removed when the serial port object is removed.

                          Regards

                          Qt has to stay free or it will die.

                          1 Reply Last reply
                          2
                          • Kent-DorfmanK Offline
                            Kent-DorfmanK Offline
                            Kent-Dorfman
                            wrote on last edited by Kent-Dorfman
                            #13

                            Along the lines of serial port control in Linux, nothing says you HAVE to use the Qt classes for serial access. I usually just do POSIX programming in a separate thread with its own read loop and select() to read data when it's available...because I'm much familiar more with POSIX system programming. It's then not too hard to make the data available to the GUI thread via an IPC mechanism.

                            The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

                            aha_1980A 1 Reply Last reply
                            0
                            • Kent-DorfmanK Kent-Dorfman

                              Along the lines of serial port control in Linux, nothing says you HAVE to use the Qt classes for serial access. I usually just do POSIX programming in a separate thread with its own read loop and select() to read data when it's available...because I'm much familiar more with POSIX system programming. It's then not too hard to make the data available to the GUI thread via an IPC mechanism.

                              aha_1980A Offline
                              aha_1980A Offline
                              aha_1980
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @Kent-Dorfman

                              I usually just do POSIX programming in a separate thread

                              Yeah, but that's not cross-platform ;)

                              Qt has to stay free or it will die.

                              1 Reply Last reply
                              0
                              • Kent-DorfmanK Offline
                                Kent-DorfmanK Offline
                                Kent-Dorfman
                                wrote on last edited by
                                #15

                                @aha_1980 said in QSerialPort permissions problem (No. Not the easy problem):

                                Yeah, but that's not cross-platform ;)

                                Op said he's working on Linux. Actually, I usually recommend manager daemons for access to I/O devices and use of IPC to communicate with them. By abstracting in that way the benefits are: much easier to unit test and validate, and the ability to trivially swap to different IO mechanisms. If your Serial daemon reships the data as a TCP stream then your clients only need to implement a TCP client handler...and guess what? Your serial data no longer needs to reside on the local machine. Then there is GPIO...NEVER give a userland client program direct access to GPIO. Always run it through a manager so that the client can only fiddle with the channels intended, and in a way that doesn't break things.

                                Anyway, more than enough comment to spawn a suitable flame war, so I digres.

                                The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

                                JonBJ 1 Reply Last reply
                                0
                                • D dhkaplan

                                  @Christian-Ehrlicher Apparently, the lock file is removed only if the QSeialPort::close() method is called, not if implicitly closed by (proper) program termination. I'd prefer it if this lock feature were optional.

                                  K Offline
                                  K Offline
                                  kuzulis
                                  Qt Champions 2020
                                  wrote on last edited by
                                  #16

                                  @dhkaplan said in QSerialPort permissions problem (No. Not the easy problem):

                                  I'd prefer it if this lock feature were optional.

                                  Lock files it is a common practicle. Besides, it gives same behavior on different platforms (as on Windows the serial ports opened only with exclusive access). And I can't imagine at all a situation when needs an access to the same serial port from different processes.

                                  1 Reply Last reply
                                  2
                                  • Kent-DorfmanK Kent-Dorfman

                                    @aha_1980 said in QSerialPort permissions problem (No. Not the easy problem):

                                    Yeah, but that's not cross-platform ;)

                                    Op said he's working on Linux. Actually, I usually recommend manager daemons for access to I/O devices and use of IPC to communicate with them. By abstracting in that way the benefits are: much easier to unit test and validate, and the ability to trivially swap to different IO mechanisms. If your Serial daemon reships the data as a TCP stream then your clients only need to implement a TCP client handler...and guess what? Your serial data no longer needs to reside on the local machine. Then there is GPIO...NEVER give a userland client program direct access to GPIO. Always run it through a manager so that the client can only fiddle with the channels intended, and in a way that doesn't break things.

                                    Anyway, more than enough comment to spawn a suitable flame war, so I digres.

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

                                    @Kent-Dorfman
                                    Out of curiosity, and not wanting to start a flame war, I'm slightly surprised that you ship I/O access out-of-process. It may well be convenient for debugging/testing, but for production? Then again I know nothing about hardware, so you may so that serial port top speed is slow it doesn't matter, I don't know....

                                    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