Results 1 to 2 of 2

Thread: QSerialPort - Is it possible to read() and write() on separate threads?

  1. #1
    Join Date
    Mar 2006
    Thanked 2 Times in 2 Posts
    Qt products
    MacOS X Unix/X11 Windows

    Default QSerialPort - Is it possible to read() and write() on separate threads?

    We have a DLL that provides an API for a USB device we make that can appear as a USB CDC com port. We actually use a custom driver on windows for best performance along with async i/o, but we have also used serial port async file i/o in the past with reasonable success as well.

    Latency is very important in this API when it is communicating with our device, so we have structured our library so that when applications make API calls to execute commands on the device, those commands turn directly into writes on the API caller's thread so that there is no waiting for a context switch. The library also maintains a listening thread which is always waiting using wait objects on an async read for new responses. These responses get parsed and inserted into thread-safe queues for the API user to read at their convenience.

    So basically, we do most of our writing in the API caller's thread, and all of our reading in a listening thread. I have tried porting a version of our code over to using QSerialPort instead of native serial file i/o for Windows and OSX, but I am running into an error whenever I try to write() from the caller's thread (the QSerialPort is created in the listening thread):

    Qt Code:
    1. QObject: Cannot create children for a parent that is in a different thread.
    To copy to clipboard, switch view to plain text mode 

    which seems to be due to the creation of another QObject-based WriteOverlappedCompletionNotifier for the notifiers pool used by QSerialPortPrivate::startAsyncWrite().

    Is the current 5.2 version of QSerialPort limited to only doing reads and writes on the same thread? This seems very unfortunate as the underlying operating systems do not have any such thread limitations for serial port file i/o. As far as I can tell, the issue mainly has to do with the fact that all of QSerialPort's notifier classes are based on QObject.

    Does anyone have a good work around to this? I might try building my own QSerialPort that uses notifiers not based on QObject to see how far that gets me. The only real advantage QObject seems to be giving here is in the destruction of the notifiers when the port closes.

  2. #2
    Join Date
    Jan 2006
    Graz, Austria
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Unix/X11 Windows

    Default Re: QSerialPort - Is it possible to read() and write() on separate threads?

    QSerialPort is designed for usage in Qt programs, which primarily use non-blocking I/O.
    Completion of I/O operations is signalled via Qt style callbacks called signals.

    Since your architecture is designed around unbuffered threaded access, you might need to use your own abstraction of the low level system APIs that is designed to fit into your concepts.


Similar Threads

  1. QSerialPort read delay
    By M4chin3 in forum Qt Programming
    Replies: 14
    Last Post: 24th January 2014, 18:06
  2. QSerialPort :: read - Serial Comms noob.
    By llaregyb in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2014, 12:18
  3. Destruction in separate threads
    By KevinKnowles in forum Qt Programming
    Replies: 3
    Last Post: 19th March 2012, 09:49
  4. Problem with QProcess in two separate threads
    By viheaho in forum Qt Programming
    Replies: 2
    Last Post: 18th March 2010, 22:52
  5. Replies: 1
    Last Post: 16th June 2009, 09:09

Tags for this Thread


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.