Hi.

I am trying to write a class that makes sync and async requests to some device on a serial port, but it always shows me worrisome
messages like:
QObject::killTimer: Timers cannot be stopped from another thread
QObject::startTimer: Timers cannot be started from another thread
For the sake of presentation I wrote a simple class (below). I know I can do it other ways but this way its much easier to implement communication timeouts, request repetition, received packet completion etc. It's also easy to implement sync methods, as I can just call directly internal blocking methods of object owned by worker thread (QSerialPort in this example).
There is no event loop in worker thread, threads are synchronized, so it should be perfectly safe to call syncWrite method from main thread, but then I get those messages. Is there any easy way out to solve this problem?

Thank you.

Qt Code:
  1. class SerialThread : public QThread
  2. {
  3. Q_OBJECT
  4. public:
  5. QMutex quit;
  6.  
  7. SerialThread()
  8. {
  9.  
  10. }
  11.  
  12. ~SerialThread()
  13. {
  14. quit.lock();
  15. wait();
  16. }
  17.  
  18. void asyncWrite(QByteArray data)
  19. {
  20. QMutexLocker locker(&mutex);
  21. buffer = data;
  22. }
  23.  
  24. bool syncWrite(QByteArray &data)
  25. {
  26. QMutexLocker locker(&mutex);
  27. serial->write(data);
  28. if(serial->waitForReadyRead(1000))
  29. {
  30. data = serial->readAll();
  31. return true;
  32. }
  33. return false;
  34. }
  35.  
  36. private:
  37. QMutex mutex;
  38. QSerialPort * serial;
  39. QByteArray buffer;
  40.  
  41. virtual void run()
  42. {
  43. mutex.lock();
  44. QSerialPort port;
  45. serial = &port;
  46.  
  47. port.setPortName("COM10");
  48. port.open(QSerialPort::ReadWrite);
  49.  
  50. while (quit.tryLock()) {
  51. quit.unlock();
  52.  
  53. if(!buffer.isEmpty())
  54. {
  55. serial->write(buffer);
  56. buffer.clear();
  57. if(serial->waitForReadyRead(1000))
  58. {
  59. emit onRead(serial->readAll());
  60. }
  61. else
  62. emit onError();
  63. }
  64.  
  65.  
  66. mutex.unlock();
  67. msleep(10);
  68. mutex.lock();
  69. }
  70. }
  71. signals:
  72. void onRead(QByteArray data);
  73. void onError();
  74.  
  75. };
To copy to clipboard, switch view to plain text mode