[Interest] Parsing data from serialport

Konstantin Shegunov kshegunov at gmail.com
Thu Apr 4 17:19:08 CEST 2019

On Thu, Apr 4, 2019 at 5:46 PM Jérôme Godbout <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> On Behalf Of Paolo
> Angelelli
> Sent: April 4, 2019 10:19 AM
> To: 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/b83ef52b/attachment.html>

More information about the Interest mailing list