
Originally Posted by
high_flyer
did I misunderstand?
Not certainly in that way.
In fact, device->open()/close() is just the emission of a signal. I emit a signal switchStateSig, then the slot switchState in the thread of the device is called. I do not control this, I just go further in the thread of the algorithm.
I can not call directly, because the device and the algorithm are in different threads, so I had to connect them with a signal and a slot.
This should work like this: from the thread of the algorithm, I call the open/close of the device, I wait for an answer, and only then I go further. Now I just emit a signal and, without waiting for an answer, I go forward. This is very bad. This is the first problem.
The second problem is that the function waitForReadyRead() unpredictably breaks the whole program. I tried to do this, but I did not get a single byte. Apparently, something is blocked:
void ms_delay(int ms){
QElapsedTimer ms_timer;
ms_timer.start();
while(ms_timer.elapsed() < ms){}
return;
}
{
mutex->lock();
qDebug() << "-------------------------";
if(!serial->isOpen())
open();
int attempts = 1;
QElapsedTimer waitTimer;
readBuf.clear();
while (attempts <= 3) {
if (serial->isWritable())
{
serial->write(data);
waitTimer.restart();
while (waitTimer.elapsed() < 333){
ms_delay(5);
readBuf += serial->readAll();
if (readBuf.size() == 4){
close();
mutex->unlock();
return readBuf;
}
}
readBuf.clear();
qDebug() << "Timeout...";
close();
open();
attempts++;
}
else
{
qDebug() << "Port is not written";
close();
mutex->unlock();
return 0;
}
}
close();
mutex->unlock();
return 0;
}
void ms_delay(int ms){
QElapsedTimer ms_timer;
ms_timer.start();
while(ms_timer.elapsed() < ms){}
return;
}
QByteArray ComPort::requestResponse(const QByteArray &data)
{
mutex->lock();
qDebug() << "-------------------------";
if(!serial->isOpen())
open();
int attempts = 1;
QElapsedTimer waitTimer;
readBuf.clear();
while (attempts <= 3) {
if (serial->isWritable())
{
serial->write(data);
waitTimer.restart();
while (waitTimer.elapsed() < 333){
ms_delay(5);
readBuf += serial->readAll();
if (readBuf.size() == 4){
close();
mutex->unlock();
return readBuf;
}
}
readBuf.clear();
qDebug() << "Timeout...";
close();
open();
attempts++;
}
else
{
qDebug() << "Port is not written";
close();
mutex->unlock();
return 0;
}
}
close();
mutex->unlock();
return 0;
}
To copy to clipboard, switch view to plain text mode
Bookmarks