I am facing a weird behavior of readyRead(). I use QextSerialPort for my serial communication and connect readyRead() with my reading slot. For example, I am writing 45 bytes to port and expecting to read 45 bytes but what happens, my readyRead() signal is emitted more than once, meaning it will read 38 bytes first then read 7 bytes. How can I make it read the whole chunk of data?
//Port setup
m_pSerialPort = new QextSerialPort(pduPortName, m_PortSettings, QextSerialPort::EventDriven);
m_pSerialPort->setBaudRate(m_PortSettings.BaudRate);
m_pSerialPort->setDataBits(m_PortSettings.DataBits);
m_pSerialPort->setFlowControl(m_PortSettings.FlowControl);
m_pSerialPort->setParity(m_PortSettings.Parity);
m_pSerialPort->setStopBits(m_PortSettings.StopBits);
m_pSerialPort->setTimeout(500);
m_pSerialPort->flush();
m_pSerialPort->open(QIODevice::ReadWrite | QIODevice::Unbuffered );
connect(m_pSerialPort, SIGNAL(readyRead()), this, SLOT(readSerialData()));
//write
qint64 bytesWritten = pSerialPort->write(data, bytes_to_write);
//Read
qint64 availableBytes = pSerialPort->bytesAvailable();
if (availableBytes >= SIZE)
bytesRead = pSerialPort->read(data , availableBytes);
I hope I explained my problem well. Please help!
Note: am using STANAG protocol for my messages.
Some communications hardware only have very small on-chip buffers. As a result, you very often only get data back in small bursts.
A lot of GPS transceivers operate in this way, with many of them only producing about half a k (512 bytes) of data at a time.
It's highly likely that the serial chip in your device is hard-wired to only return that many bytes at a time, and as a result you'll need to keep reading the port until bytes Available call returns 0 bytes to read.
I can't remember the code / os-calls off the top of my head, but you might want to have a look at your device SDK and see if it mentions anything about increasing / decreasing buffer sizes on the device, if it does then increasing that may increase the amount of data returned in one hit.
It's not unusual however to have a large buffer in your app and have to fill it incrementally with repeated calls to the device to gather data.
As you've found that the "sleep" command is also making things work, then it may not be a buffer size issue however, it might just be that the device is much slower at filling it's data buffer than your app / pc is at reading it.