Results 1 to 4 of 4

Thread: Calling QSerialPort methods from two different threads

  1. #1
    Join Date
    May 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Calling QSerialPort methods from two different threads

    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 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Calling QSerialPort methods from two different threads

    Quote Originally Posted by egzi View Post
    so it should be perfectly safe to call syncWrite method from main thread
    Only if you know for sure that none of the called code does anything thread specific.
    Which seems not to be the case here.

    You could implement the sync write in terms of doing an async write and then waiting for it to complete.

    Btw, your async write is overwriting the shared buffer, if you call it twice in a row, only the second data packet will be sent.
    Might be what you want of course.

    Cheers,
    _

  3. #3
    Join Date
    May 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Calling QSerialPort methods from two different threads

    Quote Originally Posted by anda_skoa View Post
    You could implement the sync write in terms of doing an async write and then waiting for it to complete.
    _
    Then I would have to define variables which will hold results, some signaling method to wait for the results, its a pain in the ass and all that for each and every blocking method. That is how I will do that in the end, but I hoped for some more neat method. QT supposed to be easy threaded, but instead all those mechanisms just give me a headache. I have a feeling they are good just for incrementing ints (most examples on the net).
    I wondered if I could use QTConcurrent::run and pass the thread in the thread pool. I will try when I get back my mood for qt programming.


    Quote Originally Posted by anda_skoa View Post
    Btw, your async write is overwriting the shared buffer, if you call it twice in a row, only the second data packet will be sent.
    _
    Yeah I know. It's just an example. Actually I have a blocking communication class which uses QSerialPort internally, that was easy to write. Now I want to implement an async methods but I want to have sync methods at the same time which leads me to the example-like wrapper which in turn gives me those error messages.

    Thank you.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Calling QSerialPort methods from two different threads

    Quote Originally Posted by egzi View Post
    Then I would have to define variables which will hold results
    Depends.
    If the calling thread can be allowed to further process events then it could "block" in a nested event loop.
    Even when full blocking is required, it would mostly be a matter of chosing the "Task" structure accordingly.

    Quote Originally Posted by egzi View Post
    some signaling method to wait for the results
    Binary semaphore. Acquire in caller, release in worker.

    Cheers,
    _

Similar Threads

  1. QserialPort
    By arturs in forum Newbie
    Replies: 0
    Last Post: 13th May 2015, 21:37
  2. Replies: 1
    Last Post: 1st April 2014, 09:48
  3. Replies: 5
    Last Post: 25th February 2013, 17:37
  4. question about threads: calling 'start()->"
    By ErrMania in forum Newbie
    Replies: 3
    Last Post: 23rd November 2011, 19:36
  5. Calling same method from separate threads
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 19th July 2007, 09:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.