Results 1 to 3 of 3

Thread: QSerialPort - readyRead stops working

  1. #1
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QSerialPort - readyRead stops working

    I have an Arduino that monitors 4 signals, and then outputs those values to the serial port, connected to a PC.
    On the PC I have written a program to read from this serial port, and then to display the read in value.
    The program works fine, until it doesn't!
    It might work for 5 minutes, or 2 hours before it then stops working, as in the readyRead() is no longer generated.

    I have this in my constructor...

    Qt Code:
    1. serial = new QSerialPort(this);
    2. ...
    3. openSerialPort();
    4. ...
    5. connect(serial, static_cast<void (QSerialPort::*)(QSerialPort::SerialPortError)>(&QSerialPort::error), this, &MainWindow::handleError);
    6. connect(serial, &QSerialPort::readyRead, this, &MainWindow::readData);
    To copy to clipboard, switch view to plain text mode 

    and my openSerialPort()...

    Qt Code:
    1. void MainWindow::openSerialPort()
    2. {
    3. serial->setPortName(m_ArduinoPort);
    4. serial->setBaudRate(QSerialPort::Baud9600);
    5. serial->setDataBits(QSerialPort::Data8);
    6. serial->setParity(QSerialPort::NoParity);
    7. serial->setStopBits(QSerialPort::OneStop);
    8. serial->setFlowControl(QSerialPort::NoFlowControl);
    9. if (serial->isOpen())
    10. serial->close();
    11. serial->clearError();
    12. if (serial->open(QIODevice::ReadWrite)) {
    13. console->setEnabled(true);
    14. console->insertPlainText("Port " + m_ArduinoPort + " opened...\n");
    15. } else {
    16. console->insertPlainText("Port open error!\n");
    17. console->insertPlainText(serial->errorString());
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    My readData function does a whole lot of things like checking what string it is, and then updating a database with it.
    However, I put a call in this function to check that it is being called, and initially it enters and exits it fine lots of time, and then it just stops entering it.
    The number of times it works seems arbitrary.

    In trying to figure out what the problem is, I also set up a timer to write back to the serial port every 30s, and it does this successfully, whether the readyRead is still working or not.
    I.e. I write the value 2 to it, and it always returns 1 byte written (= -1 if an error occurs and no bytes written).

    I have read that one guy spoke about the serial port "hanging" sometimes, but this was in Qt 5.1, and I'm not sure if this was ever fixed (assuming it was once broken).
    Can anyone offer any insight into this?
    I have by-the-way checked errorString, and it always says "Unknown error" even if I try clearing it before the serial-open(), or after, and whether the readyRead works or not.

  2. #2
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSerialPort - readyRead stops working

    If ever someone ever comes across this post, just an update...

    Eventually I came to conclusion it is something to do with the PC itself dropping the connection, maybe a bad driver...?
    My solution was to modify the Arduino program, and each time I wanted to write to the serial port (about 3 times every 2 mintues), to first check if the serial port was still available, if not, then to try reset it.
    Qt Code:
    1. if(!Serial) { //check if Serial is available... if not,
    2. Serial.end(); // close serial port
    3. delay(100); //wait 100 millis
    4. Serial.begin(9600); // reenable serial again
    5. }
    6. Serial.println(3);
    To copy to clipboard, switch view to plain text mode 

    I saw this solution on a different posting somewhere, and [so far] it seems to be working.

  3. The following user says thank you to ShamusVW for this useful post:

    d_stranz (1st June 2017)

  4. #3
    Join Date
    Jun 2018
    Posts
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QSerialPort - readyRead stops working

    Hi...i am a new user here. As per my knowledge it is more likely something related to how you process your data. When you have a lot of data arriving at the input you will likely end up with partial 'lines'. How you handle this could be the reason for the problem. You should not expect that all data will arrive aligned to the end of line character.

Similar Threads

  1. Replies: 4
    Last Post: 17th October 2014, 22:24
  2. Signals stops working
    By JaroMast in forum Qt Programming
    Replies: 4
    Last Post: 28th August 2012, 20:34
  3. gdb stops working for Qt program ???
    By enno in forum Qt Programming
    Replies: 0
    Last Post: 27th February 2011, 13:47
  4. Replies: 1
    Last Post: 6th December 2010, 16:18
  5. Application stops working unexpectedly
    By baluk in forum Newbie
    Replies: 16
    Last Post: 20th November 2010, 16:06

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.