Qt: Serial Library that supports Baud Rate 250k

1.6k Views Asked by At

I've given both QSerialPort and QExtSerialPort a try and neither seem to be able to support 250k correctly. QExtSerialPort does seem to have support for it as it's a listed baud type but whenever I use it to connect to a board all I get back is junk data like the baud rate is set incorrectly. I've verified all other settings, and the board does work over 250k as I've tested using miniterm to it. Are there any other libraries or should I be trying something different with QExtSerialPort?

1

There are 1 best solutions below

7
Kuba hasn't forgotten Monica On

I presume your platform must be Linux, since on Windows there's really only one way of setting the baud rate, and if it doesn't work, then it won't work for everyone.

There is the issue of rounding. QSerialPort code simply divides the base baud generator clock by your baudrate. This is an integer division and is done with truncation. If whatever baudrate you select is not exactly supported by the hardware, the truncation may shift it in an invalid direction - there could have been a closer divisor that's larger, not smaller. You should check the baud generator base frequency and select your baud rate accordingly.

There are many ways of setting baud rates on Linux. QSerialPort does not support two methods out of the three in the cited answer. Thus it won't support some drivers that happen to support only the two out of the three methods. I don't know offhand how many drivers might suffer from that issue - it could be a red herring, after all.

Please share what is the exact driver is used for your device on Linux, and what is the kernel version. I can have a look and check if this driver would cause issues with current QSerialPort code.

To avoid rounding issues, as a temporary workaround you can extract the base baudrate generator frequency via the following hack:

// This must be at the very top of the .cpp file
#define private public
#include <QSerialPort>
#include "private/qserialport_p.h"
#include "private/qserialport_unix_p.h"
#undef private

QSerialPort * port;
...
// after the port was opened
int baudBase = port->d_ptr->currentSerialInfo.baud_base;

In the .pro file, you need QT += core-private, and this is for Qt 5. For Qt 4 you need to have Qt with available sources.

The real fix, of course, belongs in QSerialPort. I know nothing about QExtSerialPort.