您尚未登录。

楼主 #1 2019-07-26 09:27:35

Gentlepig
会员
注册时间: 2018-10-24
已发帖子: 1,363
积分: 1323.5

请教,QSerialPort类的readyRead信号是如何触发的?

是超过一段时间没有收到数据就触发?还是接收到\r\n之类的才触发?

离线

#2 2019-07-26 09:38:24

晕哥
管理员
注册时间: 2017-09-06
已发帖子: 9,342
积分: 9202

Re: 请教,QSerialPort类的readyRead信号是如何触发的?

https://doc.qt.io/qt-5/qiodevice.html

QIODevice emits readyRead() when new data is available for reading; for example, if new data has arrived on the network or if additional data is appended to a file that you are reading from. You can call bytesAvailable() to determine the number of bytes that are currently available for reading. It's common to use bytesAvailable() together with the readyRead() signal when programming with asynchronous devices such as QTcpSocket, where fragments of data can arrive at arbitrary points in time. QIODevice emits the bytesWritten() signal every time a payload of data has been written to the device. Use bytesToWrite() to determine the current amount of data waiting to be written.

google翻译:

当新数据可供读取时,QIODevice会发出readyRead(); 例如,如果新数据已到达网络,或者附加数据是否附加到您正在读取的文件中。您可以调用bytesAvailable()来确定当前可用于读取的字节数。在使用QTcpSocket等异步设备进行编程时,通常会将bytesAvailable()与readyRead()信号一起使用,其中数据片段可以在任意时间点到达。每次将数据有效负载写入设备时,QIODevice都会发出bytesWritten()信号。使用bytesToWrite()确定等待写入的当前数据量。





离线

#3 2019-07-26 09:45:27

晕哥
管理员
注册时间: 2017-09-06
已发帖子: 9,342
积分: 9202

Re: 请教,QSerialPort类的readyRead信号是如何触发的?

https://stackoverflow.com/questions/26612852/how-does-readyread-work-in-qt

#include "serialportreader.h"

#include <QCoreApplication>

QT_USE_NAMESPACE

SerialPortReader::SerialPortReader(QSerialPort *serialPort, QObject *parent)
    : QObject(parent)
    , m_serialPort(serialPort)
    , m_standardOutput(stdout)
{
    connect(m_serialPort, SIGNAL(readyRead()), SLOT(handleReadyRead()));
    connect(m_serialPort, SIGNAL(error(QSerialPort::SerialPortError)), SLOT(handleError(QSerialPort::SerialPortError)));
    connect(&m_timer, SIGNAL(timeout()), SLOT(handleTimeout()));

    m_timer.start(5000);
}

SerialPortReader::~SerialPortReader()
{
}

void SerialPortReader::handleReadyRead()
{
    m_readData.append(m_serialPort->readAll());

    if (!m_timer.isActive())
        m_timer.start(5000);
}

void SerialPortReader::handleTimeout()
{
    if (m_readData.isEmpty()) {
        m_standardOutput << QObject::tr("No data was currently available for reading from port %1").arg(m_serialPort->portName()) << endl;
    } else {
        m_standardOutput << QObject::tr("Data successfully received from port %1").arg(m_serialPort->portName()) << endl;
        m_standardOutput << m_readData << endl;
    }

    QCoreApplication::quit();
}

void SerialPortReader::handleError(QSerialPort::SerialPortError serialPortError)
{
    if (serialPortError == QSerialPort::ReadError) {
        m_standardOutput << QObject::tr("An I/O error occurred while reading the data from port %1, error: %2").arg(m_serialPort->portName()).arg(m_serialPort->errorString()) << endl;
        QCoreApplication::exit(1);
    }
}

从上面这个讨论看,
我感觉只要有数据到达,哪怕是一个字节, 都会 emit readyRead()  这个信号,从而触发槽函数。

所以他加了一个定时器, 触发readyRead() 之后,开始一个定时器,估计意图是足够多数据才一起处理。





离线

楼主 #4 2019-07-26 10:06:23

Gentlepig
会员
注册时间: 2018-10-24
已发帖子: 1,363
积分: 1323.5

Re: 请教,QSerialPort类的readyRead信号是如何触发的?

谢谢解惑。

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn