【问题标题】:Reading continous stream of incoming bytes at serial port (QtSerialPort)在串行端口(QtSerialPort)读取传入字节的连续流
【发布时间】:2021-07-04 16:26:42
【问题描述】:

所以我实际上让它工作了,但它非常慢。我没有使用 Qt 库的经验,也很少使用串行通信和 c++,所以我确信我的方法效率极低。

设置:我正在为我的汽车制作数字仪表板,ECU 可以通过串行连接发送实时发动机参数。

  1. 向 ECU 发送连接请求(一次)
  2. 发送参数请求,例如rpm(一次)
  3. 它会发送一个连续的字节流(直到您请求它停止)。格式为“FF02----”重复,十六进制中FF只是一个分隔符,02是后面跟着2个字节的数据,--是引擎参数的十六进制值。

More detailed here if needed

这是我现在正在运行但速度极慢的代码的一部分,从我启动引擎到我看到值表明发生了变化,大约有 5 到 10 秒的延迟。

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();
            }
        }
    }
});

我使用QObject::connect 是因为我在某处看到“//在发出readyRead() 时调用”

我应该使用什么以及在哪里更有效地读取传入的字节?

【问题讨论】:

  • 你应该在调用 readyRead() 时读取所有字节,而不是只读取一个。

标签: c++ qt serial-port qtserialport


【解决方案1】:
connect(&activeport, &QSerialPort::readyRead, this, &appmain::handledata);

connect(&yourport, &QSerialPort::readyRead, this, &functionpointer);

您需要创建一个函数并将其作为参数传递给 connect()

【讨论】:

    猜你喜欢
    • 2018-12-15
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    相关资源
    最近更新 更多