PDA

View Full Version : QSerialPort - readyRead stops working



ShamusVW
30th May 2017, 15:35
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...


serial = new QSerialPort(this);
...
openSerialPort();
...
connect(serial, static_cast<void (QSerialPort::*)(QSerialPort::SerialPortError)>(&QSerialPort::error), this, &MainWindow::handleError);
connect(serial, &QSerialPort::readyRead, this, &MainWindow::readData);


and my openSerialPort()...


void MainWindow::openSerialPort()
{
serial->setPortName(m_ArduinoPort);
serial->setBaudRate(QSerialPort::Baud9600);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
if (serial->isOpen())
serial->close();
serial->clearError();
if (serial->open(QIODevice::ReadWrite)) {
console->setEnabled(true);
console->insertPlainText("Port " + m_ArduinoPort + " opened...\n");
} else {
console->insertPlainText("Port open error!\n");
console->insertPlainText(serial->errorString());
}
}


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.

ShamusVW
31st May 2017, 14:56
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.


if(!Serial) { //check if Serial is available... if not,
Serial.end(); // close serial port
delay(100); //wait 100 millis
Serial.begin(9600); // reenable serial again
}
Serial.println(3);

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

EbaYau
22nd June 2018, 20:20
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.