Momergil
15th December 2011, 22:30
Hello!
I'm almost having a headache because of a problem related to reading from a serial device (through the library QAbstractSerial).
I'm using a software that, through a QAbstracSerial port, communicates with a microprocessor ColdFire by sending commands to it (simple chars) and reading and interpreting its answers.
I was doing a series of advances in my software and everything was running fine. But I made some improvements in the software so it would have more commands in it (i.e. nothing that would change its logic of sending and receiving + interpreting the data, only adding some more QPushButtons and more if/else in the readyRead() function for it to understand the ColdFire answers) and since than the software simply began to fail in reading the data received by the serial port. I commented all new changes and even run the old versions of it, but the same problem persistet. I decided to use one of those freewares avaliable in the internet to "talk" to serial ports and it demonstrates that it wasn't a problem of the ColdFire programming or in the serial cables (I even used a very nice oscilloscope that can read and "translate" the data that passes through a serial port, so I could verify that the data being received by the computer was OK). But despite all changes, the new problem simply didn't dissapeared, and I'm beginning to worry if the problem is with the QAbstractSerial library.
To be more precise, the problem is that the readyRead() function is not "capturing" all data. Sometimes this means an entire line, but some times i means the lost of the first byte in all lines received. Its very important to notice that I didn't change any part of the QAbstractSerial code, neither I changed something special in the computer or else something algorithmically relevant in the software (the go-backs to previous versions with the persistent error appearing dispatches this possibility). So at the end I lost 2h of my daying trying lots of things and calling experts in computer programming and nothing worked. Could somebody give a tip of what maybe is happening?
Here is the important parts of my readyRead() function:
QByteArray bytesReceived;
Sem.acquire(1);
while (port1->bytesAvailable() > 0)
bytesReceived.append(port1->readLine()); //I don't know why, but QAbstractSerial's readlAll() simply doesn't work, so I had to do this...
Sem.release(1);
if (!bytesReceived.isEmpty())
{
emit sendMessage(bytesReceived,Qt::blue); //This sends what was received to a QPlainTextEditor in the QMainWindow
if (testesendorealizado == true)
{
qDebug() << "Dados/Struct recebida: Tamanho: " << bytesReceived.size() << "Dados: " <<
bytesReceived;
if (ultimodadofoiincompleto == false)
{
if (bytesReceived[0] == BT_RQ_SEARCH || bytesReceived[0] == '&') //This part tries to see the first byte in what was received, and always the first //byte is simply lost!
{
...
...
}
else qDebug() << "Not a known structure";
}
}
}
One more very interesting thing: apart from the times when an entire line is lost by the readyRead(), the lost of the first byte allways happens in the second relevant reading of the serial interface - what makes this problem even more strange.
Could somebody help, please?
Thanks!
Momergil
I'm almost having a headache because of a problem related to reading from a serial device (through the library QAbstractSerial).
I'm using a software that, through a QAbstracSerial port, communicates with a microprocessor ColdFire by sending commands to it (simple chars) and reading and interpreting its answers.
I was doing a series of advances in my software and everything was running fine. But I made some improvements in the software so it would have more commands in it (i.e. nothing that would change its logic of sending and receiving + interpreting the data, only adding some more QPushButtons and more if/else in the readyRead() function for it to understand the ColdFire answers) and since than the software simply began to fail in reading the data received by the serial port. I commented all new changes and even run the old versions of it, but the same problem persistet. I decided to use one of those freewares avaliable in the internet to "talk" to serial ports and it demonstrates that it wasn't a problem of the ColdFire programming or in the serial cables (I even used a very nice oscilloscope that can read and "translate" the data that passes through a serial port, so I could verify that the data being received by the computer was OK). But despite all changes, the new problem simply didn't dissapeared, and I'm beginning to worry if the problem is with the QAbstractSerial library.
To be more precise, the problem is that the readyRead() function is not "capturing" all data. Sometimes this means an entire line, but some times i means the lost of the first byte in all lines received. Its very important to notice that I didn't change any part of the QAbstractSerial code, neither I changed something special in the computer or else something algorithmically relevant in the software (the go-backs to previous versions with the persistent error appearing dispatches this possibility). So at the end I lost 2h of my daying trying lots of things and calling experts in computer programming and nothing worked. Could somebody give a tip of what maybe is happening?
Here is the important parts of my readyRead() function:
QByteArray bytesReceived;
Sem.acquire(1);
while (port1->bytesAvailable() > 0)
bytesReceived.append(port1->readLine()); //I don't know why, but QAbstractSerial's readlAll() simply doesn't work, so I had to do this...
Sem.release(1);
if (!bytesReceived.isEmpty())
{
emit sendMessage(bytesReceived,Qt::blue); //This sends what was received to a QPlainTextEditor in the QMainWindow
if (testesendorealizado == true)
{
qDebug() << "Dados/Struct recebida: Tamanho: " << bytesReceived.size() << "Dados: " <<
bytesReceived;
if (ultimodadofoiincompleto == false)
{
if (bytesReceived[0] == BT_RQ_SEARCH || bytesReceived[0] == '&') //This part tries to see the first byte in what was received, and always the first //byte is simply lost!
{
...
...
}
else qDebug() << "Not a known structure";
}
}
}
One more very interesting thing: apart from the times when an entire line is lost by the readyRead(), the lost of the first byte allways happens in the second relevant reading of the serial interface - what makes this problem even more strange.
Could somebody help, please?
Thanks!
Momergil