0

I am currently working on a project that involves serial communication between a Arduino and a laptop. I know the Arduino is indeed sending the data that I need, see this picture: http://s1.postimg.org/w5wisaetr/Help.png

Now on the other end my laptop is connected to the Arduino and running a program that I made using QT Creator. However, when reading data from the serial Port I can't get the program to display this information.

I connected my readData() function to be executed when data is received like this:

connect(m_serialPort, SIGNAL(readyRead()), m_dataGathering, SLOT(newData()));

This works and the newData() function is called whenever something in transmitted from the Arduino. However the function newData() does not display the data that I need.

newData():

void DataGathering::newData()
{
    QByteArray rMsg = m_serial->readAll();
    qDebug() << rMsg.constData();
}

This only sends empty message to the display. Like this: http://s2.postimg.org/dkcyip2u1/empty.png

The following code however works:

void DataGathering::newData()
{
    QByteArray rMsg("\nTest...");// = m_serial->readAll();
    qDebug() << rMsg.constData();
}

This code display the message like it should. However, another difference in the output display is that when the working code is executed my console also displays a lot of Framing errors, I assumed this is because the baudrate of the unwanted characters differs from the data that I need.

That is why i started questioning the readAll() function. It is also obvious that the Arduino is not only sending the data that I need but also some unwanted characters (see image in first link), but I don't see this as a problem since I will filter this out later.

All help is very much appreciated.

Update: I found out that the readAll() function is returning QByteArrays with size() equals to 0.

dsolimano
  • 8,870
  • 3
  • 48
  • 63

1 Answers1

0

Looks like the serial port QIODevice does not implement bytesAvailable, if it returns 0. This may also be why readAll() fails, depending on how it is implemented. But at least readAll() has the problem of not being able to report error.

Try using read method instead for better diagnostics, like this (untested):

void DataGathering::newData()
{
    QByteArray rMsg;
    for(;;) {
        char buf[256]; // read data in this size chunks
        qint64 len = m_serial->read(buf, sizeof buf);
        if (len <= 0) {
            if (len < 0) {
                qDebug() << "newData() read error" << m_serial->errorString();
            }
            break; // for(;;)
        }
        rMsg.append(buf, len);
    }
    qDebug() << "newData() got byte array" << rMsg.size() << ":" << rMsg;
}

It may not solve your problem, but with luck it will give you error message.

hyde
  • 60,639
  • 21
  • 115
  • 176