Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] QSerialPort read with delay (slowly updates)
Forum Updated to NodeBB v4.3 + New Features

[Solved] QSerialPort read with delay (slowly updates)

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    matijalazic
    wrote on last edited by
    #1

    Hello
    I have problem with reading from serial port. My device sends data (4 letter string + 4 letter string + 4 letter string) every 0.1 second. Somehow QT cannot keep track of that and data in qt aplication updates with 2 second or more delay. I really need to fix this without changing update time from device. Here is the code and I appreciate all the help. If you need code of the whole program for the solution please respond in comments.

    @void MainWindow::readData()
    {
    serialReadTekst = serial->readLine();
    if(serialReadTekst[0] == 'V'){
    volt = serialReadTekst[1].digitValue();
    volt = volt10 + serialReadTekst[2].digitValue();
    volt = volt
    10 + serialReadTekst[3].digitValue();
    str = QString::number(volt * (5.0 / 1023.0));
    ui->label->setText(serialReadTekst + " " + str);
    ui->lcdNumber->display(volt * (5.0 / 1023.0));
    }
    qDebug() << volt;
    }@

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You shoud not rely on timing in transmissions like this. The ports can have buffers, the libraries can have additional buffers, the cables can be hit by external interference, etc. Also, the device may choose not to send the data exactly when you request it to, or in a whole chunk. You can try forcing such a behaviour by flushing the socket after every write operation.

      The usual approach is to add some packet delimiter and read the source searching for those delimiters, and then glueing the results together.

      (Z(:^

      1 Reply Last reply
      0
      • M Offline
        M Offline
        matijalazic
        wrote on last edited by
        #3

        @sierdzio Problem is not in the device or cables. I am 100% sure about that since its arduino controller and i check data with its Serial Communication aplication and it communicates instantly.

        About your solution is there any chance you could show me some code which could fix my problem?

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Flushing is easy. Just call this:
          @
          port->(write)
          port->flush();
          @

          For delimiter: I don't have any example at my hand right now, sorry.

          Another thing you could try is to poll the port yourself in a tight loop (preferably in a separate thread, so that the application is not blocked) to get the updates as soon as they are available.

          (Z(:^

          1 Reply Last reply
          0
          • M Offline
            M Offline
            matijalazic
            wrote on last edited by
            #5

            @sierzio Sadly serial->flush(); did not help.

            About separate thread. I already tried but somehow did not work.
            The application itself is not blocked. It's just that data reads with delay.

            If you could give me example of code which would run in another thread I would be very happy to try. Thanks for all the help so far.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              matijalazic
              wrote on last edited by
              #6

              [quote author="sierdzio" date="1423221271"]Flushing is easy. Just call this:
              @
              port->(write)
              port->flush();
              @

              For delimiter: I don't have any example at my hand right now, sorry.

              Another thing you could try is to poll the port yourself in a tight loop (preferably in a separate thread, so that the application is not blocked) to get the updates as soon as they are available.[/quote]

              Thanks for your help man. I did find the solution. I was reading line by line which messed with results I guess. Now that i use readAll() it works flawlessly.

              Thank you again for your time.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved