From eb0535c9701202d5f74fe1fc9b3b094bbecb8043 Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Tue, 8 Sep 2015 22:05:49 +0300 Subject: Use the termios v2 to custom baud rate switching For a long time the Linux kernel has support new version of the termios structure (V2). To setup the user speed it is necessary to do following steps: * Query current content of termios2 by calling the ioctl() with the TCGETS2 flag. * Add to the c_cflag field the BOTHER flag. * Set to the c_ispeed/c_ospeed fields an actual values of custom speeds. * Write new content of termios2 back by calling the ioctl() with the TCSETS2 flag. This new method much simpler and is more transparent in implementation than changes of a custom divisor. It is preferable and will be used by default for all cases. If for some reason a current Linux kernel doesn't support the termios2, then will be falling back to the old method with changing of a custom divisor. Tested with the on-board and the USB (PL2303) serial ports. Task-number: QTBUG-48094 Change-Id: I49a5389b089980b616b4e2ff815ce0b579752d0e Reviewed-by: Sergey Belyashov Reviewed-by: Massimo Callegari Reviewed-by: Denis Shienkov --- src/serialport/qserialport_unix.cpp | 57 +++++++++++++++++++++++++++++- tests/auto/qserialport/tst_qserialport.cpp | 10 ++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/serialport/qserialport_unix.cpp b/src/serialport/qserialport_unix.cpp index b845de7..9b560be 100644 --- a/src/serialport/qserialport_unix.cpp +++ b/src/serialport/qserialport_unix.cpp @@ -52,6 +52,33 @@ #define CRTSCTS (IHFLOW | OHFLOW) #endif +#ifdef Q_OS_LINUX + +struct termios2 { + tcflag_t c_iflag; /* input mode flags */ + tcflag_t c_oflag; /* output mode flags */ + tcflag_t c_cflag; /* control mode flags */ + tcflag_t c_lflag; /* local mode flags */ + cc_t c_line; /* line discipline */ + cc_t c_cc[19]; /* control characters */ + speed_t c_ispeed; /* input speed */ + speed_t c_ospeed; /* output speed */ +}; + +#ifndef TCGETS2 +#define TCGETS2 _IOR('T', 0x2A, struct termios2) +#endif + +#ifndef TCSETS2 +#define TCSETS2 _IOW('T', 0x2B, struct termios2) +#endif + +#ifndef BOTHER +#define BOTHER 0010000 +#endif + +#endif + #include #include @@ -412,7 +439,17 @@ bool QSerialPortPrivate::setBaudRate() bool QSerialPortPrivate::setStandardBaudRate(qint32 baudRate, QSerialPort::Directions directions) { #ifdef Q_OS_LINUX - // try to clear custom baud rate + // try to clear custom baud rate, using termios v2 + struct termios2 tio2; + if (::ioctl(descriptor, TCGETS2, &tio2) != -1) { + if (tio2.c_cflag & BOTHER) { + tio2.c_cflag &= ~BOTHER; + tio2.c_cflag |= CBAUD; + ::ioctl(descriptor, TCSETS2, &tio2); + } + } + + // try to clear custom baud rate, using serial_struct (old way) struct serial_struct serial; ::memset(&serial, 0, sizeof(serial)); if (::ioctl(descriptor, TIOCGSERIAL, &serial) != -1) { @@ -446,6 +483,24 @@ bool QSerialPortPrivate::setStandardBaudRate(qint32 baudRate, QSerialPort::Direc bool QSerialPortPrivate::setCustomBaudRate(qint32 baudRate, QSerialPort::Directions directions) { + struct termios2 tio2; + + if (::ioctl(descriptor, TCGETS2, &tio2) != -1) { + tio2.c_cflag &= ~CBAUD; + tio2.c_cflag |= BOTHER; + + if (directions & QSerialPort::Input) + tio2.c_ispeed = baudRate; + + if (directions & QSerialPort::Output) + tio2.c_ospeed = baudRate; + + if (::ioctl(descriptor, TCSETS2, &tio2) != -1 + && ::ioctl(descriptor, TCGETS2, &tio2) != -1) { + return true; + } + } + struct serial_struct serial; if (::ioctl(descriptor, TIOCGSERIAL, &serial) == -1) { diff --git a/tests/auto/qserialport/tst_qserialport.cpp b/tests/auto/qserialport/tst_qserialport.cpp index 1a2813d..893982b 100644 --- a/tests/auto/qserialport/tst_qserialport.cpp +++ b/tests/auto/qserialport/tst_qserialport.cpp @@ -345,6 +345,8 @@ void tst_QSerialPort::baudRate_data() QTest::newRow("Baud38400") << static_cast(QSerialPort::Baud38400); QTest::newRow("Baud57600") << static_cast(QSerialPort::Baud57600); QTest::newRow("Baud115200") << static_cast(QSerialPort::Baud115200); + + QTest::newRow("31250") << 31250; // custom baudrate (MIDI) } void tst_QSerialPort::baudRate() @@ -1099,6 +1101,14 @@ void tst_QSerialPort::readWriteWithDifferentBaudRate_data() QTest::newRow("9600, 9600") << 9600 << 9600 << true; QTest::newRow("115200, 115200") << 115200 << 115200 << true; QTest::newRow("9600, 115200") << 9600 << 115200 << false; + + QTest::newRow("31250, 31250") << 31250 << 31250 << true; // custom baudrate (MIDI) + QTest::newRow("31250, 115200") << 31250 << 115200 << false; + +#ifdef Q_OS_LINUX + QTest::newRow("14400, 14400") << 14400 << 14400 << true; // custom baudrate for Linux + QTest::newRow("14400, 115200") << 14400 << 115200 << false; +#endif } void tst_QSerialPort::readWriteWithDifferentBaudRate() -- cgit v1.2.1