0

I am receiving data in this shape:

Q1\n
9.70E-6\n
OK>

from an external device via QSerialPort, but with my reading routine

QString request = "Hello";
qDebug() << "TransAction started!";
QByteArray requestData = request.toLocal8Bit();
qDebug() << "Writing data: " << requestData;
serial->write(requestData);
qDebug() << "Data written";
if(serial->waitForBytesWritten(waitTimeout))
{
     if(serial->waitForReadyRead(waitTimeout))
                {
                    qDebug() << "Waiting for data!";
                    QByteArray responseData = serial->readAll();
                    while(serial->waitForReadyRead(100))
                        responseData += serial->readAll();
                    responseData.replace('\n', ' ');
                    QString response(responseData);
                    QByteArray response_arr = response.toLocal8Bit();
                    qDebug() << "Response is: " << response_arr.toHex();
                    emit this->response(response);
                }
                else
                {
                    qDebug() << "Wait read response timeout";
                    emit this->timeout(tr("Wait read response timeout %1").arg(QTime::currentTime().toString()));
                }
            }
            else
            {
                qDebug() << "Wait write request timeout!";
                emit this->timeout(tr("Wait write request timeout %1").arg(QTime::currentTime().toString()));
            }

I only get

Q1

as response. How can I modify my code such that I am able to read all input data?
Update:
When testing it with the serial port script described here: https://stackoverflow.com/a/7654527/2546099, everything works. Apparently the problem is that the qt-version stops reading after the first line break. This problem consists also if I add

char buffer[1000];
                    for(int i = 0; i < 1000; i++)
                    {
                        int tmp = serial->read(buffer, 1000);
                        if(tmp > 0)
                            qDebug() << buffer;
                    }

directly after the line

qDebug() << "Waiting for data!";

Then I still only get the first line (without the \n). Changing times does not change the received data.

Community
  • 1
  • 1
arc_lupus
  • 3,942
  • 5
  • 45
  • 81
  • That's pretty normal, serial ports are slow so you often only get a few bytes. Many devices transmit an end-of-line indicator, often "\n". Which lets you use readLine() instead of readAll(). – Hans Passant Oct 25 '15 at 17:42
  • @HansPassant: Then the problem here is that I get two `\n` in one transmission (behind the `Q1` and the numeric value). In order to get all data I assume that I have to call readLine() three times. Is that correct? – arc_lupus Oct 25 '15 at 18:18
  • have you tried using any other serial port readers like `realterm`, just to make sure you are receiving all the data you are expecting in one go? This happened to me and it look a while before I realised the issue. – ramtheconqueror Oct 26 '15 at 09:56
  • @ramtheconqueror: When using `cutecom` I get all the data I want (i.e. the received data shown in the second line in the question). – arc_lupus Oct 26 '15 at 10:47
  • Does anything change if you make the 100ms wait in the loop much higher, like 1 second? Maybe the other device is very slow and is waiting more than 100ms between lines? – ScottG Oct 26 '15 at 13:24
  • No, it does not change anything, I still just read the first line... – arc_lupus Oct 27 '15 at 10:51

1 Answers1

1

The answer to my problem is (partly) described in this question: External vs internal declaration of QByteArray. My problem (why I did not receive any further data) was that I did not send a \x00D after the input line, thus the device just echoed my input, and was waiting for the Enter afterwards. After the input looks exactly as the first line, I misunderstood it for just getting the first line, and nothing else.

Community
  • 1
  • 1
arc_lupus
  • 3,942
  • 5
  • 45
  • 81