Quote Originally Posted by high_flyer View Post
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:
Qt Code:
  1. void ms_delay(int ms){
  2. QElapsedTimer ms_timer;
  3. ms_timer.start();
  4. while(ms_timer.elapsed() < ms){}
  5. return;
  6. }
  7.  
  8. QByteArray ComPort::requestResponse(const QByteArray &data)
  9. {
  10. mutex->lock();
  11. qDebug() << "-------------------------";
  12. if(!serial->isOpen())
  13. open();
  14. int attempts = 1;
  15. QElapsedTimer waitTimer;
  16. readBuf.clear();
  17. while (attempts <= 3) {
  18. if (serial->isWritable())
  19. {
  20. serial->write(data);
  21. waitTimer.restart();
  22. while (waitTimer.elapsed() < 333){
  23. ms_delay(5);
  24. readBuf += serial->readAll();
  25. if (readBuf.size() == 4){
  26. close();
  27. mutex->unlock();
  28. return readBuf;
  29. }
  30. }
  31. readBuf.clear();
  32. qDebug() << "Timeout...";
  33. close();
  34. open();
  35. attempts++;
  36. }
  37. else
  38. {
  39. qDebug() << "Port is not written";
  40. close();
  41. mutex->unlock();
  42. return 0;
  43. }
  44.  
  45. }
  46. close();
  47. mutex->unlock();
  48. return 0;
  49. }
To copy to clipboard, switch view to plain text mode