QSerialPort 能否读取超过 512 字节的数据?
Can QSerialPort read more than 512 bytes of data?
我想使用 QSerialPort 读取从设备传输的数据。设备每次发送一帧4000个数据字节。我尝试使用以下简单代码
QSerialPort *serialPort;
char receivedData[4000];
int numRead = 0;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
/* Initialize serial port*/
serialPort = new QSerialPort(this);
QString portName = "COM6";
qint32 baudRate = 460800;
serialPort->setPortName(portName);
serialPort->setBaudRate(baudRate);
serialPort->setDataBits(QSerialPort::Data8);
serialPort->setParity(QSerialPort::NoParity);
serialPort->setStopBits(QSerialPort::OneStop);
serialPort->setFlowControl(QSerialPort::NoFlowControl);
serialPort->setReadBufferSize(4000);
if (!serialPort->open(QIODevice::ReadOnly)) {
qDebug() << "Cannot open comport";
}
connect(serialPort, SIGNAL(readyRead()), this, SLOT(serialReceived()));
}
void MainWindow::serialReceived()
{
numRead = serialPort->read(receivedData, 4000);
serialPort->flush();
}
问题是:一直显示只读取了512个数据字节。如何读取整个 4000 字节的数据帧? (当我使用 Matlab 读取这个 4000 字节的帧时,它工作正常)
没有限制,但您不一定会在单个块中接收所有数据。
你必须一直听,直到你有你正在等待的字节数(或超时)。
void MainWindow::serialReceived()
{
receivedData.append(serialPort->readAll());
if(receivedData.size() >= 4000) {
// we're full
}
}
你通常必须循环读取数据(以确保你得到所有数据),这里是一段示例代码,它等同于你的 serialReceived() 函数,除了它使用 emit rxDataReady(newData);
给正在听的人...
void QSerialPortReader::handleReadyRead()
{
QByteArray newData;
// Get the data
while (mp_serialPort->bytesAvailable())
{
newData.append(mp_serialPort->readAll());
}
emit rxDataReady(newData);
}
编辑
虽然我没有做任何最大尺寸检查...但是如果你需要它添加它是微不足道的(即只需使用 read(..., spaceAvail) 而不是 readAll 然后递减 spaceAvail...
我想使用 QSerialPort 读取从设备传输的数据。设备每次发送一帧4000个数据字节。我尝试使用以下简单代码
QSerialPort *serialPort;
char receivedData[4000];
int numRead = 0;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
/* Initialize serial port*/
serialPort = new QSerialPort(this);
QString portName = "COM6";
qint32 baudRate = 460800;
serialPort->setPortName(portName);
serialPort->setBaudRate(baudRate);
serialPort->setDataBits(QSerialPort::Data8);
serialPort->setParity(QSerialPort::NoParity);
serialPort->setStopBits(QSerialPort::OneStop);
serialPort->setFlowControl(QSerialPort::NoFlowControl);
serialPort->setReadBufferSize(4000);
if (!serialPort->open(QIODevice::ReadOnly)) {
qDebug() << "Cannot open comport";
}
connect(serialPort, SIGNAL(readyRead()), this, SLOT(serialReceived()));
}
void MainWindow::serialReceived()
{
numRead = serialPort->read(receivedData, 4000);
serialPort->flush();
}
问题是:一直显示只读取了512个数据字节。如何读取整个 4000 字节的数据帧? (当我使用 Matlab 读取这个 4000 字节的帧时,它工作正常)
没有限制,但您不一定会在单个块中接收所有数据。 你必须一直听,直到你有你正在等待的字节数(或超时)。
void MainWindow::serialReceived()
{
receivedData.append(serialPort->readAll());
if(receivedData.size() >= 4000) {
// we're full
}
}
你通常必须循环读取数据(以确保你得到所有数据),这里是一段示例代码,它等同于你的 serialReceived() 函数,除了它使用 emit rxDataReady(newData);
给正在听的人...
void QSerialPortReader::handleReadyRead()
{
QByteArray newData;
// Get the data
while (mp_serialPort->bytesAvailable())
{
newData.append(mp_serialPort->readAll());
}
emit rxDataReady(newData);
}
编辑
虽然我没有做任何最大尺寸检查...但是如果你需要它添加它是微不足道的(即只需使用 read(..., spaceAvail) 而不是 readAll 然后递减 spaceAvail...