是超过一段时间没有收到数据就触发?还是接收到\r\n之类的才触发?
离线
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()确定等待写入的当前数据量。
离线
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() 之后,开始一个定时器,估计意图是足够多数据才一起处理。
离线
谢谢解惑。
离线