Results 1 to 3 of 3

Thread: serial communication

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    101
    Thanked 15 Times in 15 Posts

    Default Re: serial communication

    You need to write either a thread that runs in the background and collects the data, or use a QTimer to periodically poll the serial port. The threaded version looks something like this:

    Qt Code:
    1. setupTheSerialPortJustLikeInQESPTA();
    2.  
    3. while (true)
    4. {
    5. receiveData();
    6. msleep(100);
    7. }
    8.  
    9. void receiveData()
    10. {
    11. // Check if there is any data to read.
    12. if (!port->bytesAvailable())
    13. return -1;
    14.  
    15. // Read as much data as you can from the port.
    16. bytesRead = port->read(receiveBuffer, qMin(port->bytesAvailable(), BUFFER_SIZE));
    17. }
    To copy to clipboard, switch view to plain text mode 

    With the QTimer solution you don't have an infinite loop with a sleep, but you call receiveData() with a timer instead.

    A blocking approach that tries to read from the port and waits until data arrives would be preferable, but I couldn't make that work because of a problem with waitForReadyRead().
    Last edited by Cruz; 13th February 2009 at 10:00. Reason: spelling error

Similar Threads

  1. Serial Port communication
    By mgurbuz in forum Qt Programming
    Replies: 12
    Last Post: 22nd January 2011, 02:38
  2. serial communication problem
    By Tavit in forum Qt Programming
    Replies: 3
    Last Post: 31st May 2008, 12:56
  3. serial communication programming
    By jagadish in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2007, 07:47
  4. C++ Library for serial communication
    By dec0ding in forum General Programming
    Replies: 7
    Last Post: 8th July 2007, 18:18
  5. How to transfer file through serial communication
    By shamik in forum Qt Programming
    Replies: 29
    Last Post: 29th May 2007, 10:22

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.