How to make QSerialPort from Qt5.13.1 work?

前端 未结 1 1867
时光取名叫无心
时光取名叫无心 2021-01-12 21:45

Problem

QSerialPort from version 5.13.1 of the Qt library does not physically output data under Windows 7 and 10.

Example

In order t

相关标签:
1条回答
  • 2021-01-12 21:46

    Cause

    Searching Qt bug tracker there seem to be multiple bugs about QSerialPort not working on Qt 5.13.1 on Windows. All of them are duplicated with QTBUG-78086 which also contains a link to Gerrit review of the fix.

    From the bug description:

    The signal readyRead is never emitted, even if data is sent to the serial port from a connected device. The member bytesAvailable returns 0 even if data has been sent to the serial port from a connected device.

    Basically, they have tried to emit _q_notify in qwinoverlappedionotifier.cpp only if there's no notification pending. Unfortunatelly

    That commit completely breaks the I/O on Windows.

    Solution

    For now you have the options to downgrade to 5.13.0, wait for Qt 5.13.2 or

    Fix the Qt 5.13.1 qserialport yourself:

    • open QTDIR\5.13.1\Src\qtserialport\qtserialport.pro with QtCreator
    • (optional) you might need to select a kit, e.g. Projects -> Manage kits -> Desktop Qt 5.13.1 MSVC2017 64bit
    • in the project tree open src/serialport/serialport-lib/sources/qwinoverlappedionotifier.cpp
    • delete QAtomicInt pendingNotifications;
    • change

      if (!waiting && pendingNotifications-- == 0)
          emit q->_q_notify();
      

      to

      if (!waiting)
          emit q->_q_notify();
      
    • change

      int n = pendingNotifications.fetchAndStoreAcquire(0);
      while (--n >= 0) {
          if (WaitForSingleObject(hSemaphore, 0) == WAIT_OBJECT_0)
              dispatchNextIoResult();
      }
      

      to

      if (WaitForSingleObject(hSemaphore, 0) == WAIT_OBJECT_0)
          dispatchNextIoResult();
      
    • In QtCreator go to build -> clean all, then run qmake, then rebuild all

    • locate the build folder, then copy and replace Qt5SerialPort.dll and Qt5SerialPortd.dll from build\bin to QTDIR\5.13.1\msvc2017_64\bin

    Your code should work now.

    0 讨论(0)
提交回复
热议问题