It's my first question on this website !
I have some trouble reading datas from a COM port, I send a complete message from another COM port, and when I receive it with Qt, it's always cut in multiple submessages.
void SerialPortReader::init()
{
connect(m_serialPort, SIGNAL(readyRead()), this, SLOT(readData()));
}
void SerialPortReader::readData()
{
// m_serialPort->waitForReadyRead(200);
QByteArray byteArray = m_serialPort->readAll();
qDebug() << byteArray;
if(byteArray.startsWith(SOF) && byteArray.endsWith(EOF_LS)
&& byteArray.size() >= MIN_SIZE_DATA) {
decodeData(byteArray.constData());
} else {
qDebug() << "LIB SWCom : Unvalid trame !";
}
}
the messages sent are 25 or 27 Bytes long, if I use Putty or an Hyperterminal to read them, I have no trouble. Also if I use 2 emulated serial port COM to communicate, I don't have this problem... It only occurs with Qt reading system AND with 2 physical COM port...
I think I don't get when the readyRead signal is emitted exactly...
I'm very confuse, Thank you in advance for your help !
Use Thread concept here , using threads you can achieve this... run your portread in a seperate thread then it will be works fine, other wise the message will be cut. other method u can use Qprocess and open a new terminal using QProcess and send the port number and connectivity details to that terminal and run it from Qt then also u can archive this
i mean we can use therad and socket communication here look at this code
this is a simple example of telnet connection, likewise u can specify which port you want connect and connected to which ip
The readyRead-signal is emitted whenever there is pending data AND the previous execution of readyRead has finished.
Documentation says:
If this should lead to any problems in your case, you could use a while-loop where you check for bytesAvailable().
The documentation is actually quite clear about it:
This means that it is not really guaranteed how much data will be available for reading, just that some will be available.
If you wish to read more data than it is coming in one go, you may be opt for a timeout value and/or readyRead. It depends on what you are trying to achieve.
See the command line async reader example that I wrote a while ago for this operation, too:
In this case, the command line reader example will get any data that was passed in one shot, but it does not guarantee the length or anything.
Also, please note that the sync api that is behind your comments does not make much sense along with the async API that you are asking about. I am referring to
m_serialPort->waitForReadyRead(200);
in here.