Search code examples
c++qtqt5c++14qtserialport

How to set low latency in serial port open by QSerialPort


I have already developed an application and it performs serial communication with sensors. For an unknown reason, my received data is slow with QT C++ Framework.

I have tested the sample code (https://www.pjrc.com/tmp/host_software/receive_test.c). I received data in the appropriate time with this sample code. Here i found that low latency mode is set ASYNC_LOW_LATENCY.

I have read (https://doc.qt.io/qt-5/qserialport.html) but didn't get any idea about how to set low latency with QSerialPort

Q1: Please give a sample code in qt c++ with QSerialPort on how to set low latency.

Please don't suggest writing c code inside qt c++ this is not the right approach to solve this probleam.

QSerialPort *pUsbSerialPort;
pUsbSerialPort = new QSerialPort();
if(IS_VALID_OBJ(pUsbSerialPort))
{
    pUsbSerialPort->setPortName(sSerialPort);
    pUsbSerialPort->setBaudRate(BaudRate);
    pUsbSerialPort->setDataBits(QSerialPort::Data8);
    pUsbSerialPort->setParity(QSerialPort::NoParity);
    pUsbSerialPort->setStopBits(QSerialPort::OneStop);
    pUsbSerialPort->setFlowControl(QSerialPort::NoFlowControl);

    if(pUsbSerialPort->open(QIODevice::ReadWrite))
    {
        connect(pUsbSerialPort, &QSerialPort::readyRead,this , &Laser::LaserReadyRead);
        PRINT_INFO("Serial port successfully initialized" + sSerialPort);
        bIsServeropen = true;
    }
    else
    {
        PRINT_INFO("Serial port initialization failed" + sSerialPort);
        return;
    }
}
else
{
    PRINT_INFO(" Failed to assign memory to pUsbSerialPort" + sSerialPort);
    return;
}

Solution

  • The descriptor is the Handle of the QSerialPort:

    #include <sys/ioctl.h>
    #include <linux/serial.h>
    
    // ...
    
    pUsbSerialPort->open(QIODevice::ReadOnly);
    
    int fd = pUsbSerialPort->handle();
    struct serial_struct kernel_serial_settings;
    ::ioctl(fd, TIOCGSERIAL, &kernel_serial_settings);
    kernel_serial_settings.flags |= ASYNC_LOW_LATENCY;
    ::ioctl(fd, TIOCSSERIAL, &kernel_serial_settings);