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. Help with QThread and Serialport
Forum Updated to NodeBB v4.3 + New Features

Help with QThread and Serialport

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 5 Posters 2.5k 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.
  • M Offline
    M Offline
    millsey
    wrote on last edited by
    #1

    Hi all

    I am new to Qthread and need a little assistance.

    I have used the blockingmaster example project to base my serial communication program on.

    I have a mainwindow that creates a QDialog which provides a GUI to initiate and detect an instrument connected to an unknown COM port. When QDialog is contructed, QDialog creates a thread which handles the serial port for me (as seen in blockingmaster example)

    I use the dialog to look for available ports, list the port and set baud rate, I then query the instrument and confirm it is there and responding.

    I want to then close this thread off when I click "accept" on my dialog and then start a new thread (as well as a new serialport object) in the mainwindow...

    Or I want to pass control of the thread to mainwindow...

    I cannot seem to stop the thread so that I can create a new serialport object in a new thread to take over the communication with that port....

    Or should I create a global thread to be accessed by any class?

    In the blockingmaster example, how would you sto the thread? thread.stop and thread.exit dont seem to work... As when I try to open that same serial port in another thread I get process error

    Thanks

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • M millsey

      Hi all

      I am new to Qthread and need a little assistance.

      I have used the blockingmaster example project to base my serial communication program on.

      I have a mainwindow that creates a QDialog which provides a GUI to initiate and detect an instrument connected to an unknown COM port. When QDialog is contructed, QDialog creates a thread which handles the serial port for me (as seen in blockingmaster example)

      I use the dialog to look for available ports, list the port and set baud rate, I then query the instrument and confirm it is there and responding.

      I want to then close this thread off when I click "accept" on my dialog and then start a new thread (as well as a new serialport object) in the mainwindow...

      Or I want to pass control of the thread to mainwindow...

      I cannot seem to stop the thread so that I can create a new serialport object in a new thread to take over the communication with that port....

      Or should I create a global thread to be accessed by any class?

      In the blockingmaster example, how would you sto the thread? thread.stop and thread.exit dont seem to work... As when I try to open that same serial port in another thread I get process error

      Thanks

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

      @millsey Why do you need threads? QSerialPort has an asynchronous API.

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

      1 Reply Last reply
      5
      • M millsey

        Hi all

        I am new to Qthread and need a little assistance.

        I have used the blockingmaster example project to base my serial communication program on.

        I have a mainwindow that creates a QDialog which provides a GUI to initiate and detect an instrument connected to an unknown COM port. When QDialog is contructed, QDialog creates a thread which handles the serial port for me (as seen in blockingmaster example)

        I use the dialog to look for available ports, list the port and set baud rate, I then query the instrument and confirm it is there and responding.

        I want to then close this thread off when I click "accept" on my dialog and then start a new thread (as well as a new serialport object) in the mainwindow...

        Or I want to pass control of the thread to mainwindow...

        I cannot seem to stop the thread so that I can create a new serialport object in a new thread to take over the communication with that port....

        Or should I create a global thread to be accessed by any class?

        In the blockingmaster example, how would you sto the thread? thread.stop and thread.exit dont seem to work... As when I try to open that same serial port in another thread I get process error

        Thanks

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

        @millsey

        Hi and welcome,

        You will need to post code, or we won't be able to help you.

        That said, there is no (immediate) reason to move QSerialPort into its own thread if you use the non blocking api. Why do you want to use the blocking one, do you not have an Operating System on your target platform?


        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.

        M 1 Reply Last reply
        4
        • J.HilkJ J.Hilk

          @millsey

          Hi and welcome,

          You will need to post code, or we won't be able to help you.

          That said, there is no (immediate) reason to move QSerialPort into its own thread if you use the non blocking api. Why do you want to use the blocking one, do you not have an Operating System on your target platform?

          M Offline
          M Offline
          millsey
          wrote on last edited by
          #4

          @J-Hilk

          Hi both

          Thanks for your prompt response, I only used the thread method as that is what is in the example....

          Maybe it has led me down a stupid path?

          A path a noob like me can't cope with!

          Im an electronics engineer so maybe thread are best avoided where possible!

          Shall I just crerate a non block serial port object in mainwindow?

          J.HilkJ 1 Reply Last reply
          1
          • M Offline
            M Offline
            millsey
            wrote on last edited by
            #5

            Why did they use a thread in this example?

            https://doc.qt.io/qt-5/qtserialport-blockingmaster-example.html

            1 Reply Last reply
            0
            • M millsey

              @J-Hilk

              Hi both

              Thanks for your prompt response, I only used the thread method as that is what is in the example....

              Maybe it has led me down a stupid path?

              A path a noob like me can't cope with!

              Im an electronics engineer so maybe thread are best avoided where possible!

              Shall I just crerate a non block serial port object in mainwindow?

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

              @millsey
              ideally you would create your own class that manages the Serialport / data exchange ( don't do it directly in mainwindow.cpp )

              that way, once that is working as expected, you can still move it into its own thread, via very very few lines of code ;)

              Why did they use a thread in this example?

              because its the blockingmaster example ;)

              take a look at the selection:
              https://doc.qt.io/qt-5/qtserialport-examples.html

              everything asynchronous is your friend!


              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
              6
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #7

                Hi
                I would take a copy of
                https://doc.qt.io/qt-5/qtserialport-terminal-example.html
                and use as a base.
                It has most things setup and even a dialog where you can set the serial parameters.

                Sadly you found the BLOCKING examples which just show how to use the blocking version of the API but
                its actually not the recommended one as it hangs the GUI. But has it uses.

                Typically you would only use a thread with serialPort if you need to do heavy processing of the data you read.
                Then a thread can be used to process the data while main thread (the gui) just reads it when it get the signal.

                -Im an electronics engineer so maybe thread are best avoided where possible!
                Well that goes for most others too :)

                M 1 Reply Last reply
                5
                • M Offline
                  M Offline
                  millsey
                  wrote on last edited by
                  #8

                  thank you all!

                  I will check it out

                  1 Reply Last reply
                  1
                  • M Offline
                    M Offline
                    millsey
                    wrote on last edited by
                    #9

                    Hi all

                    I have another question which I can sure you can help with.

                    I have now implemented the recommended non -blocking QSerialport which works well, I’m happy I have COM ports working and I can send and receive data. I am also able to read until I see a carriage return and then parse the data where I need it.

                    I also need to add LAN connection to the same instrumentation, I will need to send and receive ASCII commands over LAN to the instrument. Can you recommend a good example project to base my communication on?

                    Thanks!

                    aha_1980A 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      Hi
                      I would take a copy of
                      https://doc.qt.io/qt-5/qtserialport-terminal-example.html
                      and use as a base.
                      It has most things setup and even a dialog where you can set the serial parameters.

                      Sadly you found the BLOCKING examples which just show how to use the blocking version of the API but
                      its actually not the recommended one as it hangs the GUI. But has it uses.

                      Typically you would only use a thread with serialPort if you need to do heavy processing of the data you read.
                      Then a thread can be used to process the data while main thread (the gui) just reads it when it get the signal.

                      -Im an electronics engineer so maybe thread are best avoided where possible!
                      Well that goes for most others too :)

                      M Offline
                      M Offline
                      millsey
                      wrote on last edited by
                      #10

                      @mrjj

                      Am I correct in saying that the non-blocking “asynchronous” behaviour is primarily achieved by the program sending the command and kind of “forgetting it has sent it”.

                      You can then continue to use the GUI as you which.

                      The connect statement below is what “wakes up” the program to a received message from the connected device...

                      connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData);

                      Am I right? The connect statement provides the means to achieve a non-blocking program?

                      Thanks!

                      mrjjM 1 Reply Last reply
                      0
                      • M millsey

                        @mrjj

                        Am I correct in saying that the non-blocking “asynchronous” behaviour is primarily achieved by the program sending the command and kind of “forgetting it has sent it”.

                        You can then continue to use the GUI as you which.

                        The connect statement below is what “wakes up” the program to a received message from the connected device...

                        connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData);

                        Am I right? The connect statement provides the means to achieve a non-blocking program?

                        Thanks!

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #11

                        Hi

                        Am I correct in saying that the non-blocking “asynchronous” behaviour is primarily achieved by the program sending the command and kind of “forgetting it has sent it”.

                        Not quite, its more about that the actual task is not happening right there meaning the cpu sort of like stays there until its finished versus it comes a nanosecond later in form of a signal.
                        And, whether its forgetting or not is up to you. you can track commands/data,
                        Hope this makes sense :)

                        The connect statement below is what “wakes up” the program to a received message from the connected device...

                        Well the device is not sleeping as such. internally in QSerialPort its thread see some data and reads it and then signal to the app that there is data.
                        The connect "merely" binds a signal to a response function

                        Am I right? The connect statement provides the means to achieve a non-blocking program?

                        yes as its based on signals. so you don't hang around in your app waiting for data. you get signal when something is ready and handle that there. and that is what “asynchronous” is mostly about.

                        1 Reply Last reply
                        5
                        • M Offline
                          M Offline
                          millsey
                          wrote on last edited by
                          #12

                          Ok thanks so much for the response

                          I am still not comfortable how these connect signals work when passing messages between different classes so I need to do some background reading on those.

                          Thanks

                          1 Reply Last reply
                          1
                          • M millsey

                            Hi all

                            I have another question which I can sure you can help with.

                            I have now implemented the recommended non -blocking QSerialport which works well, I’m happy I have COM ports working and I can send and receive data. I am also able to read until I see a carriage return and then parse the data where I need it.

                            I also need to add LAN connection to the same instrumentation, I will need to send and receive ASCII commands over LAN to the instrument. Can you recommend a good example project to base my communication on?

                            Thanks!

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

                            Hi @millsey,

                            I also need to add LAN connection to the same instrumentation, I will need to send and receive ASCII commands over LAN to the instrument. Can you recommend a good example project to base my communication on?

                            You didn's specify the protocol that is used here, so probably QTcpSocket might fulfill your needs.

                            Regards

                            Qt has to stay free or it will die.

                            M 1 Reply Last reply
                            5
                            • aha_1980A aha_1980

                              Hi @millsey,

                              I also need to add LAN connection to the same instrumentation, I will need to send and receive ASCII commands over LAN to the instrument. Can you recommend a good example project to base my communication on?

                              You didn's specify the protocol that is used here, so probably QTcpSocket might fulfill your needs.

                              Regards

                              M Offline
                              M Offline
                              millsey
                              wrote on last edited by
                              #14

                              @aha_1980

                              Hi there

                              Sorry, iPv4 :-)

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                millsey
                                wrote on last edited by
                                #15

                                Hi guys

                                One other question about QSerialport

                                Is the best way to implement a timeout without blocking the GUI to start a thread timer which emits a timeout signal and calls a timeout function?

                                mrjjM 1 Reply Last reply
                                0
                                • M millsey

                                  Hi guys

                                  One other question about QSerialport

                                  Is the best way to implement a timeout without blocking the GUI to start a thread timer which emits a timeout signal and calls a timeout function?

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @millsey
                                  Yes , have a look at QTimer :)

                                  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