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:
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).QObject::killTimer: Timers cannot be stopped from another thread
QObject::startTimer: Timers cannot be started from another thread
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:
{ Q_OBJECT public: QMutex quit; SerialThread() { } ~SerialThread() { quit.lock(); wait(); } { buffer = data; } { serial->write(data); if(serial->waitForReadyRead(1000)) { data = serial->readAll(); return true; } return false; } private: QMutex mutex; QSerialPort * serial; QByteArray buffer; virtual void run() { mutex.lock(); QSerialPort port; serial = &port; port.setPortName("COM10"); port.open(QSerialPort::ReadWrite); while (quit.tryLock()) { quit.unlock(); if(!buffer.isEmpty()) { serial->write(buffer); buffer.clear(); if(serial->waitForReadyRead(1000)) { emit onRead(serial->readAll()); } else emit onError(); } mutex.unlock(); msleep(10); mutex.lock(); } } signals: void onError(); };To copy to clipboard, switch view to plain text mode
Bookmarks