[Interest] QSerialPort - Is it possible to read() and write() on separate threads?

Colby Boles cboles at tactrix.com
Tue Apr 1 02:54:29 CEST 2014


[I am reposting this as suggested from
http://qt-project.org/forums/viewthread/40595/#169408 in hopes of maybe
reaching someone involved in the development of QSerialPort]

 

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. For
everything other than Windows, we would like to just use QSerialPort for
maximum portability.

 

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):

 

                QObject: Cannot create children for a parent that is in
a different thread.

 

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. Looking at qserialport_win.cpp, only real advantage QObject
seems to be giving here is in the destruction of the notifiers when the
port closes.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140331/9cfa97b1/attachment.html>


More information about the Interest mailing list