[Interest] Parsing data from serialport

Denis Shienkov denis.shienkov at gmail.com
Fri Apr 5 11:45:39 CEST 2019


> Never ever ever do I/O in the main GUI thread of a Qt application.

Why not? It is an asynchronous. You can do I/O in a main thread, but handle
data in a separate thread (e.g. to parse it). Of course, it depends on the
used communication protocol and etc.
A main limitation is that on Windows all I/O stops when a user hold-on the
application window (ot to drag it, or do resizing). It is a Windows
'feature'.

So, yes, in a common case it is makes sense to move the QSerialPort object
to the different thread (a concept of workers).

> Real world serial comm is fraught with peril. I've been doing it since
the days of DOS 3.1.

It's not true.

> Unless something has dramatically changed in QSerialPort, readyRead is
only emitted for the first character in the buffer because there is no
concept of block or packet. A UART handles one byte at a time.

It's not true too. The readyRead() will be triggered when the device's
handle/descriptor becomes 'signalled/activated' (when something comes into
FIFO of driver), in this case the QSerialPort reads all data which are in
this time in the FIFO (there are may be some bytes). So, no any
byte-per-byte handling as you say.

> When you get a readyRead and fail to completely empty the buffer, that's
it. You never get another one.

I'm don't understand what do you mean...

> When QSerialPort was taken from Playground and had all its method names
changed, there was an odd timing bug too. In a production system running
multiple ports of embedded target the code loading the next byte into the
class buffer (perhaps not completely Qt code as it could have been in the
device driver). The adding of the next byte could begin after readLine()
determined how many bytes were in the buffer but prior to actually pulling
them. The new byte got added. One less than a full buffer got read and
readyRead never fired again because the next byte wasn't being written to a
pristine empty buffer.

I don't know about what you say... It is trash for my opinion.. For you has
been provided a good code example how to read the lines:

>     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
>         }
>     });

BR,
Denis




пт, 5 апр. 2019 г. в 10:38, <roland at logikalsolutions.com>:

>
> Quoting interest-request at qt-project.org:
>
> > 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).
>
> Never ever ever do I/O in the main GUI thread of a Qt application.
> It's a recipe for disaster despite the countless examples you will
> find posted on-line and even in the Qt examples themselves. Those
> database examples are really nice. Try testing them with a million+
> row table.
>
> Real world serial comm is fraught with peril. I've been doing it since
> the days of DOS 3.1.
>
> Before anyone goes running off and using the above lambda, they need
> to consider a few things.
>
> Unless something has dramatically changed in QSerialPort, readyRead is
> only emitted for the first character in the buffer because there is no
> concept of block or packet. A UART handles one byte at a time.
>
> When you get a readyRead and fail to completely empty the buffer,
> that's it. You never get another one.
>
> When QSerialPort was taken from Playground and had all its method
> names changed, there was an odd timing bug too. In a production system
> running multiple ports of embedded target the code loading the next
> byte into the class buffer (perhaps not completely Qt code as it could
> have been in the device driver). The adding of the next byte could
> begin after readLine() determined how many bytes were in the buffer
> but prior to actually pulling them. The new byte got added. One less
> than a full buffer got read and readyRead never fired again because
> the next byte wasn't being written to a pristine empty buffer.
>
>
> --
> Roland Hughes, President
> Logikal Solutions
> (630) 205-1593
>
> http://www.theminimumyouneedtoknow.com
> http://www.infiniteexposure.net
> http://www.johnsmith-book.com
> http://www.logikalblog.com
> http://www.interestingauthors.com/blog
> http://lesedi.us
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190405/1628afb9/attachment.html>


More information about the Interest mailing list