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 wait for response
Forum Updated to NodeBB v4.3 + New Features

QSerialPort wait for response

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 1.8k 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.
  • S Offline
    S Offline
    Spiel
    wrote on last edited by
    #1

    Hi,

    I am working on an application which communicates with a tool via the serial port. I have a GUI and need to be able to send a command via the serial port and wait for reply with a given length until timeout, but the GUI needs to stay responsive. Can you point me to a suitable example?

    Thanks

    jsulmJ Pablo J. RoginaP 2 Replies Last reply
    0
    • S Spiel

      Hi,

      I am working on an application which communicates with a tool via the serial port. I have a GUI and need to be able to send a command via the serial port and wait for reply with a given length until timeout, but the GUI needs to stay responsive. Can you point me to a suitable example?

      Thanks

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

      @Spiel Use https://doc.qt.io/qt-5/qiodevice.html#readyRead and a QTimer

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

      S 1 Reply Last reply
      0
      • S Spiel

        Hi,

        I am working on an application which communicates with a tool via the serial port. I have a GUI and need to be able to send a command via the serial port and wait for reply with a given length until timeout, but the GUI needs to stay responsive. Can you point me to a suitable example?

        Thanks

        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #3

        @Spiel said in QSerialPort wait for response:

        Can you point me to a suitable example?

        you may want to look at the terminal example as well.

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        2
        • jsulmJ jsulm

          @Spiel Use https://doc.qt.io/qt-5/qiodevice.html#readyRead and a QTimer

          S Offline
          S Offline
          Spiel
          wrote on last edited by
          #4

          @jsulm Can you please expand a bit on that suggestion? Currently I am using a method called send(data, responseLength, timeout), which simulates a synchronous operation without blocking the main event loop, by having a separate QEventLoop. I'd like to rework the code and get rid of the QEventLoop.

          If I connect a slot to the readyRead signal, which buffers the data, and start a QTimer each time I send data, I will be able to check if data of certain length has been received in a slot connected to the timer's timeout. However the send method will return immediately, and that will break my application logic.

          jsulmJ 1 Reply Last reply
          0
          • S Spiel

            @jsulm Can you please expand a bit on that suggestion? Currently I am using a method called send(data, responseLength, timeout), which simulates a synchronous operation without blocking the main event loop, by having a separate QEventLoop. I'd like to rework the code and get rid of the QEventLoop.

            If I connect a slot to the readyRead signal, which buffers the data, and start a QTimer each time I send data, I will be able to check if data of certain length has been received in a slot connected to the timer's timeout. However the send method will return immediately, and that will break my application logic.

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

            @Spiel There is absolutely no need for separate event loops! Qt is an asynchronous framework and QSerialPort also has asynchronous API. Please read documentation and take a look at examples (for example the one pointed out by @Pablo-J-Rogina).

            Back to my suggestion: it should be clear how to implement a slot and connect it to a signal? So, implement a slot and connect it to https://doc.qt.io/qt-5/qiodevice.html#readyRead If you don't know how to do this please read https://doc.qt.io/qt-5/signalsandslots.html
            Then, after sending the command you start QTimer with your timeout. If you get a response your slot connected to readyRead signal will be called. If the slot is called before timeout from QTimer then everything is fine if it is called after timeout you can ignore it (or do what ever you want to do in this case).

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

            S 1 Reply Last reply
            2
            • jsulmJ jsulm

              @Spiel There is absolutely no need for separate event loops! Qt is an asynchronous framework and QSerialPort also has asynchronous API. Please read documentation and take a look at examples (for example the one pointed out by @Pablo-J-Rogina).

              Back to my suggestion: it should be clear how to implement a slot and connect it to a signal? So, implement a slot and connect it to https://doc.qt.io/qt-5/qiodevice.html#readyRead If you don't know how to do this please read https://doc.qt.io/qt-5/signalsandslots.html
              Then, after sending the command you start QTimer with your timeout. If you get a response your slot connected to readyRead signal will be called. If the slot is called before timeout from QTimer then everything is fine if it is called after timeout you can ignore it (or do what ever you want to do in this case).

              S Offline
              S Offline
              Spiel
              wrote on last edited by
              #6

              @jsulm Thank you for the clarification. What I still can't wrap my head around is how to make an asynchronous operation look like a synchronous operation for the caller, without using a nested event loop.

              JKSHJ 1 Reply Last reply
              0
              • S Spiel

                @jsulm Thank you for the clarification. What I still can't wrap my head around is how to make an asynchronous operation look like a synchronous operation for the caller, without using a nested event loop.

                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by JKSH
                #7

                @Spiel said in QSerialPort wait for response:

                send a command via the serial port and wait for reply with a given length until timeout, but the GUI needs to stay responsive.

                ...

                make an asynchronous operation look like a synchronous operation for the caller, without using a nested event loop.

                You have listed 3 requirements:

                1. Responsive GUI
                2. Synchronous function
                3. No separate event loop

                It is not possible to have all 3; you must choose 2. Which two are most important to you?

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

                jsulmJ 1 Reply Last reply
                2
                • JKSHJ JKSH

                  @Spiel said in QSerialPort wait for response:

                  send a command via the serial port and wait for reply with a given length until timeout, but the GUI needs to stay responsive.

                  ...

                  make an asynchronous operation look like a synchronous operation for the caller, without using a nested event loop.

                  You have listed 3 requirements:

                  1. Responsive GUI
                  2. Synchronous function
                  3. No separate event loop

                  It is not possible to have all 3; you must choose 2. Which two are most important to you?

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

                  @JKSH said in QSerialPort wait for response:

                  No separate event loop

                  This was my requirement :-)

                  @Spiel Do you really really need a synchronous operation?

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

                  JKSHJ 1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Spiel
                    wrote on last edited by
                    #9

                    I believe I do need synchronous operation. Please bear in mind that I only have less than two months of desktop programming, so I am probably getting a lot of concepts wrong at this point.

                    The application I am working on communicates with an embedded device using a number of different commands, in no particular order, and the responses vary in length. Each step depends on the outcome of the previous - hence I need to somehow wait until I get a response for a command, before deciding what should I do next. I've built my application using the nested QEventLoop, which allowed me to have pseudo-synchronous operation. However, after conversing with colleagues and doing some research, I found that my approach will lead to subtle bugs. Now I face a situation where I need to redesign my code logic in order to avoid that.

                    I am open to suggestions how to do that.

                    jsulmJ 1 Reply Last reply
                    0
                    • S Spiel

                      I believe I do need synchronous operation. Please bear in mind that I only have less than two months of desktop programming, so I am probably getting a lot of concepts wrong at this point.

                      The application I am working on communicates with an embedded device using a number of different commands, in no particular order, and the responses vary in length. Each step depends on the outcome of the previous - hence I need to somehow wait until I get a response for a command, before deciding what should I do next. I've built my application using the nested QEventLoop, which allowed me to have pseudo-synchronous operation. However, after conversing with colleagues and doing some research, I found that my approach will lead to subtle bugs. Now I face a situation where I need to redesign my code logic in order to avoid that.

                      I am open to suggestions how to do that.

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

                      @Spiel said in QSerialPort wait for response:

                      hence I need to somehow wait until I get a response for a command

                      No, you don't have to wait. The basic idea is: send command and as soon as you get response (via readyRead signal) you know that the command is processed and you can send next one. This way of programming can be confusing at the beginning, but this is how asynchronous APIs/frameworks (like Qt) work. It is better to do it this way instead of working against the framework/API.

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

                      1 Reply Last reply
                      5
                      • jsulmJ jsulm

                        @JKSH said in QSerialPort wait for response:

                        No separate event loop

                        This was my requirement :-)

                        @Spiel Do you really really need a synchronous operation?

                        JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on last edited by
                        #11

                        @jsulm said in QSerialPort wait for response:

                        @JKSH said in QSerialPort wait for response:

                        No separate event loop

                        This was my requirement :-)

                        OP did say "I'd like to rework the code and get rid of the QEventLoop"

                        @Spiel said in QSerialPort wait for response:

                        The application I am working on communicates with an embedded device using a number of different commands, in no particular order, and the responses vary in length. Each step depends on the outcome of the previous - hence I need to somehow wait until I get a response for a command, before deciding what should I do next.

                        I agree with @jsulm -- synchronous operations are not needed for this.

                        Depending on how many possible paths/branches your logic can take, you might benefit from a State Machine architecture (see https://www.mathworks.com/videos/understanding-state-machines-what-are-they-1-of-4-90488.html for an intro).

                        Each "step" could be represented as a state on the state machine diagram. Each time you receive the readyRead() signal from the serial port, you would process the response and then decide which state transition to take.

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

                        1 Reply Last reply
                        4

                        • Login

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