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. [Solved] QtSerialPort and threading
Forum Updated to NodeBB v4.3 + New Features

[Solved] QtSerialPort and threading

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 11.2k Views 1 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.
  • E Offline
    E Offline
    egonuel
    wrote on last edited by
    #1

    Hi all,

    I'm working on a windows app, that should read data from different sensors mainly from the serialport. To add those I'm using the QtSerialport module and it works fine so far. Some of the sensors are delivering data with a frequency up to 512 Hz and hence are producing some activity. Which was until now not a problem as the development machine is a quite potent one. Yesterday I had to test the app on a lower powered laptop and unfortunately it was not able to fullfill its task: the data came in so frequently, that the event queue was filled up and the UI got unresponsible. My usual response to that is to create a second thread, move the hard working object to this thread and let the UI run smoothly again, not being influenced by the other object. But doing so with the QtSerialport seems to be not supported as the port presents me with an output like this once the port is being opened:
    @QObject: Cannot create children for a parent that is in a different thread.
    (Parent is SerialPort(0x24a1738), parent's thread is QThread(0x24a1770), current thread is QThread(0x18a8b0)
    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is SerialPort(0x24a1738), parent's thread is QThread(0x24a1770), current thread is QThread(0x18a8b0)@
    My app then produces some crashes and so I built a smaller testapp just reading one serialport.
    Same output as above, but at least no crashes and the data is being read.
    But I believe that those warnings may have a reason and some objects not being created is not something good to happen (or not to happen in this case;-)).
    Apparently some object deep inside the SerialPort is not moved to the new thread and hence is producing that warning.
    Why does this happen and is there a technical reason to behave like this, and if that's the case, what is the recommended way to handle situations like the one described above? Or can this be considered as a bug, which should be reported?

    Greetings
    Immanuel

    1 Reply Last reply
    1
    • JKSHJ Online
      JKSHJ Online
      JKSH
      Moderators
      wrote on last edited by
      #2

      Did you try to open the SerialPort from your main thread? I'm guessing the open() function causes child objects to be created. Try open()-ing the port from the other thread instead.

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

      1 Reply Last reply
      1
      • K Offline
        K Offline
        kuzulis
        Qt Champions 2020
        wrote on last edited by
        #3

        2 egonuel

        And how do you create an object of class of serial port?

        @port = new SerialPort(this);@

        or

        @port = new SerialPort();@

        1 Reply Last reply
        0
        • E Offline
          E Offline
          egonuel
          wrote on last edited by
          #4

          Thank you both for your answers.
          open()-ing from the other thread seems to do the trick here. (I used new SerialPort(), as one can't move objects with parents.)
          To achieve this I wrote a small wrapper class, which contains the serialport and provides slots for open and close, as the SerialPort functions are not declared as such.
          Is there a way to avoid writting a wrapper class here? (No one comes to my mind right now.) The open() and close() functions are normal members as the SerialPort inherites from the QIODevice, right?
          Nevertheless: I'm glad it works now, thanks! :)

          1 Reply Last reply
          1
          • JKSHJ Online
            JKSHJ Online
            JKSH
            Moderators
            wrote on last edited by
            #5

            If you only open it once in your entire program, you might be able to open it first, and THEN move it to the other thread -- I believe its children will be moved too.

            How are you reading data from the port, though? AFAIK, you can only grab data from a QIODevice via direct function calls. If you're doing that across threads, you might run into strange and subtle errors in the future

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

            1 Reply Last reply
            0
            • E Offline
              E Offline
              egonuel
              wrote on last edited by
              #6

              Yeah you are right, I did not thought about that, while writing my last post. In my app the port was allready wrapped before and the reading is done by direct function calls from the same thread the port belongs to. I did wrap the open() and close(), but called those wrapped functions directly from the UI thread, hence the warnings, I originally asked about, after I moved the wrapper object to the new thread. Now I'm connecting to these functions and everything works fine.

              1 Reply Last reply
              0
              • JKSHJ Online
                JKSHJ Online
                JKSH
                Moderators
                wrote on last edited by
                #7

                Good to hear :)

                Please edit your original post and add "[Solved]" to the title

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

                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