Results 1 to 2 of 2

Thread: Parallel thread to read coming data

  1. #1
    Join Date
    Jan 2021
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Parallel thread to read coming data

    I never managed concurrent threads in Qt but now I need a function in my application to monitor the presence of available data coming from an FTDI device (I'm using the FDTI driver to read/write data). I need a for loop that run every few millisecond to check if there are bytes in the receive queue and thet run concurrently with the application. If there are bytes in the receive queue then emits a signal for the main thread.

    I read some example about the Concurrent library but I can't achieve my purpose cause I am not familiar with this stuff. Can someone help me or get me some suggestion?

  2. #2
    Join Date
    Dec 2024
    Posts
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Parallel thread to read coming data

    If you prefer not to manage threads directly, you can use a QTimer to check for data at regular intervals. This approach runs in the event loop and is simpler to implement.

    Qt Code:
    1. #include <QTimer>
    2. #include <QSerialPort>
    3.  
    4. class YourMainClass : public QObject {
    5. Q_OBJECT
    6.  
    7. public:
    8. YourMainClass(QSerialPort* serialPort) : m_serialPort(serialPort) {
    9. QTimer* timer = new QTimer(this);
    10. connect(timer, &QTimer::timeout, this, &YourMainClass::checkForData);
    11. timer->start(100); // Check every 100 milliseconds
    12. }
    13.  
    14. private slots:
    15. void checkForData() {
    16. if (m_serialPort->bytesAvailable() > 0) {
    17. emit dataAvailable();
    18. }
    19. }
    20.  
    21. signals:
    22. void dataAvailable();
    23.  
    24. private:
    25. QSerialPort* m_serialPort;
    26. };
    To copy to clipboard, switch view to plain text mode 
    In this case, you can connect the dataAvailable signal to any slot in your main application where you handle the incoming data.

    Last edited by d_stranz; 26th December 2024 at 01:51. Reason: missing [code] tags

Similar Threads

  1. Replies: 0
    Last Post: 18th May 2021, 20:05
  2. Read with one thread, send datas to another thread
    By suslucoder in forum Qt Programming
    Replies: 3
    Last Post: 11th January 2021, 18:43
  3. Appropriate widget to display coming data
    By gkhrapunovich in forum Newbie
    Replies: 4
    Last Post: 14th February 2011, 19:50
  4. Replies: 0
    Last Post: 29th May 2009, 07:08
  5. Replies: 14
    Last Post: 27th November 2006, 06:09

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
  •  
Qt is a trademark of The Qt Company.