Results 1 to 9 of 9

Thread: Serial port read/write using a multithreaded Qt app

  1. #1
    Join Date
    Nov 2016
    Posts
    7
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Question Serial port read/write using a multithreaded Qt app

    The Qt application I am required to develop, needs to have 2 threads.

    thread-1: read data from a serial port and pass it onto thread 2
    thread-2: display the raw data it received (sent by thread-1)

    From my very limited knowledge of Qt, I understand thread-1 is the worker thread and thread-2 is the main GUI thread.

    What is the design approach for implementing this application?

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,532
    Thanked 282 Times in 277 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serial port read/write using a multithreaded Qt app

    The second thread (with serial port) is unnecessary. Just use signal readyRead to get data from serial port and display it.

  3. #3
    Join Date
    Nov 2016
    Posts
    7
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Thumbs up Re: Serial port read/write using a multithreaded Qt app

    Thanks, Lesiok.

    What you are saying is correct and I understand that.

    But, there is a requirement for 2 threads because more functionalities will be added in future that require 2nd thread.

    Before we add many functionalities, I'm just trying to implement the basic framework with 2 threads.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,532
    Thanked 282 Times in 277 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serial port read/write using a multithreaded Qt app

    This use a signals and slots mechanism for communicating between threads. And yes, thread 1 is worker and thread 2 is GUI (main) thread.

  5. #5
    Join Date
    Nov 2016
    Posts
    7
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Exclamation Re: Serial port read/write using a multithreaded Qt app

    Ok, cool. I'm new to Qt and have been reading up some literature about it. So, do you think I should use a QueuedConnection to pass data from worker to main thread? Can you give me a basic structure of the code that I can start with for my application?

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,532
    Thanked 282 Times in 277 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serial port read/write using a multithreaded Qt app

    Yes, You should use a QueuedConnection. It is a default when sender and receiver are in two threads.

  7. #7
    Join Date
    Nov 2016
    Posts
    7
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Question Re: Serial port read/write using a multithreaded Qt app

    I've installed Qt 5.7 on Windows (8.1). I'm not getting QThread option (as a base class) while I try to add a new class to a project? What could be the reason?

  8. #8
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,532
    Thanked 282 Times in 277 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serial port read/write using a multithreaded Qt app

    Here You have an example how to deal with QThread. Note that in first example the Worker does not inherit the QThread. This method is better because you can easily use the Worker in any thread.

  9. #9
    Join Date
    Nov 2016
    Posts
    7
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Thumbs up Re: Serial port read/write using a multithreaded Qt app

    Ok, I will. Thanks, Lesiok.

Similar Threads

  1. read and write in serial port
    By neda in forum Qt Programming
    Replies: 13
    Last Post: 10th February 2016, 12:07
  2. Serial read misses to read data from the serial port
    By mania in forum Qt for Embedded and Mobile
    Replies: 11
    Last Post: 18th August 2014, 08:49
  3. Replies: 4
    Last Post: 10th July 2010, 17:34
  4. Replies: 1
    Last Post: 16th June 2009, 09:09
  5. How to write bytes read from serial port to a QFile
    By shamik in forum Qt Programming
    Replies: 19
    Last Post: 25th June 2007, 14:12

Tags for this Thread

Bookmarks

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.