QSerialPort: readyRead() signal is emitted 2 times instead of 1 time
-
Hello everyone!
Now I try to study using of QSerialPort with QThread. And during the study I got some strange thing: I get 2 readyRead() signals => 2 readyRead() slots, and I can't to construct response data as one.
What I have:
- I have the class for QSerialPort, there are some slots for open port, for write to port, for read from port. Slot for reading from port s connected to readyRead() signal in constructor of the class:
connect(serialPort, &QSerialPort::readyRead, this, &SerialPort_Class::slot_ReadInPort);Slot for writing to port:
void SerialPort_Class::slot_WriteToPort(QByteArray writeData) { qDebug() << "SLOT WriteToPort" << endl; if (serialPort->isOpen()) { QByteArray readData = serialPort->readAll(); qDebug() << "Write Request = " << writeData << endl; serialPort->write(writeData); if (serialPort->waitForBytesWritten(1000)) {} else { qDebug() << "Write request TIMEOUT" << endl; } } }Slot for readyRead:
void SerialPort_Class::slot_ReadInPort() { QThread::msleep(1000); qDebug() << "EMIT SendReadData" << endl; emit signal_SendReadData(""); }- I have the Thread for emitting of signal for writing to port after opening the port and after slot for readyRead() for processing of response data.
And what I get in the Debug log (only 1 write command per opening the port):
"No error" checkDevices_slot SLOT WriteToPort Write Request = "\xA5\x01""c0000\xC9" EMIT SendReadDataThis is a normal situation: one request - one response (
EMIT SendReadDatamessage).
But often I see the next situation for one cycle:"No error" checkDevices_slot SLOT WriteToPort Write Request = "\xA5\x01""c0000\xC9" EMIT SendReadData EMIT SendReadDataThis is a unnormal situation: one request - two responses (2
EMIT SendReadDatamessages).And I can't to fix it! I used waitForReadyRead() method even with 1sec delay - and the same problem. I even removed all reading functions in slot (like now) - and the same problem. Maybe, it exists because of baudrate = 9600... I don't know.
Hope for your help! Thank you in advance!
-
@J-Hilk said in QSerialPort: readyRead() signal is emitted 2 times instead of 1 time:
@oBOXPOH first of all, do not
QThread::msleep(1000);in your slot!This is a normal situation: one request - one response (EMIT SendReadData message).
thats actually the exception. Normal is: "An unspecified amount of readyRead Signals per request send"
with a normal Serialport connection, there is no guarantee that all data is already there, when the signal is emitted. Usually one has some kind of protocol on top of the serial port to fix this issue.
You only get a signal, notifying you that there is some data available to fetch, and you have to decide if ist the complete answer yet or not.
I used msleep() method only to recognize the problem, when I get the second signal.
I understand, that signal only notifying me that I have some data. And I used waitForReadyRead() method to catch all. But anyway I got the second readyRead() signal without, by the way, response data.
What do you advice to do with processing of such situations?
@oBOXPOH said in QSerialPort: readyRead() signal is emitted 2 times instead of 1 time:
What do you advice to do with processing of such situations?
-
Hello everyone!
Now I try to study using of QSerialPort with QThread. And during the study I got some strange thing: I get 2 readyRead() signals => 2 readyRead() slots, and I can't to construct response data as one.
What I have:
- I have the class for QSerialPort, there are some slots for open port, for write to port, for read from port. Slot for reading from port s connected to readyRead() signal in constructor of the class:
connect(serialPort, &QSerialPort::readyRead, this, &SerialPort_Class::slot_ReadInPort);Slot for writing to port:
void SerialPort_Class::slot_WriteToPort(QByteArray writeData) { qDebug() << "SLOT WriteToPort" << endl; if (serialPort->isOpen()) { QByteArray readData = serialPort->readAll(); qDebug() << "Write Request = " << writeData << endl; serialPort->write(writeData); if (serialPort->waitForBytesWritten(1000)) {} else { qDebug() << "Write request TIMEOUT" << endl; } } }Slot for readyRead:
void SerialPort_Class::slot_ReadInPort() { QThread::msleep(1000); qDebug() << "EMIT SendReadData" << endl; emit signal_SendReadData(""); }- I have the Thread for emitting of signal for writing to port after opening the port and after slot for readyRead() for processing of response data.
And what I get in the Debug log (only 1 write command per opening the port):
"No error" checkDevices_slot SLOT WriteToPort Write Request = "\xA5\x01""c0000\xC9" EMIT SendReadDataThis is a normal situation: one request - one response (
EMIT SendReadDatamessage).
But often I see the next situation for one cycle:"No error" checkDevices_slot SLOT WriteToPort Write Request = "\xA5\x01""c0000\xC9" EMIT SendReadData EMIT SendReadDataThis is a unnormal situation: one request - two responses (2
EMIT SendReadDatamessages).And I can't to fix it! I used waitForReadyRead() method even with 1sec delay - and the same problem. I even removed all reading functions in slot (like now) - and the same problem. Maybe, it exists because of baudrate = 9600... I don't know.
Hope for your help! Thank you in advance!
@oBOXPOH said in QSerialPort: readyRead() signal is emitted 2 times instead of 1 time:
readyRead
There is no guarantee that readyRead will be emitted only once for each write - data can come in several packages. You should also print data you get in slot_ReadInPort() to see whether this is indeed the case.
-
Hello everyone!
Now I try to study using of QSerialPort with QThread. And during the study I got some strange thing: I get 2 readyRead() signals => 2 readyRead() slots, and I can't to construct response data as one.
What I have:
- I have the class for QSerialPort, there are some slots for open port, for write to port, for read from port. Slot for reading from port s connected to readyRead() signal in constructor of the class:
connect(serialPort, &QSerialPort::readyRead, this, &SerialPort_Class::slot_ReadInPort);Slot for writing to port:
void SerialPort_Class::slot_WriteToPort(QByteArray writeData) { qDebug() << "SLOT WriteToPort" << endl; if (serialPort->isOpen()) { QByteArray readData = serialPort->readAll(); qDebug() << "Write Request = " << writeData << endl; serialPort->write(writeData); if (serialPort->waitForBytesWritten(1000)) {} else { qDebug() << "Write request TIMEOUT" << endl; } } }Slot for readyRead:
void SerialPort_Class::slot_ReadInPort() { QThread::msleep(1000); qDebug() << "EMIT SendReadData" << endl; emit signal_SendReadData(""); }- I have the Thread for emitting of signal for writing to port after opening the port and after slot for readyRead() for processing of response data.
And what I get in the Debug log (only 1 write command per opening the port):
"No error" checkDevices_slot SLOT WriteToPort Write Request = "\xA5\x01""c0000\xC9" EMIT SendReadDataThis is a normal situation: one request - one response (
EMIT SendReadDatamessage).
But often I see the next situation for one cycle:"No error" checkDevices_slot SLOT WriteToPort Write Request = "\xA5\x01""c0000\xC9" EMIT SendReadData EMIT SendReadDataThis is a unnormal situation: one request - two responses (2
EMIT SendReadDatamessages).And I can't to fix it! I used waitForReadyRead() method even with 1sec delay - and the same problem. I even removed all reading functions in slot (like now) - and the same problem. Maybe, it exists because of baudrate = 9600... I don't know.
Hope for your help! Thank you in advance!
@oBOXPOH first of all, do not
QThread::msleep(1000);in your slot!This is a normal situation: one request - one response (EMIT SendReadData message).
thats actually the exception. Normal is: "An unspecified amount of readyRead Signals per request send"
with a normal Serialport connection, there is no guarantee that all data is already there, when the signal is emitted. Usually one has some kind of protocol on top of the serial port to fix this issue.
You only get a signal, notifying you that there is some data available to fetch, and you have to decide if ist the complete answer yet or not.
-
@oBOXPOH first of all, do not
QThread::msleep(1000);in your slot!This is a normal situation: one request - one response (EMIT SendReadData message).
thats actually the exception. Normal is: "An unspecified amount of readyRead Signals per request send"
with a normal Serialport connection, there is no guarantee that all data is already there, when the signal is emitted. Usually one has some kind of protocol on top of the serial port to fix this issue.
You only get a signal, notifying you that there is some data available to fetch, and you have to decide if ist the complete answer yet or not.
@J-Hilk said in QSerialPort: readyRead() signal is emitted 2 times instead of 1 time:
@oBOXPOH first of all, do not
QThread::msleep(1000);in your slot!This is a normal situation: one request - one response (EMIT SendReadData message).
thats actually the exception. Normal is: "An unspecified amount of readyRead Signals per request send"
with a normal Serialport connection, there is no guarantee that all data is already there, when the signal is emitted. Usually one has some kind of protocol on top of the serial port to fix this issue.
You only get a signal, notifying you that there is some data available to fetch, and you have to decide if ist the complete answer yet or not.
I used msleep() method only to recognize the problem, when I get the second signal.
I understand, that signal only notifying me that I have some data. And I used waitForReadyRead() method to catch all. But anyway I got the second readyRead() signal without, by the way, response data.
What do you advice to do with processing of such situations?
-
@J-Hilk said in QSerialPort: readyRead() signal is emitted 2 times instead of 1 time:
@oBOXPOH first of all, do not
QThread::msleep(1000);in your slot!This is a normal situation: one request - one response (EMIT SendReadData message).
thats actually the exception. Normal is: "An unspecified amount of readyRead Signals per request send"
with a normal Serialport connection, there is no guarantee that all data is already there, when the signal is emitted. Usually one has some kind of protocol on top of the serial port to fix this issue.
You only get a signal, notifying you that there is some data available to fetch, and you have to decide if ist the complete answer yet or not.
I used msleep() method only to recognize the problem, when I get the second signal.
I understand, that signal only notifying me that I have some data. And I used waitForReadyRead() method to catch all. But anyway I got the second readyRead() signal without, by the way, response data.
What do you advice to do with processing of such situations?
@oBOXPOH said in QSerialPort: readyRead() signal is emitted 2 times instead of 1 time:
What do you advice to do with processing of such situations?