Important: Please read the Qt Code of Conduct -

QSerialPort handle on Windows

  • Hi. I've a problem trying to use QSerialPort's handle() method under Windows OS.

    I'd like to open a QSerialPort and then grab its handle for performing some operations on the port using the native Windows API (I need to do that because I have to obey some requests from a third-party library).

    Unfortunately, when I try to write some data on the Windows HANDLE returned by QSerialPort's handle() method then I have no effects: no data are being actually written on the port.

    Under Linux everything is fine, instead: writing data on the QSerialPort's handle() using the native API succeeds.

    Why on Windows it is not working? Any idea?


  • Lifetime Qt Champion


    Maybe a silly question but did you check that you get a valid handle ?

  • Qt Champions 2020

    I don't sure that it is correct to do I/O (ReadFile/WriteFile) to QSerialPort using handle(). In this case you do not need then QSerialPort at all!!!

    1. QSerialPort opens device in overlapped mode, so, maybe it is reason, or you do ReadFile/WriteFile wrong.
    2. QSerialPort "listen" any I/O events on HANDLE with IOCP and process it automatically, so, maybe it is reason.
    3. Maybe something else.

    QSerialPort::handle() it is possible to use to change any other features with DeviceIoControl (e.g. timeouts, speed and so on), but I don't think, that ReadFile/WriteFile is possible... Please use QSerialPort::handle() on your own risk. :)

  • The overlapped mode is the problem. If you only have a handle then ReadFile / WriteFile will fail if you don't include the overlapped structure in the call. It was opened with the OVERLAPPED flag so this parameter cannot be NULL.

    What exactly are you trying to do? Maybe there is another way.