QSerialPort passing received data to GUI [solved]

  • Hi guys, I'd to ask you what will be the fastest way for passing received data (QByteArray) after ReadyRead() signal to GUI (I mean for example QEditBox on mainwindow). The main thread should be loaded as low as possible in my app. Thanks for suggestions.

  • Moderators

    Erm... just connect the signal to a slot in your GUI class and set the QEditBox from there?
    void YourClass::on_readyRead(const QByteArray &array) {

  • Ok, yes I know :-). Thanks for replay. The problem is when I am getting data packet every 10 ms it leads to quite big load on main thread. I suggested for example extra thread for buffering data and actuate GUI two times per second or something similar.

  • Moderators

    That's much clearer now. You can drop the ReadyRead() altogether, then. Just set up a QTimer that will fire every, say, 500 ms and in a slot handling the triggered() signal do this:

    I did that recently with QSerialPort, works marvellously even on a small embedded board.

  • Lifetime Qt Champion


    You might also want to take a look at the Model/View to have more control on what data is shown, what has to be drawn etc...

  • Hi,
    current baudrate is 9600 bps, I receive upto 8 B every 10 ms. Baudrate has to be so slow because it requires the target device (low power embedded board with MSP430). One part of qt application should frequently (100 Hz) handle received data and the second part should show data to the user in GUI (here could be refresh rate about 2 Hz as I wrote above). At this moment this proccess (just showing data in GUI) spent 10% of CPU power (AMD Phenom II X4 965, 3,4GHz).
    By target device I mean second side of serial line :-).

  • Moderators

    Ok, then what about firing the QTimer in the slot that handles the "frequent" data (on_readyRead()), so that the GUI gets a call less often?

  • Lifetime Qt Champion

    One other thing: is the data to be appended ? Or does it just replace the current values ?

  • To SGaist: The data are prepended in QString (newest data on the first line in multiline string).
    To sierdzio: I think so, yes it could be the easiest way. I will try it

    I have one another question related to QSerialPort. Does it possible to handle any QSerialPort exception (framing error, buffer overflow.. etc) ? I am asking becouse sometimes when I debugging app the exception is invoked and I think that is caused by serial port.

  • 2 jarosda,

    You can try do connect to SerialPortError signal to catch some errors.

    But detecting for FramingError, ParityError or BreakConditionError still is not fully implemented. Because it is very difficult (and sometimes impossible) to make a same behavior for different operating systems to detect these errors.

    So, at least catching for this errors should be work on Windows, although it is not a fact, please try it and let me know. :)

    In general, the overall approach to catch such errors is not yet implemented in the design. Developers can not agree among themselves. Also it is so hard implement on *nix platforms...

  • Moderators

    You can also try to use the serial device in the UI thread directly. The API is asynchronous anyway and communicating between threads requires locking, etc. This overhead can severely slow down your application.

  • Lifetime Qt Champion

    Then, you might be interested in using a "QListView":http://qt-project.org/doc/qt-4.8/qlistview.html with a "QStringListModel":http://qt-project.org/doc/qt-4.8/qstringlistmodel.html

  • Guys thank you for posts, the problem wasn't in qserialdevice at all. The cpu looad was caused by offten accessing qtextedit and related operations with qstring. Thanks again, David.

  • Lifetime Qt Champion

    Good to see it works !

    If you're problem is solved, can you update the thread title so other people may know that you found a solution ?

  • Ok, I'll do it. I didn't know how to close this thread.

Log in to reply

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