Problem
QSerialPort
from version 5.13.1 of the Qt library does not physically output data under Windows 7 and 10.
Example
In order to demonstrate the described problem I have prepared the following setup:
- Hardware
I have tested the connection between a PC with a physical serial port (COM1) and a real serial device, but for demonstration purposes I have created a simple loopback by connecting together pins 2 and 3 of the DSub connector of the PC, i.e. Tx and Rx.
- Software
The problem occurs in my own GUI applications, as well as in the official examples shipped with Qt. However, for the sake of the demonstration I wrote a very basic console app:
SerialBug.pro
QT -= gui
QT += serialport
CONFIG += console
SOURCES += \
main.cpp
main.cpp
#include <QCoreApplication>
#include <QSerialPort>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSerialPort port("COM1");
port.setBaudRate(QSerialPort::Baud4800);
port.setDataBits(QSerialPort::Data8);
port.setStopBits(QSerialPort::OneStop);
port.setParity(QSerialPort::NoParity);
port.setFlowControl(QSerialPort::NoFlowControl);
QObject::connect(&port, &QSerialPort::readyRead,
[&port](){
qDebug() << port.readAll();
});
QObject::connect(&port, &QSerialPort::bytesWritten,
[](qint64 bytes){
qDebug() << bytes;
});
QObject::connect(&port, &QSerialPort::errorOccurred,
[](QSerialPort::SerialPortError error){
qDebug() << error;
});
if (port.open(QSerialPort::ReadWrite)) {
qDebug() << port.write("Test");
}
return a.exec();
}
Result
Compiling and running the example code with MSVC2017 and Qt 5.13.0 in release mode, the following output is produced:
QSerialPort::NoError
4
4
"Test"
The exact same code compiled in release mode with MSVC2017, but this time with Qt 5.13.1, produces the following output:
QSerialPort::NoError
4
port.write
returns 4
, meaning 4 bytes are send to the serial port, but that is not actually done. bytesWritten
is not emitted and the data is not read back.
Note: A serial monitor program is showing the written data, but the data does not reach the pins.
Is it possible to fix the code in any way in order to make it work with Qt5.13.1?