[Interest] Parsing data from serialport

Jérôme Godbout godboutj at amotus.ca
Wed Apr 3 15:12:10 CEST 2019


Because you cannot unread data from the device and you have no guarantee that all the data will be present when you do read, you need a buffer since you need a whole line to process the data and data might be present but the line might not be fully arrived yet, maybe just half of it. The IODevice (Serial) buffer is buffering the reception of the data until you read it, your buffer is checking data is competed to be parsed, they have a different purpose.


[36E56279]
une compagnie  [cid:image002.jpg at 01D4E9FD.50AA6910]
RAPPROCHEZ LA DISTANCE

Jérôme Godbout
Développeur Logiciel Sénior /
Senior Software Developer

p: +1 (418) 800-1073 ext.:109

amotus.ca<http://www.amotus-solutions.com/>
statum-iot.com<http://statum-iot.com/>
[cid:image003.png at 01D4E9FD.50AA6910]<https://www.facebook.com/LesSolutionsAmotus/> [cid:image004.png at 01D4E9FD.50AA6910] <https://www.linkedin.com/company/amotus-solutions/>  [cid:image005.png at 01D4E9FD.50AA6910] <https://twitter.com/AmotusSolutions>  [cid:image006.jpg at 01D4E9FD.50AA6910] <https://www.youtube.com/channel/UCoYpQgsmj1iJZyDjTQ3x8Ig>





From: Martin Marmsoler <martin.marmsoler at gmail.com>
Sent: April 3, 2019 3:49 AM
To: Jérôme Godbout <godboutj at amotus.ca>
Cc: Thiago Macieira <thiago.macieira at intel.com>; interest at qt-project.org
Subject: Re: [Interest] Parsing data from serialport

But why I need a new buffer when there already one exist? (the QIODevice and my own buffer or?)

My idea:

Main() {
Connect(readyread, myreadyread)
}

myreadyread() {

If (!canreadline())
   Return;

Parsedata() ;

}

With this idea I have again the problem with the while loop, but I dont need two buffers.
Is it an alternative?

Martin

Jérôme Godbout <godboutj at amotus.ca<mailto:godboutj at amotus.ca>> schrieb am Mi., 3. Apr. 2019, 00:01:
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-bounces at qt-project.org<mailto:interest-bounces at qt-project.org>> On Behalf Of Martin Marmsoler
Sent: April 2, 2019 3:58 PM
To: Thiago Macieira <thiago.macieira at intel.com<mailto:thiago.macieira at intel.com>>
Cc: interest at qt-project.org<mailto:interest at 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.macieira at intel.com<mailto:thiago.macieira at 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 at qt-project.org<mailto: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/20190403/f30e1f42/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.jpg
Type: image/jpeg
Size: 6751 bytes
Desc: image001.jpg
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190403/f30e1f42/attachment.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.jpg
Type: image/jpeg
Size: 1016 bytes
Desc: image002.jpg
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190403/f30e1f42/attachment-0001.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image003.png
Type: image/png
Size: 483 bytes
Desc: image003.png
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190403/f30e1f42/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image004.png
Type: image/png
Size: 506 bytes
Desc: image004.png
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190403/f30e1f42/attachment-0001.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image005.png
Type: image/png
Size: 500 bytes
Desc: image005.png
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190403/f30e1f42/attachment-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image006.jpg
Type: image/jpeg
Size: 713 bytes
Desc: image006.jpg
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190403/f30e1f42/attachment-0002.jpg>


More information about the Interest mailing list