[Interest] Parsing data from serialport

Jérôme Godbout godboutj at amotus.ca
Thu Apr 4 17:42:56 CEST 2019


You jus did the same loop into your slot, the function I did made can be called as slot to the connect you just told, I agree the serial_port->waitForReadyRead(5); is not necessary, just often the transmission have partial or bunch of data by small burst (USB VCOM) and was handy for me, but it is not ncessary in any way.



Perfoming heavy IO on main thread is often a bad idea and lead bad GUI reactivity, that’s all, but nothing prevent doing the call into it.



Side note: ReadLines made it less general purpose, you will need a protocol that use endline to signal end of message which is not always the case.

From: Konstantin Shegunov <kshegunov at gmail.com>
Sent: April 4, 2019 11:19 AM
To: Jérôme Godbout <godboutj at amotus.ca>
Cc: Paolo Angelelli <paolo.angelelli at qt.io>; interest at qt-project.org
Subject: Re: [Interest] Parsing data from serialport

On Thu, Apr 4, 2019 at 5:46 PM Jérôme Godbout <godboutj at amotus.ca<mailto:godboutj at amotus.ca>> wrote:
It relief the main thread from read data all the time and keep a good reactivity. The dedicated thread try to read and can wait until something come along and once it found something that can be parsed, it emit his own signal that the main thread only have to handle into normal slot (will be queued, since it's not the same thread).

But yeah, you can have the main thread do it and process the readyRead(), you main thread will perform the read and parsing. if you have any CRC and other things, this might be bad for application reactivity, depending on the amount of data flowing.

-----Original Message-----
From: Interest <interest-bounces at qt-project.org<mailto:interest-bounces at qt-project.org>> On Behalf Of Paolo Angelelli
Sent: April 4, 2019 10:19 AM
To: interest at qt-project.org<mailto:interest at qt-project.org>
Subject: Re: [Interest] Parsing data from serialport

What is the advantage of having such a continuous reading loop in a separate thread?

I mean it as opposed to reacting to the readyRead() signal, and having a while(canReadLine()) { ... } in the reacting slot.

I think the point is that there's little reason to poll the serial port if you can react to the event. Exactly what you'd do if you had a network socket. Qt already does the heavy lifting for you, so you only need to react to the signal and read as much as you want/need. Basically:

    QSerialPort * port;
    QObject::connect(port, &QIODevice::readyRead, port, [port] () -> void  {
        while (port->canReadLine())  {
            QByteArray data = port->readLine();
            // emit with data as argument and do the parsing
        }
    });
Whether you have the port in another thread or not is irrelevant in this case, either can work fine (unlike your while-sleep loop).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190404/fcf351e7/attachment.html>


More information about the Interest mailing list