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-boun...@qt-project.org> On Behalf Of Paolo Angelelli
Sent: April 4, 2019 10:19 AM
To: interest@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.


On Tue, 2 Apr 2019 22:01:49 +0000
Jérôme Godbout <godbo...@amotus.ca> wrote:

> Make sure your reading loop and processing data are separated. Call you read 
> device when needed or into a loop that can take some pause to avoid 100% CPU 
> usage for nothing.
> 
> QByteArray buffer;
> 
> void ReadDeviceHaveData()
> {
> 
>    while(serial_port->bytesAvailable()) // This can be dangerous is 
> data keep coming  and might be removed {
>             // You can read bytes per bytes or smaller chunk over here for 
> better reactivity and less memory consumption
>             buffer.append(serial_port->readAll());
>             processData();
>             serial_port->waitForReadyRead(5);
>      }
> }
> 
> void ProcessData()
> {
>    int pos = buffer.indexOf(‘\n’);
> 
>    while(pos >= 0)
>    {
>      QByteArray line = buffer.left(pos);
>      // Strip trailing \r for windows here
>      // Do whatever you need with your line, check data integrity
> 
>      // Remove the processed data but leave the unprocessed data alone
>      buffer.remove(0, pos + 1); // Remove \n too
>      pos = buffer.indexOf(‘\n’);
>    }
> }
> 
> From: Interest <interest-boun...@qt-project.org> On Behalf Of Martin 
> Marmsoler
> Sent: April 2, 2019 3:58 PM
> To: Thiago Macieira <thiago.macie...@intel.com>
> Cc: interest@qt-project.org
> Subject: Re: [Interest] Parsing data from serialport
> 
>  > To be able to roll back, in case your reading from the device 
> didn't result in what you wanted or you got an error. See QDataStream.
> Ah ok I understand.
> 
> So this minimal example
> QSerialPort sPort;
> sPort.open(QIODevice::ReadOnly);
> 
> if(sPort.waitForReadyRead(2000)){
> 
>   while (!device.atEnd()) {
> 
>         if (device.canReadLine()) {
> 
>                newData.push_back(device.readLine());
> 
>                linesToRead++;
> 
>         } else {
> 
>                return;
> 
>         }
> 
>                }
> 
>    ...
> 
> }
> 
> works fine, if I go trough it step by step (maybe, because enouth data come 
> in). But if I'm to fast it does not work.
> 
> If I'm using the signal readyRead I will have the same problem, 
> because new data come everytime. So I check that in the
> 
> readyRead function if a complete line come in, and if no complete line 
> I return without doing something otherwise I do
> 
> something with the data? Is this the right way?
> 
> 
> 
> Martin
> 
> 
> 
> 
> Thiago Macieira <thiago.macie...@intel.com<mailto:thiago.macie...@intel.com>> 
> schrieb am Di., 2. Apr. 2019, 18:02:
> On Tuesday, 2 April 2019 07:04:03 PDT Martin Marmsoler wrote:
> > Thank you Thiago for your response. But what is transactionstart for?  
> 
> To be able to roll back, in case your reading from the device didn't 
> result in what you wanted or you got an error. See QDataStream.
> 
> --
> Thiago Macieira - thiago.macieira (AT) intel.com<http://intel.com>
>   Software Architect - Intel System Software Products
> 
> 
> 
> _______________________________________________
> Interest mailing list
> Interest@qt-project.org<mailto:Interest@qt-project.org>
> https://lists.qt-project.org/listinfo/interest

_______________________________________________
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest
_______________________________________________
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest

Reply via email to