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. Turning off power to a running QSerialPort
Forum Update on Monday, May 27th 2025

Turning off power to a running QSerialPort

Scheduled Pinned Locked Moved Unsolved General and Desktop
qserialportsegfaultasynchronous
14 Posts 6 Posters 3.5k 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.
  • S Offline
    S Offline
    sykac
    wrote on 22 Sept 2018, 13:23 last edited by sykac
    #1

    Hello,
    I'm developing an application with several serial ports. Each of these ports is handled by a different thread and has its own QSerialPort object. From a hardware point of view, they are connected hierarchicaly, meaning that there is one main device connected to the PC with a usb cable (1 COM port), to this main device there are several other devices connected, each of them having its own COM port. The main device can turn on/off the power supply to these child ports.

    In the application, the ports are handled asynchronously. Each device object is running in its own loop. If its port is opened, it reads the incoming data. If the port is closed, it tries to open it in every loop until it succeeds. Each QSerialPort object handles errors on the errorOccurred signal. If it receives DeviceNotFoundError, PermissionError, ResourceError error, the port is closed (if it was opened) and the looping continues as described above.

    The problem is that this serial communication part of the application is crashing (segmentation fault). I spent days finding the issue but with no results so far. To better understand what is going on, I wanted to ask here. Could it be a problem for QSerialPort if the main device turns off the power supply for the child ports while they're opened and are working? Or if the power supply is turned off while the child ports are being opened/closed or any other operation is being executed on them?

    Thanks for any help or ideas!

    J 1 Reply Last reply 23 Sept 2018, 09:32
    0
    • M Offline
      M Offline
      mrdebug
      wrote on 22 Sept 2018, 17:48 last edited by
      #2

      I'm not sure to having understood.
      Is power off a child port similar to remove a usb / ftdi adapter? If yes it is normal to having the QSerialPort object goes to crash.
      I suggest you to implement directly the libusb or better, send a command to power off but, the device has to execute it later, after the serial port clouse procedure.

      Need programmers to hire?
      www.labcsp.com
      www.denisgottardello.it
      GMT+1
      Skype: mrdebug

      1 Reply Last reply
      0
      • S sykac
        22 Sept 2018, 13:23

        Hello,
        I'm developing an application with several serial ports. Each of these ports is handled by a different thread and has its own QSerialPort object. From a hardware point of view, they are connected hierarchicaly, meaning that there is one main device connected to the PC with a usb cable (1 COM port), to this main device there are several other devices connected, each of them having its own COM port. The main device can turn on/off the power supply to these child ports.

        In the application, the ports are handled asynchronously. Each device object is running in its own loop. If its port is opened, it reads the incoming data. If the port is closed, it tries to open it in every loop until it succeeds. Each QSerialPort object handles errors on the errorOccurred signal. If it receives DeviceNotFoundError, PermissionError, ResourceError error, the port is closed (if it was opened) and the looping continues as described above.

        The problem is that this serial communication part of the application is crashing (segmentation fault). I spent days finding the issue but with no results so far. To better understand what is going on, I wanted to ask here. Could it be a problem for QSerialPort if the main device turns off the power supply for the child ports while they're opened and are working? Or if the power supply is turned off while the child ports are being opened/closed or any other operation is being executed on them?

        Thanks for any help or ideas!

        J Offline
        J Offline
        JKSH
        Moderators
        wrote on 23 Sept 2018, 09:32 last edited by
        #3

        @sykac said in Turning off power to a running QSerialPort:

        Could it be a problem for QSerialPort if the main device turns off the power supply for the child ports while they're opened and are working? Or if the power supply is turned off while the child ports are being opened/closed or any other operation is being executed on them?

        I believe it heavily depends on the drivers for those serial ports. A good driver shouldn't cause software to crash when the connected device loses power.

        1. How does your PC know about the "child" ports?
        2. To understand your problem better... are you saying that the crashes only occur if you cut power?

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        2
        • S Offline
          S Offline
          sykac
          wrote on 1 Oct 2018, 14:04 last edited by
          #4

          @mrdebug By power off I mean literally switching off electricity.

          @JKSH 1. The main device has a USB hub. To this hub there are 6 ports connected.
          2. I don't have answer to this question. I don't know when the application crashes exactly, I'm just sure it's because of the serial communication and I'm trying to find out what could be a problem for QSerialPort.

          I'm sorry for this abstract question but I have troubles understanding the problem myself.

          J 1 Reply Last reply 1 Oct 2018, 15:01
          0
          • S sykac
            1 Oct 2018, 14:04

            @mrdebug By power off I mean literally switching off electricity.

            @JKSH 1. The main device has a USB hub. To this hub there are 6 ports connected.
            2. I don't have answer to this question. I don't know when the application crashes exactly, I'm just sure it's because of the serial communication and I'm trying to find out what could be a problem for QSerialPort.

            I'm sorry for this abstract question but I have troubles understanding the problem myself.

            J Offline
            J Offline
            JKSH
            Moderators
            wrote on 1 Oct 2018, 15:01 last edited by
            #5

            @sykac said in Turning off power to a running QSerialPort:

            1. The main device has a USB hub. To this hub there are 6 ports connected.

            Is the hub externally powered? If not, there's a small chance you might get a power overload.

            P.S. Earlier you mentioned "there is one main device connected to the PC with a usb cable (1 COM port)". Note that a USB hub only has USB ports -- it has no COM ports.

                       2. I don't have answer to this question. I don't know when the application crashes exactly, I'm just sure it's because of the serial communication and I'm trying to find out what could be a problem for QSerialPort. 
            

            I'm sorry for this abstract question but I have troubles understanding the problem myself.

            Unfortunately, I can't do much with such a vague description.

            You need to spend time troubleshooting. For example:

            • See if you can gradually remove code from other parts of your application and still cause the crash.
            • See if the crashes occur when you only have 1 instead of 6 serial devices.
            • See if a different hub causes the crashes.
            • See if a dedicated multi-serial-port hub causes the crashes: https://www.moxa.com/product/UPort_1610-8.htm

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            2
            • M Offline
              M Offline
              mrdebug
              wrote on 2 Oct 2018, 07:06 last edited by
              #6

              If you unplug the device when the serial port is open it is normal to have a crash.
              It is similar to remove an usb pen drive when you are working on a file.
              The os could have a problem with device handle, for example it can assign a diferent description to the device when you replug it.

              Need programmers to hire?
              www.labcsp.com
              www.denisgottardello.it
              GMT+1
              Skype: mrdebug

              1 Reply Last reply
              1
              • J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 2 Oct 2018, 07:14 last edited by J.Hilk 10 Feb 2018, 07:35
                #7

                you might be doing something wrong in your threads.

                I have an application that is running a QSerialPort in a thread, with constant data polling, and if I unplug the usb/COM-Port dongle

                I get simply an

                QModbusDevice::Error(ConnectionError) "Resource error."

                to what I react to.


                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.

                1 Reply Last reply
                2
                • aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on 2 Oct 2018, 07:26 last edited by
                  #8

                  Just to add my experience to @mrdebug and @J-Hilk:

                  I sometimes have problems with electrical arcs in our devices. If a computer is connected through USB (with FTDI USB-serial converter), anything can happen: often I'm unable to close and re-open the serial port. It seems this is dependend on the device driver.

                  In most cases it helps to disconnect and reconnect the USB cable, but I also remember cases where a PC reboot was needed.

                  Qt has to stay free or it will die.

                  S 1 Reply Last reply 5 Oct 2018, 08:47
                  0
                  • M Offline
                    M Offline
                    MrShawn
                    wrote on 3 Oct 2018, 17:32 last edited by
                    #9

                    Make sure you don't have any dangling pointers. Usually my programs are crashing when I do something stupid with a pointer. If you have multiple threads you are likely doing things with pointers somewhere, and you may not have been as careful as you think you were. I'd run in debug mode and see if any insight is provided.

                    1 Reply Last reply
                    0
                    • aha_1980A aha_1980
                      2 Oct 2018, 07:26

                      Just to add my experience to @mrdebug and @J-Hilk:

                      I sometimes have problems with electrical arcs in our devices. If a computer is connected through USB (with FTDI USB-serial converter), anything can happen: often I'm unable to close and re-open the serial port. It seems this is dependend on the device driver.

                      In most cases it helps to disconnect and reconnect the USB cable, but I also remember cases where a PC reboot was needed.

                      S Offline
                      S Offline
                      sykac
                      wrote on 5 Oct 2018, 08:47 last edited by
                      #10

                      @aha_1980 That's my case, some of my devices are using ftdi usb-serial converter. What exactly do you mean device drivers? I just manually downloaded ftdi drivers from the official web. I thought I couldn't do anything wrong with that, I just downloaded and installed the driver.

                      So you are saying it is not solveable? It just happens with this kind of HW?

                      J J 2 Replies Last reply 5 Oct 2018, 08:55
                      0
                      • S sykac
                        5 Oct 2018, 08:47

                        @aha_1980 That's my case, some of my devices are using ftdi usb-serial converter. What exactly do you mean device drivers? I just manually downloaded ftdi drivers from the official web. I thought I couldn't do anything wrong with that, I just downloaded and installed the driver.

                        So you are saying it is not solveable? It just happens with this kind of HW?

                        J Offline
                        J Offline
                        J.Hilk
                        Moderators
                        wrote on 5 Oct 2018, 08:55 last edited by
                        #11

                        @sykac where does your program actually crash!? Have you run your program with the debugger attached yet? If yes, what does the stacktrace say?


                        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.

                        1 Reply Last reply
                        1
                        • S sykac
                          5 Oct 2018, 08:47

                          @aha_1980 That's my case, some of my devices are using ftdi usb-serial converter. What exactly do you mean device drivers? I just manually downloaded ftdi drivers from the official web. I thought I couldn't do anything wrong with that, I just downloaded and installed the driver.

                          So you are saying it is not solveable? It just happens with this kind of HW?

                          J Offline
                          J Offline
                          JKSH
                          Moderators
                          wrote on 5 Oct 2018, 09:21 last edited by
                          #12

                          @sykac said in Turning off power to a running QSerialPort:

                          So you are saying it is not solveable?

                          Before you can solve it, you must first track down the cause of the crash.

                          You need to spend time troubleshooting. For example:

                          • See if you can gradually remove code from other parts of your application and still cause the crash.
                          • See if the crashes occur when you only have 1 instead of 6 serial devices.
                          • See if a different hub causes the crashes.
                          • See if a dedicated multi-serial-port hub causes the crashes: https://www.moxa.com/product/UPort_1610-8.htm

                          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                          1 Reply Last reply
                          2
                          • S Offline
                            S Offline
                            sykac
                            wrote on 5 Oct 2018, 13:56 last edited by sykac 10 May 2018, 13:58
                            #13

                            @J-Hilk @JKSH So it seems there is more problems in my serial communication. One problem starts probably in the hardware. The COM port of the device disconnects from the PC (I don't why, it could be some driver/firmware issue), QSerialPort emits error "The attached device is not functioning.". Then the application closes the serial port and tries to reopen it. It opens succesfully but when waitForReadyRead(3000) is called after that, the thread hangs forever. It doesn't crash, just hangs.

                            It is weird as in the Device Manager I cannot see the COM port anymore, although the QSerialPort opens succesfully.

                            Then there is another error which causes the crashing but I couldn't find the reproducer for it yet.

                            aha_1980A 1 Reply Last reply 5 Oct 2018, 15:11
                            0
                            • S sykac
                              5 Oct 2018, 13:56

                              @J-Hilk @JKSH So it seems there is more problems in my serial communication. One problem starts probably in the hardware. The COM port of the device disconnects from the PC (I don't why, it could be some driver/firmware issue), QSerialPort emits error "The attached device is not functioning.". Then the application closes the serial port and tries to reopen it. It opens succesfully but when waitForReadyRead(3000) is called after that, the thread hangs forever. It doesn't crash, just hangs.

                              It is weird as in the Device Manager I cannot see the COM port anymore, although the QSerialPort opens succesfully.

                              Then there is another error which causes the crashing but I couldn't find the reproducer for it yet.

                              aha_1980A Offline
                              aha_1980A Offline
                              aha_1980
                              Lifetime Qt Champion
                              wrote on 5 Oct 2018, 15:11 last edited by
                              #14

                              @sykac said in Turning off power to a running QSerialPort:

                              One problem starts probably in the hardware. The COM port of the device disconnects from the PC (I don't why, it could be some driver/firmware issue)

                              Reading you topic title, could it be that the USB connection between PC and FTDI is interrupted? And that's what I said, this may lead to endless loops in the device driver.

                              This kind of problems can hardly be solved in the user space.

                              Qt has to stay free or it will die.

                              1 Reply Last reply
                              1

                              • Login

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