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.