setBaudRate() does not actually set custom baud rate?
-
Hi,
I'm running into an issue using a serial port on Linux, and trying to set a custom baud rate. When I started having the issue I was using Qt 5.9.3; I've since updated to 5.9.4 but am having the same issue.
Specifically, I'm using the modbus device class, so I have a line:
m_ptModbus->setConnectionParameter(QModbusDevice::SerialBaudRateParameter, Qserialport::Baud115200);
… this works, it sets the baud rate to 115200. If I choose any of the other enumerated baud rates, they work as well.
I know that the qserialport class will accept an arbitrary qint32 for baud rate, and if it's not in the list of standard rates in termios.h, it creates a termios2 struct in QSerialPortPrivate::setCustomBaudRate() and will use that to attempt to set the custom rate. I need to set a rate of 250k, so I need to take advantage of that. But if I just put down
m_ptModbus->setConnectionParameter(QModbusDevice::SerialBaudRateParameter, 250000);
... it doesn't work; my baud rate will not change from whatever it was last time I ran the program.
Next I tried customizing the qserialbus class, so that I can add some qDebug()s and see what's going on. While I was at it, for good measure, I adjusted the QModbusDevice class to handle m_baudRate as a qint32 instead of a QSerialPort::BaudRate. What I see is that if I set a standard baud rate (e.g. 115200 or 57600), the whole setup works fine. If I put any number that isn't one of the standard rates, qSerialPort will report that it successfully set the baud rate, and if I query the baud rate (by m_serialPort->baudRate() within qmodbusdevice.cpp) it will tell me that the port is configured as the custom rate I put in, but the actual baud rate is whatever the last successful configuration was - so if I set it to 57600 and test, the rate is 57600; if I then set it to 250000, it will claim success but the actual baud rate of what's coming off the serial port is still 57600.
I have also confirmed that if I set a custom baud rate, within the qserialport class it gets to QSerialPortPrivate::setCustomBaudRate() and returns after running first full attempt at setting the baud rate:
struct termios2 tio2; if (::ioctl(descriptor, TCGETS2, &tio2) != -1) { tio2.c_cflag &= ~CBAUD; tio2.c_cflag |= BOTHER; tio2.c_ispeed = baudRate; tio2.c_ospeed = baudRate; if (::ioctl(descriptor, TCSETS2, &tio2) != -1 && ::ioctl(descriptor, TCGETS2, &tio2) != -1) { return true; } }
(from qserialport_unix.cpp, QSerialPortPrivate::setCustomBaudRate())
this chunk of code executes, and returns true. So it really thinks it's setting the baud rate, but it's not.Any thoughts on what might be going on here?
Thanks!
-
Could you please modify a QSP code as following:
... if (::ioctl(descriptor, TCSETS2, &tio2) != -1 && ::ioctl(descriptor, TCGETS2, &tio2) != -1) { qDebug() << "ispeed:" << tio2.c_ispeed << "ospeed:" << tio2.c_ospeed; return true; } ...
and to see an output?