Results 1 to 2 of 2

Thread: QSerialPort sending data and waiting for response

  1. #1
    Join Date
    Apr 2021
    Qt products
    MacOS X Unix/X11 Windows

    Default QSerialPort sending data and waiting for response


    I am working on an application which communicates with an appliance attached to the serial port. The application send commands and waits for the appliance's reply, or until timeout. Currently I am doing the waiting using an instance of QEventLoop, however I read that using QEventLoop leads to subtle bugs. So now I need to redesign my application logic, which currently depends on a method which sends data out of the port and waits for a response of certain length.

    What I want to achieve is (pseudocode):
    Qt Code:
    1. response = connection.sendQuery(query, responseLength, timeout)
    2. if (response.length() == responseLength) {
    3. // valid response
    4. } else {
    5. //invalid response
    6. }
    To copy to clipboard, switch view to plain text mode 

    Can you please suggest a strategy for creating that logic?


  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 818 Times in 807 Posts
    Qt products
    Windows Android

    Default Re: QSerialPort sending data and waiting for response

    Look at QSerialPort (and the base class QIODevice) which implement a handshake protocol for writing and reading data based on signals and slots. A typical use is to connect a slot to the QSerialPort::readyRead() signal, which will be emitted when the serial port has received data (not necessarily the complete response, just any part of it).

    The Qt distribution comes with a large number of QSerialPort examples.

    There is rarely any need for you to implement your own event loop.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 2
    Last Post: 19th April 2018, 09:56
  2. Sending data over QSerialPort
    By jvalerio in forum Qt Programming
    Replies: 1
    Last Post: 12th February 2016, 15:40
  3. QSerialPort Bluetooth SPP Slow Response
    By BrianNorman in forum Qt Programming
    Replies: 2
    Last Post: 29th January 2016, 09:55
  4. Replies: 6
    Last Post: 23rd October 2015, 09:47
  5. How can i get data from com port use qserialport?
    By netvan42 in forum Qt Programming
    Replies: 0
    Last Post: 1st June 2014, 13:35


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.