Results 1 to 20 of 30

Thread: QSerialPort High Speed Serial Reading from board and Logging Issue

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSerialPort High Speed Serial Reading from board and Logging Issue

    You did not use methods such as I pointed out - use exactly like mine. The point is you do not do any processing of the received data.

  2. #2
    Join Date
    Nov 2016
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSerialPort High Speed Serial Reading from board and Logging Issue

    Okay but now i see this.

    https://postimg.org/image/tocst0rox/

    I cant understand from anything here. Every time package is coming in different sizes. Maybe i couldn't understand you fully sorry.

    Thanks.

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSerialPort High Speed Serial Reading from board and Logging Issue

    Quote Originally Posted by franbogaz View Post
    Okay but now i see this.

    https://postimg.org/image/tocst0rox/
    Yes, and now you have to analyze whether the data are complete or not.

    Quote Originally Posted by franbogaz View Post
    I cant understand from anything here. Every time package is coming in different sizes. Maybe i couldn't understand you fully sorry.

    Thanks.
    This is normal. Serial port knows nothing about packets. Serial port is a stream of bytes. You have to divide the stream into packets and handle them. It should look something like this
    Qt Code:
    1. void MyObject::toReadThePort()
    2. {
    3.  
    4. if(serial->isReadable())
    5. {
    6.  
    7. ArrayBuffer.append(serial->readAll());
    8. while(ArrayBuffer.size() >= ExpectedPacketLength)
    9. {
    10. QByteArray one_packet = ArrayBuffer.left(ExpectedPacketLength);
    11. // Here processing of one_packet
    12. ArrayBuffer.remove(0,ExpectedPacketLength);//delete processed packet
    13. }
    14. receiveData();
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 
    Of course I not tried to compile it - it is only a sketch.

  4. The following user says thank you to Lesiok for this useful post:

    franbogaz (23rd January 2017)

  5. #4
    Join Date
    Nov 2016
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSerialPort High Speed Serial Reading from board and Logging Issue

    Thanks for quick reply. I will try it and get back to you as soon as possible i can.

  6. #5
    Join Date
    Nov 2016
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSerialPort High Speed Serial Reading from board and Logging Issue

    Quote Originally Posted by Lesiok View Post
    Yes, and now you have to analyze whether the data are complete or not.

    This is normal. Serial port knows nothing about packets. Serial port is a stream of bytes. You have to divide the stream into packets and handle them. It should look something like this
    Qt Code:
    1. void MyObject::toReadThePort()
    2. {
    3.  
    4. if(serial->isReadable())
    5. {
    6.  
    7. ArrayBuffer.append(serial->readAll());
    8. while(ArrayBuffer.size() >= ExpectedPacketLength)
    9. {
    10. QByteArray one_packet = ArrayBuffer.left(ExpectedPacketLength);
    11. // Here processing of one_packet
    12. ArrayBuffer.remove(0,ExpectedPacketLength);//delete processed packet
    13. }
    14. receiveData();
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 
    Of course I not tried to compile it - it is only a sketch.
    I did what you said. And it became more stable. Actually it is working at lower frequency. But i need 2.5kHz. It's working at 500 Hz now.

    I changed my code like this :

    Qt Code:
    1. while(ArrayBuffer.size() >= ExpectedPacketLength)
    2. {
    3. indexOfType = ArrayBuffer.indexOf('s');
    4.  
    5. if(indexOfType >= 1)
    6. {
    7. ArrayBuffer.remove(0,indexOfType);
    8. }
    9.  
    10. QByteArray one_packet = ArrayBuffer.left(ExpectedPacketLength);
    11. ArrayBuffer.remove(0,ExpectedPacketLength);
    12.  
    13. if(one_packet[0] == 's') // 's' buldur kodu yaz dene!
    14. {
    15. for(dummyCounter=0;dummyCounter<ExpectedPacketLength;dummyCounter++)
    16. {
    17.  
    18. dummyArray[dummyCounter] = one_packet[dummyCounter];
    19. shortArray[dummyCounter] = one_packet[dummyCounter];
    20. }
    21.  
    22. CRCValue = crcsum(&dummyArray[0],ExpectedPacketLength,CRC_INIT); // Just Checking the CRCValue. not gonna use anywhere.
    23. ReceiveIsSuccessful = crcverify(&dummyArray[0],ExpectedPacketLength);
    24.  
    25. if(ReceiveIsSuccessful == 1) // Checking if CRC is OK or not
    26. { .. parse and logging process is here..}
    To copy to clipboard, switch view to plain text mode 

    Now here is the other problem:

    When i'm trying to get data at 2.5kHz terminal goes like this :

    Everything is okay to some point. But when any error occurs (like CRC can be wrong) QT cant keep the data instaneously at that time.
    https://postimg.org/image/ilb3tg0d5/

    So i added to my code this :

    Qt Code:
    1. if(ArrayBuffer.size() >= 20000){
    2. .. then the code is the same i wrote above..
    To copy to clipboard, switch view to plain text mode 

    Now it's getting better:

    https://postimg.org/image/iml1mv26x/

    But as we can see suddenly data are escaping from the buffer after its full. When i increment the 20000 to 100000 or much far away its getting better. However board will always send data constantly.

    How can i solve this ? I tried to clear the ArrayBuffer after a chunk of data (20000) have been processed. But it didnt fix it. Any idea about this ?

    Because i'm thinking QSerialPort cant handle the high frequency communication. Maybe internal read buffer size isn't enough ?

    Thanks for help.

  7. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSerialPort High Speed Serial Reading from board and Logging Issue

    Can You tell some about protocol (frame construction) ? As I see a frame starts with letter s.

  8. #7
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSerialPort High Speed Serial Reading from board and Logging Issue

    > Because i'm thinking QSerialPort cant handle the high frequency communication.

    It is only your wrong thinking.

    > serial->setReadBufferSize(400);

    Don't touch this at all.

    By default the internal buffer of QSP is infinite, and QSP does not lose any data.

    > serial->clear(...)

    Do not use it.

    PS: The problem is in your code.
    Last edited by kuzulis; 23rd January 2017 at 11:14.

  9. #8
    Join Date
    Nov 2016
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSerialPort High Speed Serial Reading from board and Logging Issue

    Quote Originally Posted by Lesiok View Post
    Can You tell some about protocol (frame construction) ? As I see a frame starts with letter s.
    Yes. Starting with type -> 1byte for 's', then the short value -> which is 2 byte , and the other 2 byte -> is the CRC Value of these 3 bytes('s',short value byte 1, short value byte 2). Total of 5 bytes i'm receiving.


    Quote Originally Posted by kuzulis View Post
    > Because i'm thinking QSerialPort cant handle the high frequency communication.

    It is only your wrong thinking.

    > serial->setReadBufferSize(400);

    Don't touch this at all.

    By default the internal buffer of QSP is infinite, and QSP does not lose any data.

    > serial->clear(...)

    Do not use it.

    PS: The problem is in your code.
    Hi kuzulis. I read about internal buffer and i removed that code directly. I was just limited the internal buffer without any reason. Yes that was wrong. I forgot to say that at my last post.

    I'm basically thinking that now while parsing and logging processes are going on readyRead is not emitted so i'm losing data ? I can't think any other problem. I checked and debugged my code so many times.

    Thanks for the help.

    Edit: I also removed the serial->clear(); Not using it anywhere in code.

  10. #9
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSerialPort High Speed Serial Reading from board and Logging Issue

    OK. Create an else for if in line 13
    Qt Code:
    1. if(one_packet[0] == 's') // 's' buldur kodu yaz dene!
    2. {
    3. }
    4. else
    5. {
    6. qDebug() << one_packet;
    7. }
    To copy to clipboard, switch view to plain text mode 

  11. #10
    Join Date
    Nov 2016
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSerialPort High Speed Serial Reading from board and Logging Issue

    Quote Originally Posted by Lesiok View Post
    OK. Create an else for if in line 13
    Qt Code:
    1. if(one_packet[0] == 's') // 's' buldur kodu yaz dene!
    2. {
    3. }
    4. else
    5. {
    6. qDebug() << one_packet;
    7. }
    To copy to clipboard, switch view to plain text mode 
    Code never fall into there because i always handling with that error with this code.

    Qt Code:
    1. indexOfType = ArrayBuffer.indexOf('s');
    2.  
    3. if(indexOfType >= 1)
    4. {
    5. ArrayBuffer.remove(0,indexOfType);
    6. }
    To copy to clipboard, switch view to plain text mode 

    If any errorful package comes starting without 's' it will be removed from package and my loss will be just 1 number. But i'm losing 3000-4000 numbers.

  12. #11
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSerialPort High Speed Serial Reading from board and Logging Issue

    After ArrayBuffer.remove(0,indexOfType); ArrayBuffer can be shorter than ExpectedPacketLength.
    P.S.
    If data in frame are binary then every byte in frame can be 's'.
    Last edited by Lesiok; 23rd January 2017 at 12:09.

  13. #12
    Join Date
    Nov 2016
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSerialPort High Speed Serial Reading from board and Logging Issue

    Actually it is not a problem because when ArrayBuffer.size below the expected packet length its leaving the slot and waiting for readyRead signal and i debugged it and see that:

    Last 6 bytes to be processed e.g : {value,value,'s',value,value,value}. first two value is removed. and became {'s',value,value,value}. Then readyRead is emitted by the QT and data is appending to buffer again and these 4 bytes of ArrayBuffer which inherited by the last process is appended with the other {value,'s',value, bla bla... } data. And i checked their CRC and values. Everything is ok. But sometimes this buffer gets wrong number. Lets say we counted 1 to 150 without error and our buffer stayed like i exampled. Then its not 151 it became 500 or 1000 directly.

    Really hard to explain. I'm trying my best. Thanks for trying to help.

  14. #13
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSerialPort High Speed Serial Reading from board and Logging Issue

    It is a problem because after remove You have in buffer only 4 bytes not 5. Try this code :
    Qt Code:
    1. if(indexOfType >= 1)
    2. {
    3. ArrayBuffer.remove(0,indexOfType);
    4. if( ArrayBuffer.size() < ExpectedPacketLength )
    5. break;//We exit the loop
    6. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 21st June 2016, 23:32
  2. QSerialPort - speed and performance
    By Wer_Bn in forum Qt Programming
    Replies: 5
    Last Post: 30th April 2015, 17:12
  3. qserialport and reading bytes
    By gab74 in forum Newbie
    Replies: 3
    Last Post: 14th February 2014, 20:11
  4. QSerialPort :: read - Serial Comms noob.
    By llaregyb in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2014, 12:18

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.