Results 1 to 5 of 5

Thread: QExtSerialPort with readyRead()

  1. #1
    Join Date
    Sep 2007
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QExtSerialPort with readyRead()

    Hi,

    I try to listen to the serial port when there is data with
    connect(port, SIGNAL(readyRead()), this, SLOT(receiveMsg()));
    but readyRead() doesn't work with QExtSerialPort on linux, So how can I get the signal whenever there is new data on the port?

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QExtSerialPort with readyRead()

    You can't. You have to start a timer and periodically check bytesAvailable().

  3. #3
    Join Date
    Sep 2007
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QExtSerialPort with readyRead()

    thank wysota

    I set up the QTimer on main gui thread at 1000ms, does it affect the gui, slow it down or freeze it?

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(receiveMsg()));
    timer->start(1000);

  4. #4
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QExtSerialPort with readyRead()

    Quote Originally Posted by tho97 View Post
    thank wysota

    I set up the QTimer on main gui thread at 1000ms, does it affect the gui, slow it down or freeze it?

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(receiveMsg()));
    timer->start(1000);
    Depends on what you do in that timeout. Just read the bytes and return, don't do any long processing and you should be ok.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QExtSerialPort with readyRead()

    A suggest a smaller timeout (like 100ms) unless you rarely receive anything on the serial port. To be honest it is best to have an adaptive timeout, for instance start with 100ms. If you receive something reduce the timeout twice unless you are already on 100ms. If you don't receive anything, double the timeout.

    Qt Code:
    1. void sth::tryReceive(){
    2. if(bytesAvailable()>0){
    3. timeout = qMax(100, timeout/2);
    4. readData(...);
    5. } else {
    6. timeout = qMin(1000, timeout*2);
    7. }
    8. QTimer::singleShot(timeout, this, SLOT(tryReceive()));
    9. }
    To copy to clipboard, switch view to plain text mode 

    This way you'll be receiving faster if there is anything to receive and will reduce the load on the application when there is not.

  6. The following user says thank you to wysota for this useful post:

    kingslee (5th September 2008)

Similar Threads

  1. Modbus, QextSerialPort and QTimer
    By ^NyAw^ in forum General Programming
    Replies: 6
    Last Post: 27th October 2009, 16:14
  2. Problem in QExtserialport program.
    By dheeraj in forum Qt Programming
    Replies: 5
    Last Post: 31st May 2008, 10:24
  3. Replies: 4
    Last Post: 30th January 2008, 10:00
  4. any body using qextserialport?
    By yagabey in forum Newbie
    Replies: 4
    Last Post: 25th December 2007, 22:08
  5. QExtSerialPort with com ports above com9
    By mightymark in forum Qt Programming
    Replies: 2
    Last Post: 29th May 2006, 08:16

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.