0

I am having problems communicating FROM the arduino to my Qt application through QSerialPort. I have a listening signal that tells me when there is data ready to be read from the arduino. I expect a value for the number of steps that a stepper motor has undertaken before hitting a limit switch, so only a simple int such as "2005". When the data is available for reading, sometimes I get two separate reads with "200" and "5". Obviously this messes things up when I am parsing the data because it records it as two numbers, both much smaller than the intended number.

How can I fix this without me putting in a Sleep or QTimer to allow for a bit more time for the data to come in from the arduino? Note: my program is not multithreaded.

Example Qt code:

    //Get the data from serial, and let MainWindow know it's ready to be collected.
    QByteArray direct = arduino->readAll();
    data = QString(direct);
    emit dataReady();

    return 0;

Arduino:

    int count = 2005;
    Serial.print(count);
Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127

2 Answers2

0

You can add line break to synchronize.

Example Qt code:

    //Get the data from serial, and let MainWindow know it's ready to be collected.
    QByteArray direct = arduino->readLine();
    data = QString(direct);
    emit dataReady();

    return 0;

Arduino:

    int count = 2005;
    Serial.print(count);
    Serial.println();

If you are going to use QSerialPort::readyRead signal, you need to also use the QSerialPort::canReadLine function, see this.

Community
  • 1
  • 1
Arpegius
  • 5,817
  • 38
  • 53
0

Thank you for your help Arpegius. The println() function was definitely a good choice to use for the newline delimiter. And following that link, I was able to get a listening function that got everything the arduino sent as seperate strings. The extra if statements in the loop handle any cases where the incoming string does not contain the newline character (I am paranoid :D)

My code for anyone that has the same problem in the future.

int control::read()
{
    QString characters;
    //Get the data from serial, and let MainWindow know it's ready to be collected.
    while(arduino->canReadLine())
    {
        //String for data to go.
        bool parsedCorrectly = 0;
        //characters = "";

        //Loop until we find the newline delimiter.
        do
        {
          //Get the line.
          QByteArray direct = arduino->readLine();//Line();

          //If we have found a new line character in any line, complete the parse.
          if(QString(direct).contains('\n'))
          {
              if(QString(direct) != "\n")
              {
                  characters += QString(direct);
                  characters.remove(QRegExp("[\\n\\t\\r]"));
                  parsedCorrectly = 1;
              }   
          }
          //If we don't find the newline straight away, add the string we got to the characters QString and keep going.
          else
              characters += QString(direct);
        }while(!parsedCorrectly);

        //Save characters to data and emit signal to collect it.
        data = characters;

        emit dataReady();

        //Reset characters!
        characters = "";
    }

    return 0;
}