Reading continous stream of incoming bytes at serial port (QtSerialPort)

  c++, qt, qtserialport, serial-port

So I actually have it working but it is extremely slow. I have no experience with Qt library and very little with serial communication and c++ so I am sure my approach is extremely inefficient.

Setup: I am working on a digital dashboard for my car, and the ECU can send live engine parameters through a serial connection.

  1. Send connection request to ECU (once)
  2. Send parameter request, for example rpm (once)
  3. It sends a continuous stream of bytes (until you request it to stop). Format is "FF02—-" repeated, in hex where FF is just a separator, 02 is that 2 bytes of data will follow, and — — are the hex values of the engine parameter.

More detailed here if needed

Here is part of the code I have now that is working but extremely slow, there is like a whole 5-10 second delay from when I rev my engine to when I see the values indicate the change.

QObject::connect(ser, &QSerialPort::readyRead, [&]
{
    if ((m_userName == "4") and (ser->bytesAvailable()))
    {
        QByteArray incomingByte = ser->read(1).toHex();

        if (incomingByte == "ff")
        {
            dataBuff = "";
        }

        dataBuff += incomingByte;

        //converting the incoming data once all 4 bytes are received
        if (dataBuff.length() == 8)
        {
            QString revLsb = tr(dataBuff.mid(6, 2));
            QString revMsb = tr(dataBuff.mid(4, 2));

            bool bStatus1 = false;
            bool bStatus2 = false;

            uint nHexLsb = revLsb.toUInt(&bStatus1,16);
            uint nHexMsb = revMsb.toUInt(&bStatus2,16);

            float revlsb = nHexLsb * 12.5 * 256.0;
            float rev =  nHexMsb * 12.5 + revlsb;

            if ((rev != 0) & (rev != 12.5))
            {
                rev = rev / 1000;
                qDebug() << "Revs: " << rev << " rpm";
                m_rpmVal = rev;
                emit rpmValChanged();
            }
        }
    }
});

I’m using QObject::connect because I saw somewhere "//This is called when readyRead() is emitted"

What and where should I be using to read the incoming bytes more efficiently?

Source: Windows Questions C++

LEAVE A COMMENT