Serial port buffer Size
-
wrote on 23 Dec 2021, 10:46 last edited by
I open the serial port in ReadWrite mode. First I have to send a command to a device to receive the data.
The packet size sent from the device is 10240 but in serial-> readAll() I only get 4096 bytes
I also use serial-> setReadBufferSize (10240) But nothing changes. Is there a limit to the size of the serial port buffer?RandomWalk::RandomWalk(RandomWalk::DataHandler *handler, QString port, QString Config, QString model, void *param) : stopThread(false), command(false), handler(handler), param(param), recievedData{0} { serial = new QSerialPort(this); connect(serial, &QSerialPort::readyRead, this, &RandomWalk::readData); openSerialPort(port, QIODevice::ReadWrite); writeData(Config.toUtf8()); } void RandomWalk::openSerialPort(QString port, QIODevice::OpenModeFlag device) { serial->setPortName(port); serial->setBaudRate(115200); serial->setDataBits(QSerialPort::Data8); serial->setParity(QSerialPort::NoParity); serial->setStopBits(QSerialPort::OneStop); serial->setFlowControl(QSerialPort::NoFlowControl); serial->setReadBufferSize(10240); // serial->waitForReadyRead(2000); if (serial->open(device)) { qDebug() << "Connected"; } else { qDebug() << "Open error"; } } void RandomWalk::writeData(const QByteArray &data) { serial->write(data); } void RandomWalk::readData() { while (serial->bytesAvailable()) { recievedData.append(serial->readAll()); } qDebug() << "recieveddata" << recievedData.size(); }
-
I open the serial port in ReadWrite mode. First I have to send a command to a device to receive the data.
The packet size sent from the device is 10240 but in serial-> readAll() I only get 4096 bytes
I also use serial-> setReadBufferSize (10240) But nothing changes. Is there a limit to the size of the serial port buffer?RandomWalk::RandomWalk(RandomWalk::DataHandler *handler, QString port, QString Config, QString model, void *param) : stopThread(false), command(false), handler(handler), param(param), recievedData{0} { serial = new QSerialPort(this); connect(serial, &QSerialPort::readyRead, this, &RandomWalk::readData); openSerialPort(port, QIODevice::ReadWrite); writeData(Config.toUtf8()); } void RandomWalk::openSerialPort(QString port, QIODevice::OpenModeFlag device) { serial->setPortName(port); serial->setBaudRate(115200); serial->setDataBits(QSerialPort::Data8); serial->setParity(QSerialPort::NoParity); serial->setStopBits(QSerialPort::OneStop); serial->setFlowControl(QSerialPort::NoFlowControl); serial->setReadBufferSize(10240); // serial->waitForReadyRead(2000); if (serial->open(device)) { qDebug() << "Connected"; } else { qDebug() << "Open error"; } } void RandomWalk::writeData(const QByteArray &data) { serial->write(data); } void RandomWalk::readData() { while (serial->bytesAvailable()) { recievedData.append(serial->readAll()); } qDebug() << "recieveddata" << recievedData.size(); }
wrote on 23 Dec 2021, 10:58 last edited by JonB@isan
You should not assume there is any relationship between how many bytesreadAll()
returns and the size of any buffer.readAll()
/bytesAvailable()
return whatever they currently "see" as there. Repeated calls will later pick up any further data. I do not think you can influence the minimum/maximum number of bytes it returns in any one call.Since you are (correctly) appending bytes received to a member buffer use that to accumulate whatever you want. If you need to wait for > 4096 bytes for some reason that is the place to do it.
In fact reading QSerialPort::setReadBufferSize() I think you might be better leaving it at the default of 0 which means
If the buffer size is limited to a certain size, QSerialPort will not buffer more than this size of data. The special case of a buffer size of 0 means that the read buffer is unlimited and all incoming data is buffered. This is the default.
So it sounds like specifying a buffer size actually places a maximum bytes limit, risking dropping of data if you do not read it fast enough.
-
Hi
Depending on the SENDING hardware, the payload will come in one or more
"waves"/readAll()So once recievedData.size() is the expected size, all data has been read.
There is no setting to avoid this. Its how it works.
1/3