[Solved] QSerialPort read with delay (slowly updates)



  • 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;
    }@


  • Moderators

    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.



  • @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?


  • Moderators

    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.



  • @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.



  • [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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.