Results 1 to 15 of 15

Thread: QSerialPort read delay

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2013
    Posts
    29
    Thanks
    4
    Thanked 4 Times in 2 Posts

    Question QSerialPort read delay

    Hi,

    i'm using QSerialPort to read a constant datastream from my comport.
    Therefore i'm using a seperate thread that calls itself with a QTimer within a 1ms interval with signal and slots.

    Basically like this:
    Qt Code:
    1. connect(ReceiveTimer, SIGNAL(timeout()), this, SLOT(SlotTimer()));
    2.  
    3. void SlotTimer(void){
    4. QByteArray ReceivedDat = SerialPort->readAll();
    5. qDebug() << "Received bytes: " << ReceivedDat.size() << " Timestamp: " << QDateTime::currentMSecsSinceEpoch();
    6. }
    To copy to clipboard, switch view to plain text mode 

    So far this works fine for me. The outputs of qDebug() show me that the thread is called every millisecond like it should be.
    The problem is that ReceivedDat does not hold data at some calls of the slot depending on the baudrate.

    I use two baudrates 38400 and 57600 so therefore within 1ms about 3-4 Bytes and 5-6 Bytes should be stored in the buffer.
    When running my program i got new data in an interval of 16ms (38400) and around 800!ms (57600).

    Is there anything i'm missing?


    Win7 Prof 64-bit
    Qt 5.2.0 (MSVC 2010, 32 bit)
    Revision 27d10d8dcd

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

    Default Re: QSerialPort read delay

    Why You don't use readyRead() signal ?

  3. #3
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSerialPort read delay

    @M4chin3,

    why you are surprised? It is normal because asynchronous I/O. Besides these intervals depend on load of Qt-event loop of thread (main GUI thread or other thread) where is your QSerialPort instance.

  4. #4
    Join Date
    Mar 2013
    Posts
    29
    Thanks
    4
    Thanked 4 Times in 2 Posts

    Default Re: QSerialPort read delay

    @Lesiok

    You are right i could and i tried but it does not change the update interval. 800ms is way too much for my application.

    @kuzulis

    I'm surprised because my QSerialPort instance is in the separte thread with the QTimer. Also the load of the program/whole pc is < 5% and therefore i dont understand delays in the size of ~800ms.
    Next point is why should this time increase from 16ms to 800ms only from changing the baudrate from 38400 to 57600?

    edit: btw the QThread does nothing more than creating the Timer interval and reading the data from the serialport.

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSerialPort read delay

    Windows is not RT system.

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

    M4chin3 (15th January 2014)

  7. #6
    Join Date
    Mar 2013
    Posts
    29
    Thanks
    4
    Thanked 4 Times in 2 Posts

    Default Re: QSerialPort read delay

    Quote Originally Posted by Lesiok View Post
    Windows is not RT system.
    Absolutely!
    But i think with 2x 2,6GHz it should be possible to receive data from a comport under 800ms (0,8s).

    Maybe it is not quite clear i try it with an example:

    Qt Code:
    1. void SlotTimer(void){ // called with a 1ms interval
    2. QByteArray ReceivedDat = SerialPort->readAll();
    3. if(0 < ReceivedDat.size()){
    4. qDebug() << "Received bytes: " << ReceivedDat.size() << " Timestamp: " << QDateTime::currentMSecsSinceEpoch();
    5. }else{
    6. qDebug() << "No data." << " Timestamp: " << QDateTime::currentMSecsSinceEpoch();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Using a baudrate of 38400 results in this output pattern (16ms between every chunk of data):
    "Received bytes: 62 Timestamp: 1389632940000"
    "No data. Timestamp: 1389632940001"
    ... ~13 times more
    "No data. Timestamp: 13896329400015"
    "Received bytes: 61 Timestamp: 1389632940016"
    "No data. Timestamp: 1389632940017"
    ... ~13 times more
    "No data. Timestamp: 1389632940031""
    "Received bytes: 62 Timestamp: 1389632940032"
    ... and so on for minutes

    Using a baudrate of 57600 results in this output pattern (800ms between every chunk of data):
    "Received bytes: 4608 Timestamp: 1389632940000"
    "No data. Timestamp: 1389632940001"
    ... ~800 times more
    "No data. Timestamp: 1389632940799"
    "Received bytes: 4608 Timestamp: 1389632940800"
    "No data. Timestamp: 1389632940801"
    ... ~800 times more
    "No data. Timestamp: 1389632941599"
    "Received bytes: 4608 Timestamp: 1389632941600"
    ... and so on for minutes

    So i'm talking not about an sporadic error.
    Last edited by M4chin3; 15th January 2014 at 14:18.

  8. #7
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSerialPort read delay

    Ok, now please:

    1. What type of serial device you use?
    2. Please re-implement to readyRead() usage and show your timestamps, e.g.:

    Qt Code:
    1. connect(SerialPort, SIGNAL(readyRead()), this, SLOT(SlotReadyRead()));
    2.  
    3. void SlotReadyRead() {
    4. QByteArray ReceivedDat = SerialPort->readAll();
    5. if(0 < ReceivedDat.size()){
    6. qDebug() << "Received bytes: " << ReceivedDat.size() << " Timestamp: " << QDateTime::currentMSecsSinceEpoch();
    7. }else{
    8. qDebug() << "No data." << " Timestamp: " << QDateTime::currentMSecsSinceEpoch();
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to kuzulis for this useful post:

    M4chin3 (15th January 2014)

  10. #8
    Join Date
    Mar 2013
    Posts
    29
    Thanks
    4
    Thanked 4 Times in 2 Posts

    Default Re: QSerialPort read delay

    @kuzulis

    1. I'm using a virtual comport device with a controller from FTDI (FT 232 RL)
    2. I reimplemented it with readyRead() usage and it seems to work. I will take a closer look tomorrow and reply in detail. (@Lesiok: sorry i misread your reply, i thought you ment readAll and readData)

    Thanks in advance.

  11. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSerialPort read delay

    I'm surprised because my QSerialPort instance is in the separte thread with the QTimer.
    You do not need a separate thread to read from a serial port. Unless the thread has another reason for existing it only complicates what should be a straightforward problem.

  12. #10
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSerialPort read delay

    @M4chin3,

    I reimplemented it with readyRead() usage and it seems to work.
    Hmm.. It is very interest and strange.. Can you please also, in additional, provide a your project's source code?

    @ChrisW67,

    if to be honest, in Windows use of a additional thread is desirable, because still there is this bug with a freezing: https://bugreports.qt-project.org/browse/QTBUG-34946

Similar Threads

  1. QSerialPort :: read - Serial Comms noob.
    By llaregyb in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2014, 12:18
  2. Qserialport issue in QT4
    By coss_cat in forum Qt Programming
    Replies: 3
    Last Post: 11th December 2013, 18:11
  3. QSerialport in multithread
    By snow_starzz in forum Newbie
    Replies: 3
    Last Post: 3rd December 2013, 10:18
  4. Qt5 cmake and QSerialPort
    By Chris.Burner in forum Newbie
    Replies: 1
    Last Post: 21st April 2013, 16:13
  5. Replies: 1
    Last Post: 16th June 2009, 09: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.