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. problem using QSerialPort
Forum Updated to NodeBB v4.3 + New Features

problem using QSerialPort

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 8 Posters 2.6k Views 4 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.
  • Kent-DorfmanK Offline
    Kent-DorfmanK Offline
    Kent-Dorfman
    wrote on last edited by
    #4

    dont construct it in the constructor, but send a signal to the thread later to construct and start the serial object.

    1 Reply Last reply
    0
    • mzimmersM mzimmers

      Hi all -

      I've got an app that uses a COM port for initial configuration of a device that then joins a Wifi network. I'm using the QSerialPort class for this configuration. I'm connecting a readyRead signal for input, but it never triggers. I'm also getting this error message:

      serial port error QSerialPort::NoError encountered.
      QObject: Cannot create children for a parent that is in a different thread.
      (Parent is QSerialPort(0x16d13d8), parent's thread is QThread(0x161cb20), current thread is QThread(0x16e58d0)
      

      I'm wondering if I'm doing something wrong with my threads. Here's a code snippet:

      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          worker = new Worker(widget);
          thread = new QThread;
          worker->moveToThread(thread);
      
          widget->show();
          thread->start();
          rc = a.exec();
      ...
      Worker::Worker(Widget *widget, QObject *parent) : QObject (parent)
      {
          m_serialPort = new SerialPort(this);
      ...
      SerialPort::SerialPort(QObject *parent) : QObject (parent)
      {
          QSerialPort m_serialPort;
          connect(&m_serialPort, &QSerialPort::readyRead, this, &SerialPort::readSerial);
      

      My slot SerialPort::readSerial() never gets called. I've verified that there's incoming data using a port monitor. Any ideas what I've done wrong here?

      Thanks...

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #5

      @mzimmers Is there a reason why you're using a thread? QSerialPort has an asynchronous API and there is usually no need to use threads for that.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      mzimmersM 1 Reply Last reply
      2
      • mzimmersM mzimmers

        Hi all -

        I've got an app that uses a COM port for initial configuration of a device that then joins a Wifi network. I'm using the QSerialPort class for this configuration. I'm connecting a readyRead signal for input, but it never triggers. I'm also getting this error message:

        serial port error QSerialPort::NoError encountered.
        QObject: Cannot create children for a parent that is in a different thread.
        (Parent is QSerialPort(0x16d13d8), parent's thread is QThread(0x161cb20), current thread is QThread(0x16e58d0)
        

        I'm wondering if I'm doing something wrong with my threads. Here's a code snippet:

        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            worker = new Worker(widget);
            thread = new QThread;
            worker->moveToThread(thread);
        
            widget->show();
            thread->start();
            rc = a.exec();
        ...
        Worker::Worker(Widget *widget, QObject *parent) : QObject (parent)
        {
            m_serialPort = new SerialPort(this);
        ...
        SerialPort::SerialPort(QObject *parent) : QObject (parent)
        {
            QSerialPort m_serialPort;
            connect(&m_serialPort, &QSerialPort::readyRead, this, &SerialPort::readSerial);
        

        My slot SerialPort::readSerial() never gets called. I've verified that there's incoming data using a port monitor. Any ideas what I've done wrong here?

        Thanks...

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #6

        @mzimmers said in problem using QSerialPort:

        SerialPort::SerialPort(QObject *parent) : QObject (parent)
        {
            QSerialPort m_serialPort;
            connect(&m_serialPort, &QSerialPort::readyRead, this, &SerialPort::readSerial);
        

        This doesn't look right. Are you sure this is what you wrote?

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        4
        • mzimmersM mzimmers

          Hi all -

          I've got an app that uses a COM port for initial configuration of a device that then joins a Wifi network. I'm using the QSerialPort class for this configuration. I'm connecting a readyRead signal for input, but it never triggers. I'm also getting this error message:

          serial port error QSerialPort::NoError encountered.
          QObject: Cannot create children for a parent that is in a different thread.
          (Parent is QSerialPort(0x16d13d8), parent's thread is QThread(0x161cb20), current thread is QThread(0x16e58d0)
          

          I'm wondering if I'm doing something wrong with my threads. Here's a code snippet:

          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              worker = new Worker(widget);
              thread = new QThread;
              worker->moveToThread(thread);
          
              widget->show();
              thread->start();
              rc = a.exec();
          ...
          Worker::Worker(Widget *widget, QObject *parent) : QObject (parent)
          {
              m_serialPort = new SerialPort(this);
          ...
          SerialPort::SerialPort(QObject *parent) : QObject (parent)
          {
              QSerialPort m_serialPort;
              connect(&m_serialPort, &QSerialPort::readyRead, this, &SerialPort::readSerial);
          

          My slot SerialPort::readSerial() never gets called. I've verified that there's incoming data using a port monitor. Any ideas what I've done wrong here?

          Thanks...

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

          @mzimmers
          IIRC you're using a static build? so its probably not the latest Qt Version on what are you running?

          If it isn't the error @kshegunov pointed out


          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
          • jsulmJ jsulm

            @mzimmers Is there a reason why you're using a thread? QSerialPort has an asynchronous API and there is usually no need to use threads for that.

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

            @jsulm I'm not using a thread exclusively for the serial port work; my worker thread essentially does all my non-UI stuff(encoding/decoding messages, etc.)
            @kshegunov sorry; I was engaging in a bit of shorthand. m_serialPort is a member variable of SerialPort. The connect is correct, though.
            @J-Hilk I'm currently using (non-static) 5.12.5.

            Last night it occurred to me that long ago, I'd come across a problem with transferring objects to new threads, in which some sub-objects don't get transferred properly.

            I might try Dorfman's idea, though I'd prefer not to have to do that.

            Thanks...

            J.HilkJ 1 Reply Last reply
            1
            • mzimmersM mzimmers

              @jsulm I'm not using a thread exclusively for the serial port work; my worker thread essentially does all my non-UI stuff(encoding/decoding messages, etc.)
              @kshegunov sorry; I was engaging in a bit of shorthand. m_serialPort is a member variable of SerialPort. The connect is correct, though.
              @J-Hilk I'm currently using (non-static) 5.12.5.

              Last night it occurred to me that long ago, I'd come across a problem with transferring objects to new threads, in which some sub-objects don't get transferred properly.

              I might try Dorfman's idea, though I'd prefer not to have to do that.

              Thanks...

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

              @mzimmers said in problem using QSerialPort:

              @J-Hilk I'm currently using (non-static) 5.12.5.

              And there is your problem
              https://bugreports.qt.io/browse/QTBUG-78086

              please update


              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
              3
              • J.HilkJ J.Hilk

                @mzimmers said in problem using QSerialPort:

                @J-Hilk I'm currently using (non-static) 5.12.5.

                And there is your problem
                https://bugreports.qt.io/browse/QTBUG-78086

                please update

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

                @J-Hilk bang! Good eye. Thanks for catching that. I'll update immediately.

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

                  Hi,

                  IIRC, your QSerialPort object won't get moved if your don't parent it.

                  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

                    @J-Hilk bang! Good eye. Thanks for catching that. I'll update immediately.

                    KroMignonK Offline
                    KroMignonK Offline
                    KroMignon
                    wrote on last edited by KroMignon
                    #12

                    @mzimmers said in problem using QSerialPort:

                    Good eye. Thanks for catching that. I'll update immediately.

                    Any way, your initialization seems not correct to me:

                    SerialPort::SerialPort(QObject *parent) : QObject (parent)
                    {
                        QSerialPort m_serialPort;
                        connect(&m_serialPort, &QSerialPort::readyRead, this, &SerialPort::readSerial);
                    ...
                    }
                    

                    should be, I think:

                    SerialPort::SerialPort(QObject *parent)
                         : QObject (parent)
                         , m_serialPort(new QSerialPort(this))
                    {
                        connect(m_serialPort, &QSerialPort::readyRead, this, &SerialPort::readSerial);
                    ...
                    }
                    

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    1 Reply Last reply
                    1
                    • SGaistS SGaist

                      Hi,

                      IIRC, your QSerialPort object won't get moved if your don't parent it.

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

                      @SGaist ah yes, that's what it was. Thanks for refreshing my memory.

                      Edit:

                      Which member variables does this apply to? For example, I also have a member QByteArray; does that need to be parented too?

                      Thanks...

                      KroMignonK 1 Reply Last reply
                      0
                      • mzimmersM mzimmers

                        @SGaist ah yes, that's what it was. Thanks for refreshing my memory.

                        Edit:

                        Which member variables does this apply to? For example, I also have a member QByteArray; does that need to be parented too?

                        Thanks...

                        KroMignonK Offline
                        KroMignonK Offline
                        KroMignon
                        wrote on last edited by
                        #14

                        @mzimmers said in problem using QSerialPort:

                        Which member variables does this apply to?

                        Each member which base class is QObject.

                        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                        mzimmersM 1 Reply Last reply
                        2
                        • KroMignonK KroMignon

                          @mzimmers said in problem using QSerialPort:

                          Which member variables does this apply to?

                          Each member which base class is QObject.

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

                          @KroMignon thanks...so a QByteArray object wouldn't be affected then.

                          It looks like the mechanics of updating have changed since I last used them. Is there a pre-built 5.14 for Windows available somewhere on github? EDIT: looks like the answer is "no;" I guess everyone builds their own now. Given my recent troubles trying to build a static Qt, I hope the directions in the 5.14 README are accurate.

                          I'm going to mark this solved, in anticipation that when I get a new Qt built, it'll work. Thanks for everyone's input on this.

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

                            You need now to use your Qt account to access the pre-built binaries (the same you use to post here)

                            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
                            0
                            • SGaistS SGaist

                              You need now to use your Qt account to access the pre-built binaries (the same you use to post here)

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

                              @SGaist thanks. Good thing they still exist, because I'm continuing to have nothing but trouble in my attempts to build the libraries. I really should report my problems with this to someone; any suggestions?

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

                                The bug tracker is your friend :-)

                                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
                                • SGaistS SGaist

                                  The bug tracker is your friend :-)

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

                                  @SGaist done. Thanks.

                                  aha_1980A 1 Reply Last reply
                                  0
                                  • mzimmersM mzimmers

                                    @SGaist done. Thanks.

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

                                    Hi @mzimmers,

                                    would you mind letting us know, where?

                                    Thanks and regards

                                    Qt has to stay free or it will die.

                                    mzimmersM 1 Reply Last reply
                                    1
                                    • aha_1980A aha_1980

                                      Hi @mzimmers,

                                      would you mind letting us know, where?

                                      Thanks and regards

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

                                      Hi @aha_1980 - if you're referring to my bug report, it's here.

                                      1 Reply Last reply
                                      2

                                      • Login

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